Unit 2
Control Statements
If-else Statement
The Java if statement is used to test the condition. It checks boolean
condition: true or false. There are various types of if statement in
Java.
• if statement
• if-else statement
• if-else-if ladder
• nested if statement
if Statement
if(condition){
//code to be executed
}
if-else Statement
if(condition){
//code if condition is true
}else{
//code if condition is false
}
if-else-if ladder Statement
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//
code to be executed if all the conditions are fal
se
}
if-else Statement
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}
Switch Statement
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Loops
In programming languages, loops are used to execute a set of
instructions/functions repeatedly when some conditions become true.
There are three types of loops in Java.
For Loops
The Java for loop is used to iterate a part of the program
several times. If the number of iteration is fixed, it is
recommended to use for loop.
There are three types of for loops in java.
• Simple For Loop
• For-each or Enhanced For Loop
• Labeled For Loop
For Loops
for(initialization;condition;incr/decr){
//statement or code to be executed
}
For Loops
It consists of four parts:
1. Initialization: It is the initial condition which is executed once
when the loop starts. Here, we can initialize the variable, or we
can use an already initialized variable. It is an optional
condition.
2. Condition: It is the second condition which is executed each
time to test the condition of the loop. It continues execution
until the condition is false. It must return boolean value either
true or false. It is an optional condition.
3. Statement: The statement of the loop is executed each time
until the second condition is false.
4. Increment/Decrement: It increments or decrements the
variable value. It is an optional condition.
While Loops
while(condition){
//code to be executed
}
Do-while Loops
do{
//code to be executed
}while(condition);
Break Statement
When a break statement is encountered inside a loop, the loop is
immediately terminated and the program control resumes at the
next statement following the loop.
The Java break statement is used to break loop or switch
statement. It breaks the current flow of the program at specified
condition. In case of inner loop, it breaks only inner loop.
We can use Java break statement in all types of loops such as for
loop, while loop and do-while loop.
Break Statement
jump-statement;
break;
Continue Statement
The continue statement is used in loop control structure when you
need to jump to the next iteration of the loop immediately. It can be
used with for loop or while loop.
The Java continue statement is used to continue the loop. It
continues the current flow of the program and skips the remaining
code at the specified condition. In case of an inner loop, it continues
the inner loop only.
We can use Java continue statement in all types of loops such as for
loop, while loop and do-while loop.