Chapter 10
Chapter 10
Iteration constructs
In java
Introduction
• In real-life software programming, there are many instances when we
have to repeat a specific task again and again.
• There are two ways by which we can do such tasks.
• First, we can code the same thing again and again.
• This method is highly inefficient and violates the DRY (Don't Repeat
Yourself) principle.
• The other option is to use a technique in Java which is known as loops.
• Loops are the iteration statements that are used to perform a certain
task repeated a number of times without having to code again and
again.
• It is highly efficient.
• Java provides three types of iteration statements:
– for statements
– while statements
– do-while statements
Java for Loop
• 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.
• 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.
• Increment/Decrement: It increments or decrements the
variable value. It is an optional condition.
• Statement: The statement of the loop is executed each
time until the second condition is false.
Syntax:
for(initialization; condition; increment/decrement)
{
//statement or code to be executed
}
Example:
ForExample.java
Introduction The Java for loop is a control The Java while loop is a The Java do while loop is a
flow statement that iterates a control flow statement that control flow statement that
part of the programs multiple executes a part of the executes a part of the
times. programs repeatedly on the programs at least once and
basis of given boolean the further execution depends
condition. upon the given boolean
condition.
When to use If the number of iteration is If the number of iteration is If the number of iteration is
fixed, it is recommended to not fixed, it is recommended not fixed and you must have
use for loop. to use while loop. to execute the loop at least
once, it is recommended to
use the do-while loop.
}
}
}
Java do-while Loop
• The Java do-while loop is used to iterate a part of
the program repeatedly, until the specified
condition is true.
• If the number of iteration is not fixed and you must
have to execute the loop at least once, it is
recommended to use a do-while loop.
• Java do-while loop is called an exit control loop.
• Therefore, unlike while loop and for loop, the do-
while check the condition at the end of loop body.
• The Java do-while loop is executed at least once
because condition is checked after loop body.
Syntax:
do{
//code to be executed / loop body
//update statement
}while (condition);
2. Update expression: Every time the loop body is executed, the this
expression increments or decrements loop variable.
– Example:
• i++;
public class DoWhileExample {
public static void main(String[] args) {
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
}
}
Java Infinitive do-while Loop
• If you pass true in the do-while loop, it will be
infinitive do-while loop.
– Syntax:
do{
//code to be executed
}while(true);
public class DoWhileExample2 {
public static void main(String[] args) {
do{
System.out.println("infinitive do while loop"
);
}while(true);
}
}
Ctrl+c (command to terminate the infinite loop.)
Java 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.
– Syntax:
jump-statement;
break;
public class BreakExample {
public static void main(String[] args) {
//using for loop • Output:
for(int i=1;i<=10;i++){ • 1
if(i==5){ • 2
//breaking the loop • 3
break; • 4
}
System.out.println(i);
}
}
}
Java Break Statement with Inner Loop
public class BreakExample2 {
public static void main(String[] args) {
//outer loop
for(int i=1;i<=3;i++){
//inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2){
//using break statement inside the inner loop
break;
}
System.out.println(i+" "+j);
}
}
}
}
• Output:
• 11
• 12
• 13
• 21
• 31
• 32
• 33
Java Break Statement with Labeled For
Loop
• We can use break statement with a label. The
feature is introduced since JDK 1.5. So, we can
break any loop in Java now whether it is outer
or inner loop.
• /Java Program to illustrate the use of continue statement
• //with label inside an inner loop to break outer loop
• public class BreakExample3 {
• public static void main(String[] args) {
• aa:
• for(int i=1;i<=3;i++){
• bb:
• for(int j=1;j<=3;j++){
• if(i==2&&j==2){
• //using break statement with label
• break aa;
• }
• System.out.println(i+" "+j);
• }
• }
• }
• }
Java Break Statement in while loop
• /Java Program to demonstrate the use of break statement
• //inside the while loop.
• public class BreakWhileExample {
• public static void main(String[] args) {
• //while loop
• int i=1;
• while(i<=10){
• if(i==5){
• //using break statement
• i++;
• break;//it will break the loop
• }
• System.out.println(i);
• i++;
• }
• }
• }
Java Break Statement in do-while loop
• //Java Program to demonstrate the use of break statement
• //inside the Java do-while loop.
• public class BreakDoWhileExample {
• public static void main(String[] args) {
• //declaring variable
• int i=1;
• //do-while loop
• do{
• if(i==5){
• //using break statement
• i++;
• break;//it will break the loop
• }
• System.out.println(i);
• i++;
• }while(i<=10);
• }
• }
Java 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.
• Syntax:
• jump-statement;
• continue;
• //Java Program to demonstrate the use of continue statement
• //inside the for loop.
• public class ContinueExample {
• public static void main(String[] args) {
• //for loop
• for(int i=1;i<=10;i++){
• if(i==5){
• //using continue statement
• continue;//it will skip the rest statement
• }
• System.out.println(i);
• }
• }
• }
Java Continue Statement with Inner Loop
• /Java Program to illustrate the use of continue statement
• //inside an inner loop
• public class ContinueExample2 {
• public static void main(String[] args) {
• //outer loop
• for(int i=1;i<=3;i++){
• //inner loop
• for(int j=1;j<=3;j++){
• if(i==2&&j==2){
• //using continue statement inside inner loop
• continue;
• }
• System.out.println(i+" "+j);
• }
• }
• }
• }
Java Continue Statement with Labelled For
Loop
• /Java Program to illustrate the use of continue statement
• //with label inside an inner loop to continue outer loop
• public class ContinueExample3 {
• public static void main(String[] args) {
• aa:
• for(int i=1;i<=3;i++){
• bb:
• for(int j=1;j<=3;j++){
• if(i==2&&j==2){
• //using continue statement with label
• continue aa;
• }
• System.out.println(i+" "+j);
• }
• }
• }
• }
Java Continue Statement in while loop
• //Java Program to demonstrate the use of continue statement
• //inside the while loop.
• public class ContinueWhileExample {
• public static void main(String[] args) {
• //while loop
• int i=1;
• while(i<=10){
• if(i==5){
• //using continue statement
• i++;
• continue;//it will skip the rest statement
• }
• System.out.println(i);
• i++;
• }
• }
• }
• Test it
Java Continue Statement in do-while Loop
• //Java Program to demonstrate the use of continue statement
• //inside the Java do-while loop.
• public class ContinueDoWhileExample {
• public static void main(String[] args) {
• //declaring variable
• int i=1;
• //do-while loop
• do{
• if(i==5){
• //using continue statement
• i++;
• continue;//it will skip the rest statement
• }
• System.out.println(i);
• i++;
• }while(i<=10);
• }
• }