1-8-Iterative Constructs in Java
1-8-Iterative Constructs in Java
ITERATIVE CONSTRUCTS
IN JAVA
By,
Ashwini K S
Lorven Public School, Chandarapura
2
INTRODUCTION
• In this, if the condition is true the control is allowed to enter into the loop otherwise
the entry will be denied.
‘FOR’ LOOP
EXAMPLE
for (i=0 ; i< 100 ; i++)
{
System.out.println(“ Welcome to Java”);
}
This loop repeats for 100 times i.e., from 0 to 99.
Every the loop gets executed the statement welcome to Java is printed.
When i becomes 100 then the condition fails and control comes out of the loop.
https://www.geeksforgeeks.org/java-for-loop-with-examples/
‘WHILE’ LOOP
EXAMPLE
https://www.geeksforgeeks.org/java-while-loop-with-exam
ples
/
Lorven Public School, Chandarapura
9
• An exit control loop, controls exit of the loop, that's why it is referred to as exit
control loop.
• An exit control loop checks the condition for exit and if given condition for exit
evaluate to FALSE, control will exit from the loop body or else control will enter
again into the loop.
• In this loop body is executed first and then the given condition is checked
afterwards.
• An example of exit controlled loop is Do While Loop.
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.
CONTINUE STATEMENT
• The continue statement is used in loop control structure when you need to jump to
the next iteration of the loop immediately.
• 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.
• https://compiler.javatpoint.com/opr/test.jsp?filename=ContinueExample
}
System.out.println(i);
}
}
Lorven Public School, Chandarapura }
18
INTERCONVERSION OF LOOPS
i=1; i=1;
for(i=1; i<p ; i++)
While(i<p) do
{
{ {
if (a %i==0 &&b if(a% i==0&&b%i==0)
%i==0) if(a% i==0&&b
%i==0) gcd=i;
gcd=i; i++;
gcd=i;
} i++; }
} While(i<p);
Lorven Public School, Chandarapura
20
INFINITE LOOP
EMPTY/NULL LOOP
• Empty loops are used create delay in the execution of the program.
• They are also referred as delay loops.