With in this lesson you'll learn how "while loops" allow to continually execute statements while a particular condition is "true" check out the while statement below. The loop is executed as long as "i" is less than or equal to 10.
in each loop repition the statement in the body first print "i" and then increase it by 1. Guess what the program prints? Then the program prints the numbers from 0 to 10. Now make the program print the numbers from 0 to 10. You can Also change the initial value of "i" 20 to print the number starting from 20 to 30 Look at the codes above. Great! you should see it prints number 10 to 30, Now make the program print the numbers from 30 down to 1 see the above codes for your reference. in this case the variable "i" is initialize with 30 Then decrease in the (while-loop) while it is greater than or equal to 1 Using the(--) operator the print Result should be now from the number 30 down to 1 use a while loop to calculate the sum of all numbers from 1 to 2000. Make it print only the sum not the intermediate values ok? so you will be acquainted how to use (while-loop) when you make a game soon.public class Main{
public static void main(String [] args){
int i = 0;
while(i<10){
System.out.println(i); //while loop for incrementation
i++;
}
while(i<30){
System.out.println(i);//while loop for incrementation
i++;
}
int i = 30;
while(i>=1){
System.out.println(i);//while loop for decrementation
i--;
}
}
}
0 Comments