Chapter 7_1: Java Loops

Java Loop

Sometimes it is necessary for the program to execute the statement several times, and Loops can execute a block of code as long as a specified condition is reached.

Java supports sevreal types of loop in java

  1. While loop
  2. Do-While loop
  3. For loop

Java While Loop

while loop is the most basic loop in Java. It has one control condition and executes as long the condition is true. The condition of the loop is tested before the body of the loop is executed; hence it is called an entry-controlled loop.

Syntax
while (condition) {
  // code block to be executed
}

In the example below, the code in the loop will run as long as a variable (y) is less than 10:

Example
public class LoopDemo {

	public static void main(String[] args) {
		int y = 1;
		while (y < 10) {
			System.out.println(y);
			y++;
		}
	}

}

Output:

1
2
3
4
5
6
7
8
9

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.This loop is an exit-controlled loop.

Syntax
do {
  // code block to be executed
}
while (condition);

The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:

Example
public class LoopDemo {

	public static void main(String[] args) {

		int y = 1;
		do {
			System.out.println(y);
			y++;

		} while (y < 10);

	}
}

Output:

1
2
3
4
5
6
7
8
9

Java For Loop

Java for loops is very similar to Java while loops in that it continues to process a block of code until a statement becomes false.When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax
for ( init; condition; increment ) 
{ statement(s); }
  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.
Example
public class LoopDemo {

	public static void main(String[] args) {

		//Code of Java for loop  
	    for(int i=1;i<=5;i++){  
	        System.out.println(i);  
	    }  
		
	}
}

Output:

1
2
3
4
5

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
public class LoopDemo {

	public static void main(String[] args) {

		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("* ");
			}
			System.out.println();// new line
		}

	}
}

Output:

* 
* * 
* * * 
* * * * 
* * * * * 

To understand more about Loop , see below video –

7_1
https://youtu.be/rB2AiuAIfOA