Iterative Constructs in Java for(),while and do-while() Loops (Java)

 Sometimes, it is required that a process should be repeated many time in a program and it is possible with Iteration Statements. These statements covers for(), while() and do-while() functions. These iteration statements repeat set of instructions upto a limit and terminated as soon as the condition is fulfilled.

Program Construct:

They are devided into following categories:
1) Sequence : The sequence construct means statements executes in sequence.
2) Selection - if(), switch() : The selection construct means the statements executes depending upon the used conditions.
3) Iteration - while(), do_while(), for() : The iteration construct means repetition of set of statements upto a limit depending upon condition.

So Today we will study about Iteration Statements.

Iteration also called Loops:

To perform any operations more than one time the iteration/loop statements are used. The iteration or loop statements mainly divided in three (3) parts or parameters as its syntax namely:
1) Initial or starting value of the iteration or loop statement,
2) Ending or stopping condition of the iteration or loop statement.
3) Step increment/decrement value of the iteration or loop statement (also known as update statement).
The placement/position of the 3 parameters mentioned above may change as per the requirement. 
The iteration or loop statements are mainly in three types: 
(1) for()
(2) while() 
(3) do-while()

1.1 The for() loop

The for() loop is also known as entry controlled loop.
Definition: It allows a set of statements to be repeated or looped through a fixed number of times.
Syntax:
        for(initial-value;test-condition;update-statement)
        {
                statement/statements;
        }


Note: All the 3 parameters in a for() loop do have a unique role to play. If anyone is missing the result would be different than what we expect.
Example:
            for(int i = 1;i <= 5;i++)
            {
                System.out.println("I = " + i);
            }
The above program code will print the value of 'i' from 1 to 5 in different lines as follows:
Output:
I = 1
I = 2
I = 3
I = 4
I = 5
Working of the loop() mentioned in the example:
Remember that every for() loop works like this.

In the above diagram after accepting the initial value i = 1 at point (1) the control goes for condition check (i <= 5) at point (2) and if it is true, the control enters into the loop (i.e., inside the opening curly brace) and executes the printing statement at point (3) to print the value of variable 'i', then after the control goes to closing brace of the loop, from here it follows point (4) and goes to update statement to change the value of variable 'i', from here it again checks the condition at point (2), if true, then again follows point (6) to perform the execution of the statements. Similarly point (4), (5), (2) and (6) are repeated till the condition is true, when the condition becomes false, the control comes out of the for() loop to execute all the statements given after the loop.

1.1.1 Effect of placing a semicolon after the for() loop

If the semicolon is placed after the for() statement then the loop will execute the statements following the for() statement after the execution of the loop is finished.
Example: 
            for(int i = 1;i < 5;i++);
            {
                System.out.println("I = " + i);
            }
Output:
I = 5
It is because of the present of semicolon after the for() statement, as because of semicolon after initialization and checking the condition, which is true, tries to enter the body of the loop but because of semicolon it goes to update statement and it repeats until the condition gets false, and once the condition is false, the loop terminates and goes out of the loop and it gets inside the body of the loop and executes the the statement that prints output I = 5.

1.2 Nested for() statement

Definition: The presence of one or more for() loops with in a for() loop is known as nested for() loop statement.
Syntax:
            for(initial-value;test-condition;update-statement)
            {
                for(initial-value;test-condition;update-statement)
                {
                    statement/statements;
                }
                ...(more for() statements can be used)
            }
Example:
            for(int i = 1;i <= 2;i++)
            {
                for(int j = 1;j < 4;j++)
                {
                    int c = i*j;
                    System.out.print(c);
                }
                System.out.println();
            }
Output:
123
246
Note: Remember that in case of nested loop, the inner most loop finishes first, then the loop just before that and finally the outermost loop i.e., the inner loop is dependent upon the outer loop.

2. The while() loop

The while() loop is also known as entry controlled loop, because before entering the loop first the condition is checked.
Definition: This loop is used to execute one or more than one statements till the condition is true and the loop terminates when the condition becomes false.
Syntax:
            while(test-condition)
            {
                statement;
                statement;
                ...
                .....
                update statement
            }



Example:
            int a = 0;
            while(a <= 5)
            {
                a++;
                System.out.println("A = " + a);
            }
            System.out.println("Out of while loop A = " + a);
Output:
A = 1
A = 2
A = 3
A = 4
A = 5
A = 6
Out of while loop A = 6
Working of the while() loop mentioned in the example:
In the above example the initial value of a is 0, the control when tries to enter into while loop, it finds condition is true, so while loop allows to enter into the loop, after that two statements a++ and System.out.println("A = " + a); executes till a <= 5 and as soon as a = 6, the condition becomes false and control comes out of while and runs the last statement that displays Out of the loop a = 6.

3. The do-while() loop

The do-while() loop is also known as exit controlled loop, because the control enters into the loop body without checking any condition.
Syntax:
            Starting values of the loop variable
            do
            {
                statement;
                statement;
                .... update statement
            }
            while(test-condition);

Example:
            int a = 0;
            do
            {
                a++;
                System.out.println("A = " + a);
            }
            while(a <= 4);
            System.out.println("Out of do-while loop A = " + a);
Output:
A = 1
A = 2
A = 3
A = 4
A = 5
Out of do-while loop A = 5
Working of the while() loop mentioned in the example:
In the above example the initial value of a is 0, as seen that the loop is begining with do and here no condition is present, so the control enters into the loop and two statements a++System.out.println("A = " + a); executes, then after the control gets while(), now here the condition (a <= 4) is checked and if it is true, the control repeats the loop and when the condition becomes false(a = 5), the loop terminates and runs the statement given after the while that displays Out of do-while loop A =  5.

3.1 Nested while() and Nested do-while() loops:

Nested while():

Definition: Presence of one or more than one while statements within a while statement is known as nested while loop.
Syntax:
            Starting value of the loop1 & loop2
            while(condition1)
            {
                statements;
                ...
                while(condition2)
                {
                    statement;
                    statement;
                    ...
                }
                ...
                statement;
            }

Nested do-while():

Definition: Presence of one or more than one do-while statements within a do-while statement is known as nested do-while loop.
Syntax:
            Starting value of the loop1 & loop2
            do
            {
                statements;
                ...
                do
                {
                    statement;
                    statement;
                    ...
                }
                while(condition2);
                ...
                statement;
            }
            while(condition1);

Understanding an infinite loop:

Definition: The loop in which the control does not reach to the test condition and the loop continues its working endlessly us known as infinite loop or unending loop.
When this situation arises, it means there is some logical error in the loop and immediately necessary action should be taken to make the test condition correct.
Example:
            for(int x = 1;x <= 10;x--)
            {
                System.out.println("X = " + x);
            }

IMPORTANT:

Every loop requires an initial value to work upon. So, remember that the initial value of the control variable in for(), while() and do-while() loops should be properly initialised like this: x = 0, y = 0, n = 6 etc...

So that it is for the Iterative Constructs in Java which includes for() loop, while() loop and do-while() loop. Hope you find it useful and if you have any query do comment in the comment box given below and I will try to solve your query.

Thank You.
Iterative Constructs in Java for(),while and do-while() Loops (Java) Iterative Constructs in Java for(),while and do-while() Loops (Java) Reviewed by Get2Know on September 09, 2020 Rating: 5

No comments:

Business

Powered by Blogger.