5.ControlStatements Answer
5.ControlStatements Answer
DAY4:
------
1.if/else if
2.Loopings(for, while, do-while)
3.Switch case
4.break/continue
QUESTIONS(Theory)
------------------
1.What is difference between break and continue?
ANS:
1. break - The java break statement is used to break the loop. it breaks the
current flow of the program at specified condition.
2. continue - the java continue statement is used to continue the loop. it
continues the current flow of the program and skip the remaining code at the
specified condition
2.Loop statements:
do while loop
while loop
for loop
for-each loop
3.Jump statements:
break statement
continue statement
while:
-Condition is checked first then statement(s) is executed.
-It might occur statement(s) is executed zero times, If condition is false.
-No semicolon at the end of while. while(condition)
-Variable in condition is initialized before the execution of loop.
-while loop is entry controlled loop.
-while(condition) { statement(s); }
do-while:
-Statement(s) is executed at least once, thereafter condition is checked.
-At least once the statement(s) is executed.
-Semicolon at the end of while. while(condition);
-variable may be initialized before or within the loop.
-do-while loop is exit controlled loop.
-do { statement(s); } while(condition);
}
System.out.println(i);
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
QUESTION 2:
------------
package org.test;
Output:
1
2
3
4
QUESTION 3:
----------
package org.test;
}
Output:
1
2
3
4
6
7
8
9
10
QUESTION 4:
------------
package org.test;
Output:
1
2
3
1
2
3
1
2
3
QUESTION 5:
------------
package org.test;
}
Output:
1
1
1
2
2
2
3
3
3
QUESTION 6:
-----------
package org.test;
Output:
1
1
2
1
2
3
QUESTION 7:
-----------
package org.test;
Output:
2
3
3
QUESTION 8:
------------
package org.test;
Output:
no output
QUESTION 9:
------------
package org.test;
Output:
QUESTION 10:
------------
package org.test;
Output:
output will not generate in if loop
QUESTIONS(Programs)
-------------------
QUESTION 1:
-----------
Description: Write Java program to allow the user to input his/her age.
Then the program will show if the person is eligible to vote.
A person who is eligible to vote must be older than or equal 1 to 18
years old.
Example:
--------
Input = 10
Output = print not eligible.
QUESTION 2:
-----------
Description: Write a program to find even or odd number
Example:
---------
Input = 10
Output = Even
QUESTION 3:
------------
Description: Write a program to print even number from 1 to 100
Example:
---------
Output = 2,4,....100
QUESTION 4:
------------
Description: Find the sum of odd number 1 to 100
Example:
--------
Output = 2500
QUESTION 5:
-----------
Description: Count of even number 1 to 100
Example:
--------
Output = 50
QUESTION 6:
-----------
Description: Write a program to find the factorial of a number.
Example:
--------
Input = 5
Output = 120
QUESTION 7:
------------
Description: Write a program to print the Fibonacci series of a number 1 to 100.
Example:
--------
Output = 0,1,1,2,3,5.....
QUESTION 8:
-----------
Description: Find prime number or not.
Example:
--------
Input = 11
Output = prime number
QUESTION 9:
-----------
Description : Print the below patterns using for loop.
Output:
-------
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
-----------------------
*
* *
* * *
* * * *
* * * * *
--------------------------
*
* *
* * *
* * * *
* * * * *
-----------------------------
QUESTION 10:
-------------
Description: Find Amstrong number or not
Example:
--------
Input = 153
Output = Amstrong number
QUESTION 11:
-------------
Description: Reverse the number
Example:
--------
Input = 123
Output = 321
QUESTION 12:
-------------
Description: Count of the number
Example:
--------
Input = 123
Output = 3
QUESTION 13:
-------------
Description: Sum of the number
Example:
--------
Input = 123
Output = 6
QUESTION 14:
--------------
Description: Verify the number is palindrome number not
Example:
--------
Input = 141
Output = Palindrome