Java has a 3 types of loops. The one we're going to be focusing on is a while loop. A while loop allows you to repeat a certain portion of the code a number of times.
The while loop is tested with the loop-continuation-conditional.
while (x < 10) { }
In the example above, the less than operator (<) is the loop-continuation-conditional. It states that while x is less than 10, statements within the loop body { } will continue to execute.
The above example will continue infinitely because there are no statements within the loop body to ever trigger the loop-continuation-conditional to be false.
A more realistic example would be
int x = 0;
while (x < 10) {
System.out.print(x + " - ");
x++;
}
Zero is assigned to the x variable.
A while loop is generated testing whether x is less than 0.
If so, the user will see x printed on their screen.
x is incremented by 1 and the while loop repeats.
During the first iteration, x is 0.
Is 0 < 10?
Yes, so display x: display 0.
Increment x by 1. So 0 + 1 = 1.
- Side note, x++ is the same as x = x +1.
x is assigned the value of 1.
The while loop repeats testing whether 1 < 10.
Yes, so display x: display 1.
...
Until finally x is incremented to 10.
Is 10 < 10?
No. 10 is equal to 10 but it's not less than 10.
The script terminates.
Your result should look like this:
0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 -
You may be asking yourself why the result is displayed on the same line. System.out.print() is identical to System.out.println() with the exception that System.out.println() also inserts a new line after the statement is executed.
You may also be asking yourself why isn't x + " - " performing the sum of x and " - ". When a string is involved, the + operator is treated as a concatenation operator appending one string to another, or a number to a string. So 1 + 2 would yield 3, while 1 + "2" would yield 12.
Another thing you may do is test whether the number being displayed is the last number in the sequence. If so, no trailing dash needs to be displayed.
To do this,
int x = 0;
string dash;
while (x < 10) {
dash = (9 == x) ? "" : " - ";
System.out.print(x + comma);
x++;
}
The above code runs exactly the same as the previously mentioned one except it removes the last dash. Your result will look like this:
0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9
It initiates the variable dash and sets it to the string data type.
It performs the test within the loop body to see if the last number is reached. In this case we know that the maximum amount of iterations that the loop can have is 10; the loop will terminate once it reaches 10. So when x reaches 1 less than the maximum number, in this case 9, we want the dash variable to be assigned an empty string, otherwise assign a dash.
You may be wondering what the code dash = (9 == x) ? "" : " - "; is.
It's a short way of writing an if (condition) { statement } else { other statement }
The ?: is called a ternary operator in Java. It basically substitutes the if and else.
The above code can be re-written as
if (9 == x) {
dash = "";
} else {
dash = " - ";
}
In the following example, we'll generate 2 random numbers and ask the person what the answer to the sum of the 2 numbers is.
While the answer is wrong, it'll keep asking the person to try again. Once the user guesses the answer correctly, the while loop will terminate and the next statement will execute which just notifies the user that they guessed the answer correctly.
The while loop is tested with the loop-continuation-conditional.
while (x < 10) { }
In the example above, the less than operator (<) is the loop-continuation-conditional. It states that while x is less than 10, statements within the loop body { } will continue to execute.
The above example will continue infinitely because there are no statements within the loop body to ever trigger the loop-continuation-conditional to be false.
A more realistic example would be
int x = 0;
while (x < 10) {
System.out.print(x + " - ");
x++;
}
Zero is assigned to the x variable.
A while loop is generated testing whether x is less than 0.
If so, the user will see x printed on their screen.
x is incremented by 1 and the while loop repeats.
During the first iteration, x is 0.
Is 0 < 10?
Yes, so display x: display 0.
Increment x by 1. So 0 + 1 = 1.
- Side note, x++ is the same as x = x +1.
x is assigned the value of 1.
The while loop repeats testing whether 1 < 10.
Yes, so display x: display 1.
...
Until finally x is incremented to 10.
Is 10 < 10?
No. 10 is equal to 10 but it's not less than 10.
The script terminates.
Your result should look like this:
0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 -
You may be asking yourself why the result is displayed on the same line. System.out.print() is identical to System.out.println() with the exception that System.out.println() also inserts a new line after the statement is executed.
You may also be asking yourself why isn't x + " - " performing the sum of x and " - ". When a string is involved, the + operator is treated as a concatenation operator appending one string to another, or a number to a string. So 1 + 2 would yield 3, while 1 + "2" would yield 12.
Another thing you may do is test whether the number being displayed is the last number in the sequence. If so, no trailing dash needs to be displayed.
To do this,
int x = 0;
string dash;
while (x < 10) {
dash = (9 == x) ? "" : " - ";
System.out.print(x + comma);
x++;
}
The above code runs exactly the same as the previously mentioned one except it removes the last dash. Your result will look like this:
0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9
It initiates the variable dash and sets it to the string data type.
It performs the test within the loop body to see if the last number is reached. In this case we know that the maximum amount of iterations that the loop can have is 10; the loop will terminate once it reaches 10. So when x reaches 1 less than the maximum number, in this case 9, we want the dash variable to be assigned an empty string, otherwise assign a dash.
You may be wondering what the code dash = (9 == x) ? "" : " - "; is.
It's a short way of writing an if (condition) { statement } else { other statement }
The ?: is called a ternary operator in Java. It basically substitutes the if and else.
The above code can be re-written as
if (9 == x) {
dash = "";
} else {
dash = " - ";
}
In the following example, we'll generate 2 random numbers and ask the person what the answer to the sum of the 2 numbers is.
While the answer is wrong, it'll keep asking the person to try again. Once the user guesses the answer correctly, the while loop will terminate and the next statement will execute which just notifies the user that they guessed the answer correctly.
Comments
Post a Comment