Home > Java > javaTutorial > control flow statemet:

control flow statemet:

Linda Hamilton
Release: 2025-01-29 16:14:13
Original
611 people have browsed it

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) {
Copy after login

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;
            }
Copy after login

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;

Copy after login
}
Copy after login

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;
        }
Copy after login

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;

Copy after login
    }

}   
Copy after login

}

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

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template