Control Statements
Control statements are statements which alter the normal execution flow of a
program
There are three types of Control Statements in java
Selection statement Iteration Statement Jumping Statement
if while break
if – for continu
else do – while e
switch return
1
Cascading if- else
Syntax:
if (condition1)
{
statement-1
}
….
else if(conditio-n)
{
statement-n
}
else
{
default statement
}
next statement coial 2
else - if Example
/* program to print seasons for a month input using if & else if
*/
public class ElseIfDemo {
public static void main(String[] args) {
int month = [Link](args[0]);
if(month == 12 || month == 1 || month ==
2) [Link]("Winter");
else if(month == 3 || month == 4 || month ==
5) [Link]("Spring");
else if(month == 6 || month == 7 || month ==
8) [Link]("Summer");
else if(month == 9 || month == 10 ||
month == 11)
[Link]("Autumn");
else
}
If args[0] is 6 then the Output is :
} [Link]("invalid month");
Summer [Link] confidetial 3
Switch Case
The switch-case conditional construct is a more structured way of testing for multiple
conditions rather than resorting to a multiple if statement
Syntax:
switch (expression) {
case value-1 : case-1 block
break;
case value-2 : case-2 block
b
default reak;
: default block
break;
}
statement-x;
© [Link] confidenti 4
Switch Case - Example
/* This is an example of a switch case statement*/
public class SwitchDemo {
public static void main(String[] args)
{ int weekday =
[Link](args[0]);
switch(weekday)
case 1: {
[Link](“Sunday"); break;
case 2: [Link](“Monday"); break;
case 3: [Link](“Tuesday"); break;
case 4: [Link](“Wednesday"); break;
case 5: [Link](“Thursday");
case 6: break;
case 7: [Link](“Friday"); break;
default: [Link](“Saturday");
} break; [Link](“Invalid
} day");
}
If args[0] is 6 then the Output is : Friday
confidenti 5
while loop – Example
/* This is an example for a while loop */
public class Sample{
public static void main(String[] args)
{ int i = 0;
while (i < 5) {
[Link]("i: "+i);
i = i + 1; Output:
} i: 0
i: 1
} i: 2
} i: 3
i: 4
© 2017 [Link] confidenti 6
do-while loop
Syntax:
Body of the loop
do
{
Body of the loop false
} while(boolean expression); Boolean
expression
tru
e
[Link] confidenti 7
do…while loop – Example
/* This is an example of a do-while loop */
public class Sample {
public static void main(String[] args)
{
int i =5;
do {
Syste
[Link]
.prin
} tln(" Output:
i: 5
} i:
"+i);
i = i
[Link] confidenti 8
Enhanced for loop - Example
/* This is an example of a enhanced for loop
*/
public class Sample {
public static void main(String[] args)
{ int [] numbers = {10, 20, 30, 40,
50}; for(int i : numbers ) { Outpu
[Link]( "i: "+i ); t: i:10
i:2
} 0 i:
30
}
i:40
} i:50
[Link] confidenti 9
continue statement
The continue statement skips the current iteration of a loop
In while and do loops, continue causes the control to go directly to the test-condition
and then continue the iteration process
In case of for loop, the increment section of the loop is executed before the test-
condition is evaluated
[Link] confidenti 10