DEV Community

Harish R
Harish R

Posted on

control flow statemet:

package controlflowmethod;

what is while?

In Java, the while loop is used to execute a block of code repeatedly as long as a given condition is true.

public class While {

public static void main(String[] args) {
Enter fullscreen mode Exit fullscreen mode

task 1

  • the i value is 0,
  • the condition is (i<5),
  • print statement(your choise),
  • loop by i=i+1
                int i=0;
        while(i<5) { 
            System.out.println("1");  //ANS = 1 1 1 1
                i=i+1;
            }
Enter fullscreen mode Exit fullscreen mode

task 2

  • the i value is 5,
  • the condition is (i>=1),
  • print statement(your choise),
  • loop by i=i-1

    int i=5;
    while(i>=1) {
        System.out.println(i);   // ANS = 5 4 3 2 1
        i=i-1;

Enter fullscreen mode Exit fullscreen mode
}
Enter fullscreen mode Exit fullscreen mode

task 3

  • the i value is 1,
  • the condition is (i<=10),
  • print statement(your choise),
  • loop by i=i+2
                int i=1;
        while(i<=10) {
            System.out.println(i);   // ANS = 1 3 5 7 9
            i=i+2;
        }
Enter fullscreen mode Exit fullscreen mode

task 4

  • the i value is 0,
  • the condition is (i<10),
  • print statement(your choise),
  • loop by i=i+2.
                int i=0;
        while(i<10) {
            System.out.println(i);   // ANS = 0 2 4 6 8
            i=i+2;

Enter fullscreen mode Exit fullscreen mode
    }

}   
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more