Control Structure
Control Structure
1
Overview of Java statements
• Java has three kinds of control structures
Sequence statement
Selection (decision making) statements
• allows the code to execute a statement or blocks statements conditionally.
if statement
switch statement
Repetition statements
for statement
Performs the actions in its body zero or more time
while statement
Performs the actions in its body zero or more times
do…while statement
Performs the actions in its body one or more times2
The if Statement
• An if statement consists of a Boolean expression
followed by one or more statements.
• Execute an action if the specified condition is true.
• Syntax:
if(Boolean_expression)
{
/* Statement_1
Statement_2
Statement_n */
}
3
Example
public class IfStat{
public static void main(String args[]){
int x =10;
if( x <20){
System.out.print("This is if statement");
}
}
}
4
The if...else Statement
• An if statement can be followed by an optional else
statement, which executes when the Boolean
expression is false.
• Syntax:
if(Boolean_expression)
{
/*statements*/
} else {
/*statements*/
}
5
Example
public class IfElse{
public static void main(String args[]){
int x =30;
if(x <20){
System.out.print("This is if statement");
} else {
System.out.print("This is else statement");
}
}
}
6
The if...else if...else Statement
• An if statement can be followed by an optional else
if...else statement, which is very useful to test various
conditions using single if...else if statement.
• When using if, else if , else statements there are few
points to keep in mind.
– An if can have zero or one else's and it must come
after any else if's.
– An if can have zero to many else if's and they must
come before the else.
– Once an else if succeeds, none of the remaining
else if's or else's will be tested.
7
Syntax
if(Boolean_expression1){
/* statements */
} else if(Boolean_expression2){
/* statements */
} else if(Boolean_expression3){
/* statements */
} else {
/* statements */
}
8
Nested if...else Statement
• It is always legal to nest if-else statements which
means you can use one if or else if statement inside
another if or else if statement.
• Syntax:
if(Boolean_expression1){
/* statements */
if(Boolean_expression2){
/* statements */
}
}
9
Example
public class Test{
public static void main(String args[]){
int x =30;
int y =10;
if( x ==30){
if( y ==10){
System.out.print("X = 30 and Y = 10");
}
}
}
10
Switch Statement
• A switch statement allows a variable to be tested for equality against a list of
values.
• Each value is called a case, and the variable being switched on is checked
for each case.
• Syntax:
switch(expression){
case value :
//Statements
break;
case value :
//Statements
break;
//You can have any number of case statements.
default:
//Statements
} 11
Example
public class Test{
public static void main(String args[]){
char grade = 'C';
switch(grade) {
case'A':
System.out.println("Excellent!");
break;
case'B':
t p u t:
System.out.println("Excellent!"); Ou one
e l l d
break; W d e is C
case'C': r g ra
System.out.println("Well done"); You
break;
default:
System.out.println("Invalid grade");
}
System.out.println("Your grade is "+ grade);
}
13
}
Repetition Statements
• Repetition statements allow us to execute a statement multiple
times repetitively.
• Java has three kinds of repetition statements: the while loop, the
do…while loop, and the for loop.
• The programmer must choose the right kind of loop for the
situation. 14
The while Statement
• The while statement has the following syntax:
while ( condition )
while is a
statement;
reserved word
15
The while Statement
• Note that if the condition of a while statement is false
initially, the statement is never executed
16
The while Statement
//While Loop Example
public class Main {
public static void main(String args[]){
Output:
int a= 10;
a : 10
while( a < 20 ){ a : 11
System.out.print("a : " + a ); a : 12
a++; a : 13
System.out.print("\n"); a : 14
} a : 15
a : 16
}
a : 17
} a : 18
a : 19 17
The do Statement
• The do statement has the following syntax:
Uses both do
the do and {
while statement;
reserved }
words while ( condition )
18
The do Statement
• A do loop is similar to a while loop, except that the condition is
evaluated after the body of the loop is executed.
• Therefore the body of a do loop will execute at least one time.
//Do… While Loop Example
public class Main {
public static void main(String args[]){ Output:
int a= 3;
a:3
do{
a:4
System.out.print(" a : " + a );
a++;
a:5
System.out.print("\n"); a:6
}while( a < 10 ); a:7
} a:8
} a:9
19
The for Statement
• The for statement has the following syntax:
20
The for Statement
• A for loop is equivalent to the following while
loop structure:
initialization;
while ( condition )
{
statement;
increment;
}
21
The for Statement
• Like a while loop, the condition of a for
statement is tested prior to executing the loop
body
• Therefore, the body of a for loop will execute
zero or more times
• It is well suited for executing a specific
number of times that can be determined in
advance
22
Example
public class Main {
public static void main(String[] args) {
int i,j;
for(i=0;i<=10;i++) { Output:
for(j=10;j>=1;j--) *
if(j<=i)
**
***
System.out.print("*");
****
else *****
System.out.print(" "); ******
System.out.println(); *******
} ********
} *********
**********
} 23
break and continue statements
• beak statement
– tells the compiler to exit the switch statement.
• continue statement
– this statement causes the program to jump to the
next iteration of the loop.
24