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 = Integer.parseInt(args[0]);
if(month == 12 || month == 1 || month ==
2) System.out.println("Winter");
else if(month == 3 || month == 4 || month ==
5) System.out.println("Spring");
else if(month == 6 || month == 7 || month ==
8) System.out.println("Summer");
else if(month == 9 || month == 10 ||
month == 11)
System.out.println("Autumn");
else
}
If args[0] is 6 then the Output is :
} System.out.println("invalid month");
Summer wip.om 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;
© wipro.co 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 =
Integer.parseInt(args[0]);
switch(weekday)
case 1: {
System.out.println(“Sunday"); break;
case 2: System.out.println(“Monday"); break;
case 3: System.out.println(“Tuesday"); break;
case 4: System.out.println(“Wednesday"); break;
case 5: System.out.println(“Thursday");
case 6: break;
case 7: System.out.println(“Friday"); break;
default: System.out.println(“Saturday");
} break; System.out.println(“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) {
System.out.println("i: "+i);
i = i + 1; Output:
} i: 0
i: 1
} i: 2
} i: 3
i: 4
© 2017 wipro.co confidenti 6
do-while loop
Syntax:
Body of the loop
do
{
Body of the loop false
} while(boolean expression); Boolean
expression
tru
e
wipro.co 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
m.out
.prin
} tln(" Output:
i: 5
} i:
"+i);
i = i
wipro.co 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
System.out.println( "i: "+i ); t: i:10
i:2
} 0 i:
30
}
i:40
} i:50
wipro.co 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
wipo.com confidenti 10