bscodinglab

Looping Statements

Looping statements in Java allow you to repeatedly execute a block of code as long as a certain condition is true. Additionally, Java also has an enhanced for loop for iterating through arrays or collections.

  • While
  • Do-While
  • For loop
  • Nested For loop
  1. While :- A while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. If the condition is true, the code block is executed. The condition is then evaluated again, and the process continues until the condition is false. If the condition is false when the loop is first reached, the code block will not be executed at all.

Here’s how the while statement works

  • The condition is evaluated before entering the loop. If the condition is true, the code inside the loop is executed. If the condition is false initially, the loop is skipped entirely, and the program continues with the code following the while loop.
  • After executing the code inside the loop, the program returns to the while statement, re-evaluates the condition, and repeats the process. If the condition is still true, the loop continues to execute; otherwise, it terminates, and control passes to the code following the while loop.

Syntax :-

while (condition)

 {

                  // Statements Write Hare.

}

Example :-

int count = 1;

while (count <= 5)

         {

                System.out.println(“Count is :  ” + count);

                count++;

         }

 Output :-   Count is : 1

                    Count is : 2

Count is : 3

Count is : 4

Count is : 5

While loops can be used to implement a wide variety of tasks, such as printing a list of items, calculating a sum, or searching for a specific value.

2. Do-While :- In Java provides a “do-while” statement, which is a variation of the “while” loop. The “do-while” loop is used to execute a block of code at least once and then repeatedly execute it based on a specified condition.

Syntex :-

do {

// Write Statements hare.

} while (condition);

Here’s how the “do-while” loop works

    1. Do :– This keyword marks the start of the loop.
    2. {…} :- The block of code inside the curly braces is the body of the loop. It contains the code that will be executed repeatedly.
    3. while (condition) :- The while keyword is followed by a condition in parentheses. The code inside the do block will be executed as long as this condition evaluates to true. After each iteration, the condition is checked, and if it is true, the loop continues. If it is false, the loop is exited, and the program continues with the next statement after the do-while

Example :-

int i = 1;

do{

System.out.println(i);

i++;

        }while (i <= 5);

Output :-    1

2

3

4

5

In this example:-

    • The do block contains code to print the value of i.
    • i is incremented in each iteration.
    • The loop will continue executing as long as i is less than or equal to 5. Once i becomes 6, the condition is false, and the loop terminates.

3. For loop :- A “For” Loop is used to repeat a specific block of code several times. We can initialize the variable, check condition and increment/decrement value. It consists of four parts

    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. Increment/Decrements :- It increments or decrements the variable value. It is an optional condition..
    4. Statement :- The statement of the loop is executed each time until the second condition is false. After the condition becomes false, the ‘for’ loop terminates.
for-java

Syntex :-  for( Init;  Condition;  Increment )

                       {

                              // Statements Write hare.

                       }

Example :-

                      // create an array of numbers

            int[] numbers = {1, 2, 3, 4, 5};

 // print the elements of the array using a for loop

 for (int num : numbers)

{

      System.out.println(number);

}

Output :-    1

2

3

4

This example uses a for-each loop, which is a simplified version of the for loop that is used to iterate over arrays and collections.

For loops can be used to perform a variety of tasks, such as:

    • Printing a sequence of numbers
    • Iterating over an array or collection
    • Performing a calculation on a set of data
    • Searching for a specific value in a data structure

4.Nested For loop :- A nested for loop is a loop inside another loop. Nested for loops can be used to solve a variety of problems. For example, we can use nested for loops to print a 2D matrix, iterate over a multi-dimensional array, or generate complex patterns.

 Syntex :-      // Outer loop

                        for( Init;  Condition;  Increment )

                             {

                                    // Inner loop

                                    for( Init;  Condition;  Increment )

                                         {

                                                // Statements Write hare.

                                         }

                             }

Example :-

      Int n=1;

        // Outer loop iterates from 1 to 3

        for (int i = 1; i <= 3; i++)

 {

          // Inner loop iterates from 1 to i

            for (int j = 1; j <= 3; j++)

{

      System.out.print(n);

 }

         System.out.println();  / / This Statement  has been written to go on a new line.

    }

          Output :-   1         2          3

                              4          5          6

                               7          8          9

In this example, the outer loop iterates from 1 to 3. For each iteration of the outer loop, the inner loop iterates from 1 to 3. This means that the code inside the inner loop will be executed 3 times for each iteration of the outer loop.