Control Flow

For Loop in Java

The for loop is a control structure that executes a block of code a specified number of times.


Syntax
for(initialization; booleanExpression; update) {
    //statements
} //end for loop

Notes

Initialization declares the initial value of the controlling variable. The controlling variable can be defined in-line with the for loop, or defined previously. Typically, the for loop uses an integer variable.

booleanExpression is the controlling statement that must be true in order for the 'for' structure to loop and execute. booleanExpression must result in either a true or false output. It is created using comparing operators (==, >, =, <=, !=). There can also be multiple boolean expressions within the parentheses (booleanExpression). The boolean expressions are connected through logical operators (&&, ||, !).

Update is the change in the controlling variable that occurs after each execution of the statements. It performs an arithmetic operation on the controlling variable.


Example
for(int x = 0; x <10; x++) { 
    System.out.println(x); 
} //x will continue to increment while it remains less than 10.

Add to Slack

< Ternary Operator   |   For Each >

© 2019 SyntaxDB. All Rights Reserved.