Loops
Do you want to repeat yourself? Do you want to repeat yourself? Do you want to repeat yourself? But you don’t want to keep typing the same thing over and over again? The way to do this in Java is with loops.
The first loop we will look at is the ‘for‘ loop.
The ‘for‘ loop has the following format.
for(starting value; conditional check; modifying expression) { code block to be executed... }
This type of loop is good to use when you know you want a block of code to be executed a set number of times. Let’s look at an example program:
public class MyClass {
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
In our for loop we see the starting value of int i = 0. Then the conditional check, i < 10. This loop will keep cycling through for as long as the value of int i is less than 10. And last, i++, is our modifying expression. With each loop cycle, the value of int i incresses by 1. So the output of this program is:
0 1 2 3 4 5 6 7 8 9
Java has two other key words that can be used with a loop. They are ‘continue‘ and ‘break.’
The keyword ‘continue‘ will cause a loop to jump out of its current cycle and skip ahead to the next run through, while ‘break‘ will cause a loop to stop cycling through all together. Here is an example:
public class MyClass { public static void main(String[] args) {for(int i = 0; i < 10; i++) {
if(i == 2) { continue; } if(i == 8) { break; }
System.out.println(i); } } }
Now the output is:
0 1 3 4 5 6 7
The next loop we will look at is the ‘while‘ loop.
The ‘while‘ loop has the following format:
while(conditional check) { code block to be executed... }
This type of loop is good to use when you want a block of code to keep cycling through until a certain condition tells it to stop. It is usually a good choice for video game programming.
Let’s look at an example of a ‘while‘ loop:
public class MyClass { public static void main(String[] args) {int i = 0;
while(i < 10) { System.out.println(i); i++; }
} }
This type of loop, however, does not provide a starting value nor a modifying expression. You must supply some way to break or end the loop. Otherwise, the loop will just keep going on forever.
Since the ‘while‘ loop does have a conditional check, it is possible that the check evaluates to false on the first try. When that happens the loop never takes place, not even once. Like so:
public class MyClass { public static void main(String[] args) {int i = 12;
while(i < 10) { System.out.println(i); i++; }
} }
The solution Java provides to this scenario, is the ‘do while‘ loop.
The ‘do while‘ loop has the following format:
do{ code block to be executed... }while(conditional check);
This type of loop will cycle through at least once, even if the conditional check evaluates to false. Here is an example:
public class MyClass { public static void main(String[] args) {int i = 12;
do{ System.out.println(i); i++; }while(i < 10);
} }
The output of this example is just:
12
Challenge:
The following code produces an infinite loop. Fix the code so that the last number printed to the screen is 15. Then end the loop.
public class MyClass { public static void main(String[] args) {int i = 0;
do{
System.out.println(i);
i++;
}while(true);
} }
Comments
Post a Comment