java tutorial - Java for loop - java programming - learn java - java basics - java for beginners
Learn Java - Java tutorial - Java for loop - Java examples - Java programs
- The for loop - has a repetition control structure that allows you to effectively write a loop that must be executed a certain number of times.
- The for loop is useful when you know how many times a task needs to be repeated.
Syntax
The for loop syntax in Java:
The control process in the cycle:
- The initialization phase is performed first, and only once. This step allows you to declare and initialize any variables to control the loop, and it ends with a semicolon (;).
- Next is a logical expression. If true, the loop body is executed if it is false, the loop body is not executed, and control passes to the next statement past the loop.
- After the for loop is executed, the control goes back to the update operator . It allows you to update any variables for loop control, and is written without a semicolon at the end.
- The logical expression is now evaluated again. If it is true, the loop is executed and the process is repeated. If it is false, then the for loop terminates.
Process description
Learn java - java tutorial - for-loop - java examples - java programs
Sample Code
The following is an example of the for loop code in Java:
The following result will be obtained: