Types of loops in java with example

Looping is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true.

There are 3 type of basic loop-

  1. For Loop
  2. While Loop
  3. Do-While Loop

For Loop

  • Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.
  • the for loop allows to execute a set of instructions until a certain condition is satisfied. Condition may be pre-defined or open-ended.
  • A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax

for(initialization;condition;increment/decrement)
{
statement(s);
}

Explanation:

  • The for statement contains three expressions which are separated by semicolons. Following actions are to be performed in three expressions.
  • The initialize counter sets a loop to an initial value. This statement is executed only once.
  • The test condition is a relational expression that determines the number of iterations desired or it determines when to exit from the loop.
  • The for loop continues to execute as long as conditional test is satisfied. When the condition becomes false, the control of the program exits from the body of the for loop and executes next statement after the body of the loop.
  • The re-evaluation parameter decides how to make changes in the loop (quite often increment or decrement operations are to be used). The body of the loop may contain either a single statement or multiple statements.
  • In case there is only one statement after the for loop, braces may not be necessary. In such a case, the only one statement is executed till the condition is satisfied.

Flow Diagram

For loop

While Loop

  • Loops are used to execute a set of statements repeatedly until a particular condition is satisfied.
  • In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute.
  • When condition returns false, the control comes out of loop and jumps to the next statement after while loop.
  • A while loop in java programming repeatedly executes a target statement as long as a given condition is true.
  • Its called a loop because control keeps looping back to the start of the statement until the test becomes false.
  • The test condition is indicated at the top and it tests the value of the expression before processing the body of the loop.
  • The test condition may be any expression. The loop statements will be executed till the condition is true, i.e. the test condition is evaluated and if the condition is true, then the body of the loop is executed.
  • When the condition becomes false, the execution will be out of the loop.

Syntax

while(condition)
{
statement(s);
}

Flow Diagram

While loop

Do-While Loop

  • In Do-while,the statements inside loop execute and then the condition gets evaluated.
  • If the condition returns true then the control gets transferred to the “do” else it jumps to the next statement after do-while.
  • The last of C’s loops is the do-while. Unlike the for and the while loops, in which the condition is tested at the top of the loop, the do-while loop checks its condition at the bottom of the loop.
  • This means that a do-while loop will always execute at least once.
  • Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in java programming checks its condition at the bottom of the loop.

Syntax

do{
statement(s);
}
while(condition);

Flow Diagram

While loop

Loop Control Statement

  • Loop control statements change execution from its normal sequence.
  • When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

There are 2 type of Loop Control Statement-

  1. break
  2. continue

break

  • Sometimes,it is needed to stop a loop suddenly when condition is satisfief.This can be done with the help of break statement.
  • A break statement is used for unusual termination of a block.
  • The keyword break allows the programmers to terminate the loop. The break skips from the loop or block in which it is defined.
  • The control then automatically goes to the first statement after the loop or block. The break can be associated with all conditional statements.
  • We can also use break statements in the nested loops. If we use break statement in the innermost loop, then the control of the program is terminated only from the innermost loop.
  • The break statement causes an immediate exit from a loop. Because the code that follows it in the loop is not executed if the break is reached, you can also sometimes avoid nesting by including a break.
  • It is possible to force an immediate exit from a loop, bypassing the loop’s conditional test, by using the break statement.

Syntax

break;

Flow Diagram

 break statement

continue

  • The statement continue,is just opposite of break statement.
  • As soon as the continue statement is executed in a loop,the control skips rest of the statements for that value and resumes for the next iteration.
  • The continue statement is used inside loop. When a continue statement is encountered inside a loop, control jumps to the beginning of the loop for next iteration, skipping the execution of statements inside the body of loop for the current iteration.
  • The continue statement causes an immediate jump to the top of a loop.
  • The continue statement brings back program control to the start of the loop. You can use it for both ‘for’ and ‘while’ loops.
  • The continue statement is exactly opposite to break. The continue statement is used for continuing the next iteration of loop statements.
  • When it occurs in the loop, it dose not terminate, but it skips current iteration and control is transferred to the next iteration.

Syntax

continue;