Looping Control Statement in Java

0
Share

What are the Looping Control Statement ?

Repetition a portion program according to user requirement is known as Looping. In java there are 3 different iterative control statement are used for repetition of the program such as –

For Loop

Syntax
for(initilization; Test expression; Update expression){
    Statement...;
} 

Where 
initialisation means initial the value.
Test expression means to make condition.
Update expression means increment/decrement.
  • For loop is an entry control loop. In case for loop the keyword ‘for’ contain 3 different expression like -initilization ,test expression , and update expression .
  • First initialisation expression execute & the control transfer to the test expression.
  • If test expression will be satisfied / True the body statement loop execute.
  • After execute the body, control transfer to the update expression and increment or decrement according to the logic , again control back to the test expression and check that.
  • This process will be continue till the condition satisfied, when the condition became false control can transfer out of the body part and print rest of the part.
  • It is called pre-tested loop (That means 1st test the condition then repeat the process.).

While Loop

Syntax
initilization ;
While(Condition)
{
    Statement ... ;
    Update expression;
}
  • While loop is also an entry control loop.
  • In this loop the keyword ‘while’ contain a certain condition & execute first.
  • If the condition is satisfied then the body of the loop will be execute & control back to the while statement again, this process will continue till the condition is true.
  • When condition becomes false, the repetition will stop & control transfer to the next part of the program.

Do.. While Loop

Syntax
initilization ;
do
{
    Statement ... ;
    Update expression;
}
While(Condition)
  • It is an exist control loop.
  • In this loop the keyword ‘do’ contain the body of the loop & execute first.
  • After execute the body control goes to the while condition to checking the condition, if the condition is true / satisfied again control back to the ‘do’ statement & this process still continue to the condition true.
  • When condition becomes false, the repetition will stop & control transfer to the next part of the program.
  • In do.. while loop the body of loop must be execute once even the condition is false, because it is the post- executed or exist control loop.

When we working on the loop certain condition happened according to the user requirement we also used 2 keywords named as ‘break’ & ‘continue’ keyword to perform the operation.

Break Statement

  • The break statement is used to exist from loop & exist from a switch block, but when the break is used to exist from a loop, it must be associated with ‘if statement’.
  • Otherwise it will display a error “miss place of break”.
  • when break is used to exist of a switch block there is no need of ‘if condition’.
Example
public class breakTest{
    public static void main(String arg[]){
        int i;
        for (i=1; i<=100; i++){
            System.out.println(i);
            if(i==20){
                break;
            }
        }
    }
}

Continue Statement

  • Continue basically used to bypass the control to the next iteration or the update expression without executing the rest part of the body.
  • It is also associated with ‘if condition ‘.
Example
public class continueTest{
    public static void main(String arg[]){
        int i;
        for (i=1; i<=500; i++){
            if (i>=100 && i<=149){
                continue;
            }
            System.out.println(i);
        }
    }
}

To see more article click below given link –

  • What are the List of CSS3 New Features ?
    CSS3 is the latest version of the normal CSS . CSS3 adds several new styling features and improvements to enhance the web presentation capabilities. Text Effect in...
  • CSS Basic Property
    What is CSS ? CSS stands for Cascading Style Sheets. CSS basically used in designing the web tags & pages. CSS is the most popular used...
  • Java Program to swap two number use 3rd variable
    In this program, you’ll learn to Program to swap 2 number with 3rd variable in Java. After write the code, the final output is displayed on...
  • Java program to multiply two floating point number
    In this program, you’ll learn to Program to multiply two floating point number in Java. After write the code, the final output is displayed on the...
  • Java Program to find the ASCII value of a character
    In this program, you’ll learn to Program to find the ASCII value of a character in Java. After write the code, the final output is displayed...
  • Java Program to calculate quotient & reminder
    In this program, you’ll learn to Given Number is even or odd in Java. After write the code, the final output is displayed on the screen....
%d bloggers like this: