Loop Statement in Java

Loop Statement
Java has three kinds of loop statements:
  • while loop
  • for loop
  • do-while loop.
Loop statements allow a block of statements to be executed repetitively.
  • When a loop is executed its block of statements can be executed any number of times.
  •  Each execution of the block of statement is called an iteration of the loop.
  •  The number of iterations is controlled by given condition (boolean expression).
  • The boolean expression is evaluated before each iteration (pretest) in while loops and for loops, and after each iteration (post-test) in do-while loops.
  • When the given boolean expression becomes false the loop is terminated.
While Loop
The while loop is a pretest loop of statement. Because while loop checks the certain condition first, if the condition is true then all the statements or processes written under the while loop are executed otherwise ignored all.
Syntax of While loop
initilization;
    while (boolean expression) {
               statement1;
               statement2;
               …………………               
increment/decrement;

          }
Here,
  • Boolean expression is a condition that can be true or false.
  • Block of statements is a sequence of statements. If there is only single line statement then there is no requirement of braces.
  • First condition is tested and after that each iteration of the loop will be executed
  • If condition is false then loop terminates.
For Loop
The for loop is a pretest loop statement.
Syntax of  for loop(1)
for (initialization; boolean-expression; increment/decrement) {
               statement1;
               statement2;
               ……
                 }

Syntax of  for loop(2)
for (initialization; boolean-expression;) {
               statement1;
               statement2;
               ……
               increment/decrement;
                 }

Syntax of  for loop(3)
initialization;
for (;boolean-expression;) {
               statement1;
               statement2;
               ……

               increment/decrement;
                 }

Syntax of  for loop(4)
Infinite for loop for (;;) {
               statement1;
               statement2;
               ……
  }
Here, Here is the flow of control in a for loop:
  • The initialization step is executed first, and only once. This expression is usually an assignment (for example, i = 0) that sets the initial value of a loop control variable.
  • Next, the Boolean expression is evaluated. If it is true, the block of statement of the loop is executed. If condition is false, the block of statement of the loop is skipped.
  • Increment/Decrement  is an expression. At the end of each iteration, the increment/Decrement expression is evaluated.
Step of executing for loop
            Step 1. The initialization step is executed first, and only once.
            Step 2. In Second step Boolean Expression executed.
            Step 3. In Third step Block of Statement is executed.
            Step 4.  In Fourth step Increment/Decrement executed.
Do-While Loop
The do-while loop is a post-test loop statement. At least one time block of statement will be executed even if Boolean-expression is false.  It has the following form.
Syntax of do-while loop
    do {
               statement1;
               statement2;
               ………           
     increment/decrement;
    } while (boolean-expression);

Here,
  • First block of statement will be executed and after that condition is checked.
  • Block of statements is a sequence of statements. If there is only single line statement then there is no requirement of braces.
  • Boolean expression is a condition that can be true or false.
  • If condition is true then again block of statement will be executed, otherwise loop is terminated.
  • The boolean expression is tested after each iteration of the loop. The loop terminates when it is false.
 



 

Latest Tutorial


Most Visited Tutorial