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;
}
}
}
Top comments (0)