A loop (iteration) is a sequence of instructions that is continually repeated until a certain condition is reached. Loops are different in syntax but their basic functionality is same. Loops are beneficial when you’re going to reuse a certain code sequence several times.
There are three main types of loops in java-
for loop
while loop
do-while loop
for loop : A for loop is a repetition control structure that allows you to write a loop that needs to be executed a specific number of times.
A for loop is useful when you know how many times a task is to be repeated.
Syntax-
for ( Initialization; Condition; Increment/ Decrement) {
//code to be executed
}
Initialization- This step executes first and only only once at the beginning of the for loop.
Condition-If it's true the loop execute and the control jump to the Incrementation/ Decrementation
Incrementation/ Decrementation- Here we increment or decrement the loop counter value. this executes at the end of each iteration after the code block has been executed.
Note_ Do not forget to increase the variable used in the condition, otherwise the loop will never end!
Flowchart
![](https://static.wixstatic.com/media/493dbc_7bf7d0977db24fd4affc7bba294e0ff6~mv2.png/v1/fill/w_821,h_503,al_c,q_90,enc_auto/493dbc_7bf7d0977db24fd4affc7bba294e0ff6~mv2.png)
Example: The example below will print the numbers 1 to 6
![](https://static.wixstatic.com/media/493dbc_896ecee6bee547cda4afc2b0f07506ad~mv2.png/v1/fill/w_531,h_342,al_c,q_85,enc_auto/493dbc_896ecee6bee547cda4afc2b0f07506ad~mv2.png)
Output:
![](https://static.wixstatic.com/media/493dbc_cf70094c4dec4f5cbd29653c3e94d03e~mv2.png/v1/fill/w_378,h_252,al_c,q_85,enc_auto/493dbc_cf70094c4dec4f5cbd29653c3e94d03e~mv2.png)
while loop- When the number of iteration is not fixed we use while loop. it is called entry-controlled loop too. In while loop we only give condition. Initialization we do separately outside the loop. At last we do incrementation/ decrementation in the loop.
Syntax-
Initialization;
while ( Condition) {
//code to be executed
Incrementation/ Decrementation;
}
Flowchart
![](https://static.wixstatic.com/media/493dbc_05b21700f00346209b7a621647f2203d~mv2.png/v1/fill/w_795,h_493,al_c,q_90,enc_auto/493dbc_05b21700f00346209b7a621647f2203d~mv2.png)
Example: The example below will print the numbers 1 to 6
![](https://static.wixstatic.com/media/493dbc_1fd89c884fe046729e86629001c6adcd~mv2.png/v1/fill/w_540,h_343,al_c,q_85,enc_auto/493dbc_1fd89c884fe046729e86629001c6adcd~mv2.png)
Output:
![](https://static.wixstatic.com/media/493dbc_69542063fa104b218dc517497e998a5b~mv2.png/v1/fill/w_298,h_237,al_c,q_85,enc_auto/493dbc_69542063fa104b218dc517497e998a5b~mv2.png)
do/while loop- Do/while loop is a variant of while loop. In this loop code executes at least once without fail because it does not check the condition for the first time.
Syntax-
Initialization;
do {
//code to be executed
Incrementation/ Decrementation;
}
while (Condition) ;
Flowchart
![](https://static.wixstatic.com/media/493dbc_0c0c0b16b7c8414cb860fa6ec1de27a7~mv2.png/v1/fill/w_907,h_687,al_c,q_90,enc_auto/493dbc_0c0c0b16b7c8414cb860fa6ec1de27a7~mv2.png)
Example: The example below will print the numbers 1 to 6
![](https://static.wixstatic.com/media/493dbc_42cc88adafd348e9919e17db52cad81a~mv2.png/v1/fill/w_478,h_350,al_c,q_85,enc_auto/493dbc_42cc88adafd348e9919e17db52cad81a~mv2.png)
Output:
![](https://static.wixstatic.com/media/493dbc_b972bae731274ee9a20543ab7ee68842~mv2.png/v1/fill/w_355,h_197,al_c,q_85,enc_auto/493dbc_b972bae731274ee9a20543ab7ee68842~mv2.png)