count from 2 to 10:
public class LoopTest1 {
public static void main(String[] args) {
// 1. initialize the loop variable
int count = 2;
// 2. test the loop variable
while (count <= 10) {
System.out.println(count);
// 3. change the loop variable
count++;
}
}
}
count by 2s:
public class LoopTest1 {
public static void main(String[] args) {
// 1. initialize the loop variable
int count = 1;
// 2. test the loop variable
while (count <= 5) {
System.out.println(count);
// 3. change the loop variable
count+=2;
}
}
}
count backwards:
public class LoopTest1 {
public static void main(String[] args) {
// 1. initialize the loop variable
int count = 5;
// 2. test the loop variable
while (count >= 1) {
System.out.println(count);
// 3. change the loop variable
count--;
}
}
}
Comments
Leave a comment