Control Statements in Java (Loop's)
Control Statements in Java (Loop's)
(loop’s)
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.
o for loop
o while loop
o do-while loop
Introduction The Java for loop is a The Java while loop The Java do while
control flow statement that is a control flow loop is a control
iterates a part of statement that flow statement that
the programs multiple executes a part of the executes a part of the
times. programs repeatedly programs at least
on the basis of given once and the further
boolean condition. execution depends
upon the given
boolean condition.
Example //for loop for(int //while loop int i=1; //do-while loop int
i=1;i<=10;i++) while(i<=10) i=1;
{ System.out.println(i); } { System.out.println( do{ System.out.print
i); i++; } ln(i); i++; }
while(i<=10);
Syntax:
1. //Java Program to demonstrate the example of for loop
2. //which prints table of 1
3. public class ForExample
4. {
5. public static void main(String[] args)
6. {
7. //Code of Java for loop
8. for(int i=1;i<=10;i++)
9. {
10. System.out.println(i);
11. }
12. }
13. }
Output:
1
2
3
4
5
6
7
8
9
10
Example:
1. public class NestedForExample
2. {
3. public static void main(String[] args)
4. {
5. //loop of i
6. for(int i=1;i<=3;i++)
7. {
8. //loop of j
9. for(int j=1;j<=3;j++)
10. {
11. System.out.println(i+" "+j);
12. }//end of j
13. }//end of i
14. }
15. }
Output:
11
12
13
21
22
23
31
32
33
A Pyramid Example:
1. public class PyramidExample
2. {
3. public static void main(String[] args)
4. {
5. for(int i=1;i<=5;i++)
6. {
7. for(int j=1;j<=i;j++)
8. {
9. System.out.print("* ");
10. }
11. System.out.println();//new line
12. }
13. }
14. }
Output:
*
**
***
****
*****
While Loop
The Java while loop is used to iterate a part of the program repeatedly until the specified
Boolean condition is true. As soon as the Boolean condition becomes false, the loop
automatically stops.
The while loop is considered as a repeating if statement. If the number of iteration is not
fixed, it is recommended to use the while loop.
Syntax:
1. while (condition){
2. //code to be executed
3. I ncrement / decrement statement
4. }
1. Condition: It is an expression which is tested. If the condition is true, the loop body is
executed and control goes to update expression. When the condition becomes false, we exit
the while loop.
Example:
i <=100
2. Update expression: Every time the loop body is executed, this expression increments or
decrements loop variable.
Example:
i++;
Here, the important thing about while loop is that, sometimes it may not even execute. If the
condition to be tested results into false, the loop body is skipped and first statement after the
while loop will be executed.
Example:
In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately
need to initialize and increment the variable used in the condition (here, i). Otherwise, the
loop will execute infinitely.
WhileExample.java
1. public class WhileExample {
2. public static void main(String[] args) {
3. int i=1;
4. while(i<=10){
5. System.out.println(i);
6. i++;
7. }
8. }
9. }
Output:
1
2
3
4
5
6
7
8
9
10
Infinitive While Loop
If you pass true in the while loop, it will be infinitive while loop.
Syntax:
1. while(true){
2. //code to be executed
3. }
Example:
WhileExample2.java
1. public class WhileExample2 {
2. public static void main(String[] args) {
3. // setting the infinite while loop by passing true to the condition
4. while(true){
5. System.out.println("infinitive while loop");
6. }
7. }
8. }
Output:
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:
1. do{
2. //code to be executed / loop body
3. //update statement
4. }while (condition);
1. Condition: It is an expression which is tested. If the condition is true, the loop body is
executed and control goes to update expression. As soon as the condition becomes false, loop
breaks automatically.
Example:
i <=100
2. Update expression: Every time the loop body is executed, the this expression increments or
decrements loop variable.
Example:
i++;
Note: The do block is executed at least once, even if the condition is false.
Example:
In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately
need to initialize and increment the variable used in the condition (here, i). Otherwise, the
loop will execute infinitely.
DoWhileExample.java
1. public class DoWhileExample {
2. public static void main(String[] args) {
3. int i=1;
4. do{
5. System.out.println(i);
6. i++;
7. }while(i<=10);
8. }
9. }