Java Beginners - Java For Beginners Java While & do while loop Rating: 0.0/5 (0 votes cast) | Level : Beginners Author : Arunkumar S Download Source : Not Avaliable
Java While & do while loopWhile - As was introduced in Java Control Flow Statements, Java has the ability to loop repeatedly until some terminating condition is met. During each iteration of the loop, the same block of statements is executed. When the terminating condition evaluates to false, the loop ends and execution continues normally at the statement following the loop's closing curly brace. There are three looping constructs in Java: while, do-while, and for. Every looping situation in Java can be handled with a while loop. Why does Java have a do-while and for loop if everything can be done with while? Some looping constructs are more cleanly and concisely written as do-while or for loops. In this tutorial we will look at while and do-while. We will take up the for loop in a later tutorial. Before we look at examples of the while statement, let's take a look at its general form: while(conditional_expression) { statements_to_execute } This general form shows a simple while structure. As long as conditional_expression evaluates to true at the beginning of each loop, then statements_to_execute will continue to be executed. Notice that statements_to_execute is wrapped in curly braces {}. If statements_to_execute consists of a single statement only, the curly braces are optional. Most Java programmers consider it to be good coding style to always include the curly braces. Warning: Unless you want a loop to continue forever, make sure that conditional_expression eventually evaluates to false in order to end the loop. Let us look at some examples of while: //Example 1 while(true) { System.out.println("infinite loop"); }
//Example 2 int counter = 0; while(counter < 5) { System.out.println("iteration: " + counter); counter++; }
//Example 3 String args[] = {"red", "blue", "green"}; int counter = 0; while(counter < args.length) { System.out.println("Element " + counter + " is " + args[counter]); counter++; }
- In example 1 The while will loop forever. There are times when this is useful but in this case it just repeatedly prints out "infinite loop" until the JVM throws an exception.
- Example 2 shows a typical while loop. In this instance, an int variable named counter is initialized to 0. Next comes the while keyword and the conditional expression (counter < 5). The first time the conditional is tested counter is 0, so the code block is executed, printing out "iteration: 0". Then counter is incremented by 1 with the counter++ statement. The conditional expression is evaluated again at the beginning of the second loop. This time counter equals 1 (still less than 5) so the program prints out "iteration: 1" and counter is incremented to 2. This continues three more times until counter is incremented to 5. At this point the conditional expression is false, because 5 is not less than 5. The loop breaks and execution continues after the closing curly brace "}"
- Example 3 demonstrates another common use of loops--iterating over an array or collection and performing some action on each of the elements in the array or collection. Arrays and collections are just containers that can hold many variables (called elements). We will see more details on arrays and collections in upcoming tutorials. In this example an array of String objects is initialized with the values red, blue and green. These Strings are the array's elements. Next an int counter variable is initialized to 0. Then the while loop evaluates whether counter is less than the number of elements in the array (counter < args.length). At the beginning of the first loop counter is 0 and the length of the array is 3, so the code block executes. At the end of the third loop, counter is incremented to 3. Since 3 is not less than 3, the loop exits.
Zero-based Counting You may have noticed that all of my counter variables were initialized to 0 instead of 1. If I inserted a System.out.println("iteration: " + counter) into the code block, it would display "iteration: 0" for the first first pass through the loop, "iteration: 1" for the second pass, and so on. Java uses zero-based indexes for identifying the elements in arrays and collections. This means that the first element is 0, the second element is 1, and so on. Because looping over arrays and collections is the most common reason for using a while statement, it is more convenient to initialize the counter to match the first element in the array or collection. 1 | 2 | Next >
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|