|
Loop Practice Exercises
What do the loops below do?
Loop 1
for(int i = 0; i < 20; i++){
if(i % 2 == 0){
System.out.println(i*2);
}
}
Loop 2
for(int i = 20; i > 0 ; i-- ){
if(i % 2 == 0){
System.out.println(i);
}
Loop 3
int mysteryInt = 100;
for(int i = 5; i > 0;i-- ){
mysteryInt -= i;
System.out.println(mysteryInt)
}
Loop 4
for(int i = 5; i > 0;i-- ){
int mysteryInt = 100;
mysteryInt -= i;
System.out.println(mysteryInt);
}
Loop 5: a while loop
int mysteryInt = 1;
int counter=1;
while(mysteryInt < 3 ) {
mysteryInt = mysteryInt* counter;
counter++;
}
System.out.println(mysteryInt);
Rewrite loop s 1—3 as a while loops
Top
|