Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, April 9, 2019

Java While Loop - While loop Example Programs, Nested While Loop Examples

While Loop In Java:

In this tutorial, we will discuss about while loop, its syntax, example, how to make infinite loop.

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.

Java While loop is an iterative loop and used to execute set of statements for a specified number of times.


Java While Loop - While loop Example Programs, Nested While Loop Examples


We will see now

1. While loop syntax
2. While flowchart
3. Infinite while loop
4. Nested while loop

Syntax:


while ( boolean_expression )
{
    // some statements
}


Flow Chart:

Java While Loop


While Loop Example:



package com.adeepdrive.whileDemo.statement;
public class WhileExample {
    public static void main(String[] args) {
        int z = 1;
        while (z <= 10) {
            System.out.println("z value : " + z);
            z++; // increment operator
       }
    }
}

The above while loop runs for 10 times until z values equals to 10. After that z value becomes 11, while condition (11 <= 10) returns false.


Output:


z value : 1
z value : 2
z value : 3
z value : 4
z value : 5
z value : 6
z value : 7
z value : 8
z value : 9
z value : 10


Java Infinite While Loop Example:


To make while loop infinite,

1. Just remove the increment operation from loop.
2. Pass true to while loop,.



Syntax:

while( true ){
    // Statements to be executed.
}


Example 1:

package com.adeepdrive.whileDemo.statement;
public class InfinateWhileLoop {
    public static void main(String[] args) {
        while (true) {
            System.out.println("infinate loop example 1");
        }
    }
}


Output:

infinate loop example 1
infinate loop example 1
infinate loop example 1
infinate loop example 1
infinate loop example 1


Example 2:



public class InfinateWhileLoop2 {
    public static void main(String[] args) {
    int i = 1;
        while (i == 1) {
            System.out.println("infinate loop example 2");
        }
    }
}


Output:

infinate loop example 2
infinate loop example 2
infinate loop example 2
infinate loop example 2
infinate loop example 2


Nested While Loop:

Writing while loop inside another while loop.

while ( boolean_expression 1)
{
    // statement 1
    while (boolean_expression 2)
    {   
        // Second while loop statements.
    }
}

Nested While Loop Example:



public class NesredWhileLoop 
{
public static void main(String[] args) 
 {
  int i=1, j=0;
  while(i<5)
  {
   j = 0;
   while(j<i)
   {
    System.out.print(" "+i+" ");
    j++;
   }
   System.out.println();
   i++;
  }
 }
}

Output:

 1
 2  2
 3  3  3
 4  4  4  4

Prints numbers from 1 to 4 in triangle shape.

No comments:

Post a Comment

Please do not add any spam links in the comments section.