loops in Java with example

For loop:

In Java we have three types of basic loops: for, while & do-while. In this article we are going to discuss about for loop. We will cover following topics in this article: a) What is for loop b) syntax of for loop c) example of for loop d) for loop flow diagram e) infinite for loop.

What is For loop?

It executes a block of statements repeatedly until the specified condition returns false.

Syntax of for loop:

for (initialization; condition; increment/decrement) {
    statement(s) //block of statements
}

Mind the semicolon (;) after initialization and condition in the above syntax.

Initialization expression executes only once during the beginning of loop
Condition(Boolean Expression) gets evaluated each time the loop iterates. Loop executes the block of statement repeatedly until this condition returns false.
Increment/Decrement It executes after each iteration of loop.

Confused??
Don’t worry let see an example to understand it better.

For loop example:

 

class ForLoopExample {
    public static void main(String args[]){
         for(int i=10; i>1; i--){
              System.out.println("The value of i is: "+i);
         }
    }
}

The output of this program is:

The value of i is: 10
The value of i is: 9
The value of i is: 8
The value of i is: 7
The value of i is: 6
The value of i is: 5
The value of i is: 4
The value of i is: 3
The value of i is: 2

In the above program:
int i=1 is initialization expression
i>1 is condition(Boolean expression)
i– Decrement operation

Infinite for loop: The importance of Boolean expression and increment/decrement operation co-ordination:

 

class ForLoopExample2 {
    public static void main(String args[]){
         for(int i=1; i>1; i++){
              System.out.println("The value of i is: "+i);
         }
    }
}

This is an infinite loop as the condition would never return false. The initialization step is setting up the value of variable i to 1, since we are incrementing the value of i, it would always be greater than 1 (the Boolean expression: i>1) so it would never return false. This would eventually lead to the infinite loop condition. Thus it is important to see the co-ordination among Boolean expression and increment/decrement to determine whether the loop would terminate at some point of time or not.

Here is another example of infinite for loop:

 

// infinite loop
for ( ; ; ) {
// statement(s)
}
//For loop example to iterate an array:

//Here we are iterating and displaying array elements using the for loop.

class ForLoopExample3 {
public static void main(String args[]){
int arr[4]={2,11,45,9};
//i starts with 0 as array index starts with 0 too
for(int i=0; i<4; i++){
System.out.println(arr[i]);
}
}
}
[/code]

Output:

2
11
45
9

While loop:

In the last post we discussed about for loop. In this tutorial we are going to discuss about while loop with the help of examples and flow diagrams. We will cover following topics in this article: a) Introduction to while loop b) Syntax of while loop c) Flow diagram of while loop d) While loop example e) infinite while loop.

What is while loop?
Like for loop, It also executes a block of statements repeatedly until the condition(Boolean expression) returns false.

Syntax of while loop

while (Boolean expression) {
    statement(s) //block of statements
}

The logic in while loop is simple it executes the block of statements when the Boolean expression returns true. It gets terminated when the Boolean expression returns false.

Are you confused??
Don’t worry we will see the flow diagrams and example. That would make you comfortable.

While loop example

class WhileLoopExample {
    public static void main(String args[]){
         int i=10
         while(i>1){
              System.out.println(i);
              i--;
         }
    }
}

The output of this program is:
10
9
8
7
6
5
4
3
2

Did you understand how we get that output?
If not refer the below flow diagram.

 

Infinite while loop

class WhileLoopExample2 {
    public static void main(String args[]){
         int i=10
         while(i>1)
         {
             System.out.println(i);
              i++;
         }
    }
}

This loop would never end, its an infinite while loop. This is because condition is i>1 which would always be true as we are incrementing the value of i inside while loop.

Here is another example of infinite while loop:

while (true){
    statement(s);
}

While loop example to iterate an array

Here we are iterating and displaying array elements using while loop.


class WhileLoopExample3 {
public static void main(String args[]){
int arr[4]={2,11,45,9};
//i starts with 0 as array index starts with 0 too
int i=0;
while(i&lt;4){
System.out.println(arr[i]);
i++;
}
}
}

Output:

2
11
45
9

do-while loop in Java

n this tutorial we are going to discuss about do-while loop with the help of examples and flow diagrams. We will cover following topics in this article: a) Introduction to do-while loop b) Syntax of do-while loop c) do-while loop example

Introduction to do-while loop

do-while loop is similar to while loop, however there is a single difference between these two. Unlike while loop, do-while guarantees at-least one execution of block of statements. This happens because the do-while loop evaluates the boolean expression at the end of the loop’s body. Therefore the set of statements gets executed at-least once before the check of boolean expression.

Syntax of do while loop:

class DoWhileLoopExample {
    public static void main(String args[]){
         int i=10;
         do{
              System.out.println(i);
              i--;
         }while(i>1);
    }
}

The output of this program is:
10
9
8
7
6
5
4
3
2

do-while loop example program to iterate an array

Here we are iterating and displaying array elements using do-while loop.

class DoWhileLoopExample2 {
public static void main(String args[]){
int arr[4]={2,11,45,9};
//i starts with 0 as array index starts with 0 too
int i=0;
do{
System.out.println(arr[i]);
i++;
}while(i<4);
}
}
[/code]

Output:
2
11
45
9