Iterating Statements

345 views.

For Loop

This loop can be used for executing the statements multiple times. A for loop has to be used when we know the exact number of iterations.

Syntax:

 

Program:

Rule1:

All the three sections of for loop are optional. If we do not specify any initialization and if do not specify any increment/decrement, then the complier will not specify any initialization and will not specify any increment/decrement, but do not specify any condition then the compiler will automatically specify a Boolean value true.

Rule2:

Specifying the section separators (;) in a for loop are mandatory. for (;;) will execute for

Rule3:

In the for loop, initialization section and  the increment/decrement section can contain any number of valid java statements, but the condition section must contain a value of only Boolean type.

Rule4:

The initialization section can contain multiple initializations separated by a comma(,) all should be of same time and the data type should be specified only one time.

Rule5:

The condition section can contain any number of conditions, but they must be joined by using a logical operator (& && | || ^)

Rule6:

The increment/decrement section can contains multiple increments  or decrements but separated by  comma(,).

Rule7:

If a variable is declared inside for loop, then it can be used only in that for loop.

Rule8:

Specifying the {} is optional. If we don’t specify {} then it will consider only one statement, and that one statement is mandatory. If we want to consider multiple statements then specifying the {} is mandatory. Within the {} we can specify any number of statements.

While loop:

This loop can be used for executing the statements multiple times. A while loop has to be used when we do not know the exact number of iterations.

Syntax:

Program

Rule1:

Specifying a condition to while loop is mandatory and it should be of Boolean type.

Rule2:

Specifying the {} is optional. If we don’t specify {} then it will consider only one statement, and that one statement is mandatory. If we want to consider multiple statements then specifying the {} is mandatory. Within the {} we can specify any number of statements.

Note:

A java program should not contain any unreachable statements i.e. every statement has to be executed at some point of time.

Do-While loop

This loop can be used for executing the statement multiple times. A do-while loop has to be used when we do not know exact number of iterations.

Syntax

Program

Rule1:

Specifying a condition to do-while loop is mandatory and it should be of Boolean type.

Rule2:

Specifying the {} is optional. If we don’t specify {} then it will consider only one statement, and that one statement is mandatory. If we want to consider multiple statements then specifying the {} is mandatory. Within the {} we can specify any number of statements.

Nested Loop

If we specify a loop inside another loop then it is called as nested loop. Any loop can be specified inside any other loop any number of times.

Program1

Program2:

 

 

Leave a Reply