A blog for computer science passionates.

Monday 20 April 2020

Java : Iterative Statements

Hello Friends!!!
Welcome to passionforcs,

In this article, we are going to learn Iterative statements in Java. Iterative statements are generally known as looping statements. Looping statements are used to create a chain to perform the same task. such as if we want to print PassionForCS for five times, we can print the same sentence simply by using System.out.println() for five times or use iterative statements.

Basically, there are three different looping statements:
  • for
  • while
  • do-while
Here for and while both statements are entry control loop and do-while loop is exit control loop.

What is the entry control loop?

The entry control loop means when the control enters into the loop it checks for the condition whether the condition is true or false if the condition is true the control enters into the loop.

What is the exit control loop?

The exit control loop means when the control exits from the loop it checks for the condition whether the condition is true or false if the condition is true the control enters into the loop second time. while working with the exit control loop, it executes once without checking the condition.

Let's explore more, how do entry control and exit control loop works?
Flowchart of Entry Control & Exit Control Loop

Now we are going to learn for, while & do-while loop with syntax:

for loop: Generally, this loop is used when we know about the number of iterations. In this loop we have initialization
i.e. in a single statement.
Syntax:
for(initialization;condition;increment/decrement operation){
    //code block
}

Example,

while loop: Generally, this loop is used when we are unaware with the number of iterations. In while loop we have to initialize a variable before it checks for the condition in while loop and increment/decrement operation is done in side the loop.
Syntax:
while(condition){
    //code block
    //increment/decrement operation
}

Example,


Here I use the same example to give you an illustration that how for and while loop is different and how do their syntax work?

do-while loop: this loop is an exit control loop so when we don't know the number of iterations but we want to execute the loop at least once at that time we can use do-while loop. when we are working with do-while loop we have to initialize a variable before it is used and then it increase or decrease its' value and then checks for the condition. If the condition is true loop will be executed.
Syntax:
do{
    //code block
    //increment/decrement operation
}while(condition);

Example,

One more looping statement is there in Java programming is known as for-each loop and it especially designed and work for arrays and collections. It is introduced after the 5th version of Java (J2SE 5.0).
Syntax:
for(<dataType> varNm : <Array>|<Any Collection>)
{
    //code block
}

Example,

You can see here in the syntax we use only for in this loop, we can say that it is an extended version of for loop. in this loop there is no need for increment/decrement operations. this loop iterates for each element of collection or an array.
We will explore more examples of different looping statements.. till then that's it and in this pandemic,  learn new things but Stay Home, Stay Safe and Be Happy. :)




No comments:

Post a Comment