While looping

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 iterations is not fixed, it is recommended to use the while loop.

How Does a While loop execute?

A while loop in Java follows a specific execution pattern:

  1. Condition Evaluation: The loop begins by assessing the condition that has been specified. The condition is a boolean expression that determines whether the loop should continue running or not. If the condition is initially false, the loop might not execute at all if it’s a while loop.
  2. Condition Check: If the condition is true, the code block inside the loop is executed. If the condition is false, the loop is skipped, and the program moves to the code following the loop.
  3. Loop Body Execution: The code inside the loop body is executed. This code can consist of one or more statements. After executing the last statement in the loop body, the program returns to the condition check step.
  4. Back to Condition Check: The program goes back to step 1 and reevaluates the condition. If the condition is still true, the loop body is executed again; if it’s false, the loop terminates, and the program continues with the code after the loop.
  5. Loop Iteration: Steps 3 and 4 continue in a loop until the condition evaluates to false. Each cycle of these steps is called an “iteration.”

When do you use a while loop?

  •   when we don’t know how far we can go
  •   When we don’t know how many iterations are in any scenario then we use while loop.

Syntax of While Loop:

variable initialization

while(condition){

statement(s);

iteration;

}

Explanation:

  1. The while keyword is used to initiate a while loop.
  2. The condition is an expression that is evaluated before each iteration of the loop. If the condition is true, the loop’s body will be executed; otherwise, if the condition is false, the loop will terminate, and the program will move to the code after the loop.
  3. The code block enclosed within the curly braces {} is the body of the while loop. This code block contains the statements that will be executed repeatedly as long as the condition remains true.

Example of While Loop:

int i=1;  
    while(i<=10){  
        System.out.println(i);  
    i++;  

    }

Output:

1

2

3

4

5

6

7

8

9

10

Explanation:

  1. We start by declaring and initializing an integer variable i with the value 1.
  2. The while loop is initiated with the condition i <= 10. As long as this condition evaluates to true, the code block inside the loop will be executed repeatedly.
  3. Inside the loop, System.out.println(i); is used to print the current value of i to the console.
  4. After printing the value of i, i++ is used to increment the value of i by 1. This is equivalent to i = i + 1.
  5. The loop will then go back to check the condition i <= 10. If the condition is still true, the loop will continue to the next iteration, and the process repeats.
  6. The loop will continue executing and printing the values of i until the condition i <= 10 becomes false. In this case, the loop will terminate when i becomes 11.
  7. After the loop terminates, the program continues to the next line of code after the loop, if there is any.

The output is a sequence of numbers from 1 to 10, each on a new line, printed to the console. This is because the loop iterates through the values of i from 1 to 10 (inclusive) and prints each value in each iteration. Once i becomes 11 (after the loop), the condition i <= 10 becomes false, and the loop stops executing

 

Another Example of While Loop:

public class WhileLoopingExample {
    public static void main(String[] args) {
         int i=1;
           while (i<=5) {
   
                 System.out.println("Java");
                 i++;
           }

      }
}

Output:

Java

Java

Java

Java

Java

Explanation:

  1. int i = 1; This line declares a variable i of type integer and initializes it to the value 1. This variable will be used as a counter in the loop.
  2. while (i <= 5) {The while keyword initiates a while loop, which continues to execute the code block within the curly braces {} as long as the condition in the parentheses i <= 5 evaluates to true. In this case, the loop will continue as long as the value of i is less than or equal to 5.
  3. System.out.println(“Java”); This line prints the string “Java” to the console. The System.out.println() method is used to display the specified string and adds a new line after printing.
  4. i++; This line increments the value of the variable i by 1. It is equivalent to i = i + 1

The loop will continue to execute as long as the condition i <= 5 is true, and in each iteration, it will print “Java” to the console. The value of i will be increased by 1 in each iteration, starting from 1 and going up to 5. After the loop completes, the program will stop, and the output will be: The output consists of five lines, each containing the string “Java”.

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close