3.3.1 Using while Loop in Java

The syntax of a while loop is

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


The block of statements is repeatedly executed while the boolean expression evaluates to true. As an example, we can rewrite the OutputArray program of Section 2.5.6 and assign values to array elements intArray[0] and intArray[1] using a while loop.

OutputArray
public class OutputArray
{

public static void main(String[] args)
{
int i=0;
int intArray [];
int Array=new int [2];
while( i< intarray.length)
{
intarray[i] = i+1;
i++;
}
System.out.println(''Values of intArray are " +
intArray[0] + " and " + intArray[1]);
}
}

No comments:

Post a Comment