Using do while Loop in Java

The syntax of a do while loop is

do {
one or more statements;
}
while (boolean expression);


Unlike a while loop, a do while loop is guaranteed to execute at least once. In the OutputArray example of the previous section, the while loop (lines 8–11) can be replaced by the following do while loop:

OutputArray
public class OutputArray
{

public static void main(String[] args)
{
int i=0;
int intArray [];
int Array=new int [2];

do {
intArray[i] = i + 1;
i++;
} while (i < intArray.length);
}
}

No comments:

Post a Comment