☰ See All Chapters |
Java Control Statements
Iteration statements are also called as looping statements. By default all statements are executed sequentially in java program. Iteration statements are used to repeat the statements until specified condition is met.
In Java, we have the following looping statements:
while
do...while
for
for-each
while loop
while loop syntax
while(condition) { statement(s); } |
The statement(s) will be executed as long as condition evaluates to true. statement(s) may be a single statement or a block of statements.
The value of the variable involved in condition should be changing during every pass of the while loop. If it is not changed, then the while loop enters into an infinite loop.
do-while
do-while syntax
do { statement(s); } while (condition); |
statement(s) will be executed once. Then the condition will be evaluated, if it evaluates to true the do block will be executed again. This process repeats until the given condition becomes false. statement(s) may be a single statement or a block of statements
The value of the variable involved in condition should be changing during every pass of the while loop. If it is not changed, then the while loop enters into an infinite loop.
for loop
for loop syntax
for (initialization; boolean_expression; update_statement) { //body of the loop; } |
for statement has the following properties:
The two semicolons are required and create three sections: an initialization statement, a boolean expression, and an update statement.
The initialization step occurs once at the beginning of the loop.
The boolean_expression must evaluate to true or false.
The initialization and update_statement sections can contain multiple statements, separated by commas.
Any variables declared in the initialization step are local variables in for loop and go out of scope when the loop finishes.
Below is the flow of control in for loop
The initialization statement is executed first, and only once. This step allows you to declare and initialize any loop control variables.
Next, the condition (boolean_expression) is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after for loop.
After the body of for loop executes the flow of control jumps back up to the update_statement. This statement allows you to update any loop control variables.
The condition (boolean_expression) is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, for loop terminates.
for loop example
package com.java4coding;
public class HelloWorld { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println("The number is " + i); } } } |
The Enhanced for Statement (for-each)
foreach statement syntax
for (datatype iterator: collection) { //body of the loop } |
Java 5.0 introduced a new looping control structure called the enhanced for statement, also referred to as for - each loop.
An enhanced for statement is designed for iterating through arrays and collections.
An enhanced for statement has the following properties:
The data type of the iterator must be compatible with the data type of the collection.
The scope of the iterator is the body of the loop.
The number of iterations of the loop equals the size of the collection. If the collection is empty, the body of the loop does not execute. Iterator receives the elements from a collection, one at a time, from beginning to end. The collection being cycled through is specified by collection.
The collection must be an array or an object of type java.lang.Iterable , an interface introduced in Java 5.0 exclusively for for-each loops
There is one important point to understand about the for-each style loop. Its iteration variable is “read-only” as it relates to the underlying array. An assignment to the iteration variable has no effect on the underlying array. In other words, you can’t change the contents of the array by assigning the iteration variable a new value.
You must declare the iterator within the enhanced for statement; it cannot be a variable that is already declared.
foreach statement example
package com.java4coding;
public class HelloWorld { public static void main(String[] args) { String names[] = { "Manu", "Advith", "Likitha" }; for (String name : names) { System.out.println("Name is " + name); } } } |
All Chapters