Conditional control statement type two in java

Looping statements:

  • It is used to execute continually until the condition becomes false.
Types:
  • While
  • Do while
  • For
1. While:
  • It first checks the condition then executes the result until becomes false.
Syntax:

 while (condition)
{
          Statements;
}

Example program for while condition:

package exampleprogram;

public class Looping_program {

    public static void main(String[] args) {

        int i = 1, sum = 0;

        while (i <= 5) {

            sum = sum + i;

            i++;

        }

        System.out.println("\n The sum of numbers upto 5 is : " + sum);

    }

}

Output:
2. do while:
  • It is first to perform some operation after that check the conditions.
  • It's opposite to the while condition.
Syntax:

do
{
          Statements;
}
while (condition);

Example program for do while condition:

package exampleprogram;
public class Looping_program {
    public static void main(String[] args) {
        int i = 1, sum = 0;

        do {
            sum = sum + i;
            i++;
        } while (i <= 5);
        System.out.println("\n The sum of numbers upto 5 is : " + sum);
    }
}

Output:
3. for loop:
  • It is used to execute a set of instructions repeatedly until the condition becomes false.
Syntax:

 for(initialization ;condition ;increment or decrement)
{
          Statement;
}

Example program for for loop:

package exampleprogram;

public class Looping_program {

    public static void main(String[] args) {

        int i;

        for (i = 1; i <= 10; i++) {

            System.out.println(i);

        }

    }

}

Output:
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me