0% found this document useful (0 votes)
68 views9 pages

Control Statements in Java (Loop's)

The document discusses the three types of loops in Java - for, while, and do-while loops. It provides the syntax and examples of each loop, and compares their usage based on whether the number of iterations is known. The for loop is used when the number of iterations is fixed. The while loop is used when the number is not fixed. The do-while loop is used when the number is not fixed and the loop needs to execute at least once.

Uploaded by

Helo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
68 views9 pages

Control Statements in Java (Loop's)

The document discusses the three types of loops in Java - for, while, and do-while loops. It provides the syntax and examples of each loop, and compares their usage based on whether the number of iterations is known. The for loop is used when the number of iterations is fixed. The while loop is used when the number is not fixed. The do-while loop is used when the number is not fixed and the loop needs to execute at least once.

Uploaded by

Helo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

CONTROL STATEMENTS IN JAVA

(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

Java For Loop vs While Loop vs Do While Loop

Compariso for loop while loop do while loop


n

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.

When to use If the number of iteration is If the number of If the number of


fixed, it is recommended to iteration is not fixed, iteration is not fixed
use for loop. it is recommended to and you must have
use while loop. to execute the loop
at least once, it is
recommended to use
the do-while loop.

Syntax for(init;condition;incr/decr) while(condition){ do{ //code to be


{ // code to be executed } //code to be executed executed }
} while(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 for for(;;){ //code to be while(true){ //code do{ //code to be


infinitive loop executed } to be executed } executed }
while(true);

Java For Loop


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.

o Simple for Loop


o for-each or Enhanced for Loop
o Labeled for Loop

Simple For Loop


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.

Syntax:

1. for (initialization; condition; increment/decrement)


2. {  
3. //statement or code to be executed  
4. }  
Example:

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

Java Nested for Loop


If we have a for loop inside the another loop, it is known as nested for loop. The inner loop
executes completely whenever outer loop executes.

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. }    

The different parts of while loop:

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++;

Flowchart of Java While Loop

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:

infinitive while loop


infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop

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);    

The different parts of do-while loop:

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++;

Flowchart of do-while loop:

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. }    

You might also like