ANDROID & JAVA - Java Loops - For, While and Do... While PDF
ANDROID & JAVA - Java Loops - For, While and Do... While PDF
ANDROID & JAVA - Java Loops - For, While and Do... While PDF
Home
while(Boolean_expression)
{
//Statements
}
When executing, if theboolean_expressionresult is true then the actions inside the loop will be executed. This will continue as long as
the expression result is true.
Here key point of thewhileloop is that the loop might not ever run. When the expression is tested and the result is false, the loop body
will be skipped and the first statement after the while loop will be executed.
Example:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
do
{
//Statements
}while(Boolean_expression);
Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is
tested.
If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process
repeats until the Boolean expression is false.
Example:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
3. After the body of the for loop executes, the flow of control jumps back up to the update statement. This statement allows you
to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the Boolean
expression.
4. The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop,
then update step,then Boolean expression). After the Boolean expression is false, the for loop terminates.
Example:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
for(declaration : expression)
{
//Statements
}
Declaration . The newly declared block variable, which is of a type compatible with the elements of the array you are
accessing. The variable will be available within the for block and its value would be the same as the current array element.
Expression. This evaluate to the array you need to loop through. The expression can be an array variable or method call
that returns an array.
Example:
}
}
}
This would produce following result:
10,20,30,40,50,
James,Larry,Tom,Lacy,
break;
Example:
10
20
continue;
Example:
}
System.out.print( x );
System.out.print("\n");
}
}
}
This would produce following result:
10
20
40
50
Top comments
Michael Furhoff
Shared publicly
2 years ago
Thanks
Home
kirankumar Rajendran
Follow
87