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 repeatedly to execute a set of statements until a particular condition is satisfied.
  • The for loop allows a set of instructions to be executed until a certain condition is met. The position can be pre-defined or open-ended.
  • There is an iteration control structure for loops that allows you to efficiently write a loop that needs to execute a specific number.

Syntax

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

Explanation:

  • There are three expressions for the statement that are separated by a semicolon. The following verbs are to be performed in three expressions.
  • The initialization 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 desired iterations or determines when to exit the loop.
  • The execution of the loop continues until the conditional test is satisfied. When the condition becomes false, the control of the program exits the body of the loop and executes the next statement after the body of the loop.
  • The revalidation parameter decides how to make changes to the loop (very frequent increments or increment operations are to be used). The body of a loop can contain a single statement or multiple statements.
  • In case there is only one statement after the loop, braces may not be necessary. In such a case, only one statement is executed until the condition is satisfied.

Flow Diagram

For loop

While Loop

  • Loops are used repeatedly to execute a set of statements until a particular condition is satisfied.
  • While looping, the condition is evaluated first and if it is true then the inside statement while executing the loop.
  • When the condition becomes false, the control exits the loop and jumps to the next statement after the loop.
  • The loop in Java programming repeatedly executes a target statement as long as a given condition is true.
  • This is called a loop because the control keeps looping back until the start of the statement until the test goes wrong.
  • 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 can be any expression. Loop statements will be executed until the condition is true, i.e. the test state is evaluated and if the condition is true, the body of the loop is executed.
  • When the condition returns false, the execution will be out of the loop.

Syntax

while(condition)
{
statement(s);
}

Flow Diagram

While loop

Do-While Loop

  • In the do-time, statements are executed inside the loop and then the condition is evaluated.
  • If the condition returns true, the control is moved to "do", otherwise it is doo-jump after the next statement.
  • The last of the ends of C is do-as. Unlike the For and the Loops, in which the position at the top of the loop is tested, the do-while loop checks its position at the bottom of the loop.
  • This means that a one-time loop will always execute at least once.
  • Unlike loops and while loops, which test the position of the loop at the top of the loop, do ... while in Java programming the loop checks its position 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;