Iteration Statements in Java

Iteration Statements in Java

by Sanjay Nair February 3, 2018
  • We can create loops in the program using iteration statements.
  • Alternatively speaking, the iterative statements repeat a set of statements until the condition for termination is not met.
  • There are three types of iteration statements in java.
  • They are for, while and do-while.
  1. The ‘while’ statement
  • The most fundamental loop statement of Java is the while loop. The while loop executes a statement over and over again until the controlling expression evaluates to true. The general form of while loop is:

  • The condition can be any boolean expression.
  • The body of the loop will be executed as long as the conditional expression is true.
  • Only boolean expressions could be used in the condition part.
  • The loop body will be executed over and over again as long as the condition is evaluated to true.

Output will be:

Num 5

Num 4

Num 3

Num 2

Num 1

 

  1. The ‘do-while’ statement
  • The do-while statement will execute the body atleast once as the conditional expression is placed at the bottom of the loop.
  • The general form of do-while loop is

  • During each iteration the body part is executed first and only then is the conditional expression evaluated.
  • If the conditional expression is true, then the loop will repeat.
  • Or else the loop is terminated. Just like other Java loops, the condition has to be a boolean expression.
  • The previous program demonstrating while loop can be written in do-while loop as:

Output will be:

 

Num 5

Num 4

Num 3

Num 2

Num 1

 

  1. for loop

 

  • The syntax of the for loop statement is

  • Previous program of while loop can be written in for loop as:

Output will be:

Num 5

Num 4

Num 3

Num 2

Num 1

 

  • There can be more than one statement in initilaization and iteration section.
  • They must be separated with comma.
  • The initialization and iteration section can contain more than one statement.
  • But the only condition to do that is to use comma between the sections.
  • This is illustrated below.

Output will be as follows:

 

c = 1

d = 4

c = 2

d = 3

 

  • In this example the values of c and d are set in the initialization section. The two statements in the body part are executed over and over again as long as the condition is true.

Nested Loops in Java

  • Loops can be nested as per requirement. Here is example of nesting the loops.
  • We can nest the loops as per our requirement. An example of nesting the loops is as follows.

  • We nested two for loops above.
  • The number of times the inner loop executes depends on the value of a in the outer loop.

The output of the program is:

……..

…….

……

…..

….

..

.

 

for-each loop

  • The advanced form of for loop is the for-each loop.
  • For-each loop makes iteration over collection of elements easier leaving very little room for error.
  • Syntax of for-each loop is:

Output:1

2
3
4

You may also like

Leave a Reply