Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - While Statement



The syntax of the while statement is shown below −

while(condition) { 
   statement #1 
   statement #2 
   ... 
}

The while statement is executed by first evaluating the condition expression (a Boolean value), and if the result is true, then the statements in the while loop are executed. The process is repeated starting from the evaluation of the condition in the while statement. This loop continues until the condition evaluates to false. When the condition becomes false, the loop terminates. The program logic then continues with the statement immediately following the while statement. The following diagram shows the diagrammatic explanation of this loop.

While Loop

Example - Usage of while loop

Following is an example of a while loop statement −

class Example {
   static void main(String[] args) {
      int count = 0;
		
      while(count<5) {
         println(count);
         count++;
      }
   }
}

In the above example, we are first initializing the value of a count integer variable to 0. Then our condition in the while loop is that we are evaluating the condition of the expression to be that count should be less than 5. Till the value of count is less than 5, we will print the value of count and then increment the value of count. The output of the above code would be −

0 
1 
2 
3 
4

Example - Printing Elements of a list Using while Loop

In this example, we're showing the use of a while loop to print contents of an list. Here we're creating an array of integers as numbers and initialized it some values. We've created a variable named index to represent index of the array while iterating it. In while loop we're checking the index to be less than size of the array and printed the element of the array using index notation. index variable is incremented by 1 and loop continues till index becomes the size of the array and loop exits.

class Example {
   static void main(String[] args) {
      def numbers = [10, 20, 30, 40, 50];
      def index = 0;

      while( index < 5 ) {
         print("value of item : " + numbers[index] );
         index++;
         print("\n");
      }
   }
}

Output

value of item : 10
value of item : 20
value of item : 30
value of item : 40
value of item : 50

Infinite While Loop

You can implement an infinite while loop using the while loop statement by providing "true" as the test condition.

Example: Implementing Nested while Loop

In this example, we're showing the infinite loop using while loop. It will keep printing the numbers until you press ctrl+c to terminate the program.

class Example {

   static void main(String[] args) {
      int x = 10;

      while( true ) {
         print("value of x : " + x );
         x++;
         print("\n");
      }
   }
}

Output

value of item : 10
value of item : 20
value of item : 30
value of item : 40
value of item : 50
...
ctrl+c
Advertisements