Home >Java >javaTutorial >control flow statemet:

control flow statemet:

Linda Hamilton
Linda HamiltonOriginal
2025-01-29 16:14:13686browse

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) {

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;
            }

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;

}

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;
        }

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;

    }

}   

}

The above is the detailed content of control flow statemet:. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn