Day4_ControlStatements
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.Whether we can use continue statement in switch?
ANS:
The continue statement applies only to loops, not to a switch statement.
3.What is mean by control statements and types?
ANS:
-Java compiler executes the code from top to bottom.
-The statements in the code are executed according to the order in which they
appear.
-However, Java provides statements that can be used to control the flow of Java
code.
-Such statements are called control flow statements.
-It is one of the fundamental features of Java, which provides a smooth flow of
program.
Java provides three types of control flow statements.
1.Decision Making statements:
if statements
*if()
*if else()
*if else if else()
*nested if statement
switch statement
2.Loop statements:
do while loop
while loop
for loop
for-each loop
3.Jump statements:
break statement
continue statement
4.What is mean by for loop?
ANS:
a programming construct that allows a block of code to be executed repeatedly
until a specific condition is met.
5.Can you explain about for loop execution process?
ANS:
Java for loop helps execute a set of code repeatedly, and it is recommended when
the number of iterations is fixed.
6.What is difference between while and do-while?
ANS:
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);
7.What is the use of default keyword in switch?
ANS:
The default keyword specifies the default block of code in a switch statement.
The default keyword specifies some code to run if there is no case match in the
switch.
8.Difference between for and while loop?
ANS:
Feature for Loop
while Loop
------- --------
----------
Initialization Declared within the loop structure
Declared outside the loop;
and executed once at the beginning. should
be done explicitly before the loop.
Condition Checked before each iteration. Checked
before each iteration.
Update Executed after each iteration.
Executed inside the loop;
needs
to be handled explicitly
Use Cases Suitable for a known number of Useful
when the number of iterations is
iterations or when looping over ranges. not
known in advance or based on a condition.
Initialization Limited to the loop body. Scope
extends beyond the loop;
and Update Scope needs to be
handled explicitly.
QUESTIONS(Find the output)
-----------------------
QUESTION 1:
------------
package org.test;
public class Hello {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i == 5) {
}
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;
public class Hello {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
Output:
1
2
3
4
QUESTION 3:
----------
package org.test;
public class Hello {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
System.out.println(i);
}
Output:
1
2
3
4
6
7
8
9
10
QUESTION 4:
------------
package org.test;
public class Hello {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println(j);
}
Output:
1
2
3
1
2
3
1
2
3
QUESTION 5:
------------
package org.test;
public class Hello {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.println(i);
}
}
Output:
1
1
1
2
2
2
3
3
3
QUESTION 6:
-----------
package org.test;
public class Hello {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
System.out.println(j);
}
Output:
1
1
2
1
2
3
QUESTION 7:
-----------
package org.test;
public class Hello {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = i + 1; j <= 3; j++) {
System.out.println(j);
}
Output:
2
3
3
QUESTION 8:
------------
package org.test;
public class Hello {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = i + 1; j <= i; j++) {
System.out.println(j);
}
Output:
no output
QUESTION 9:
------------
package org.test;
public class Hello {
public static void main(String[] args) {
int i=5;
if (i == 5) {
break;
}
System.out.println(i);
}
Output:
output will not generate in if loop
QUESTION 10:
------------
package org.test;
public class Hello {
public static void main(String[] args) {
int i=5;
if (i == 5) {
continue;
}
System.out.println(i);
}
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