What a while loop does
A while loop runs a block of statements again and again as long as a condition is true. Before every pass, Scala evaluates the condition. If it is true, the block runs once and the condition is checked again. If it is false, the loop ends and the program continues with the line after the closing brace.
while (condition) {
instruction1
instruction2
}
The condition is an expression of type Boolean, such as count <= 5. You can think of a while loop as an if statement that jumps back and asks its question again after each run of the block.
A worked example
var count = 1
while (count <= 5) {
println("I made a mistake")
count += 1
}
println("Finished!")
I made a mistake
I made a mistake
I made a mistake
I made a mistake
I made a mistake
Finished!
Here is what happens, step by step:
countis created with the value 1.- Is
1 <= 5? Yes. Print the message, thencountbecomes 2. - Is
2 <= 5? Yes. Print again,countbecomes 3. - The same happens while
countis 3, 4 and 5, for five messages in total, and the last pass leavescountat 6. - Is
6 <= 5? No. The loop ends without running the block. println("Finished!")runs once, because it is after the loop.
Every counting loop has the same three parts: a variable that starts somewhere, a condition that keeps the loop going, and a change to the variable inside the body that eventually makes the condition false.
Scala details
- Use
var, notval. The counter changes, and avalcan't be reassigned.v += 1on avaldoes not compile. - Scala has no
++. Writecount += 1(orcount = count + 1). Writingcount++is a compile error:end of statement expected but identifier found. - The condition must be a
Boolean. Scala doesn't treat a number as true or false. - An unchanged counter never ends. If the body never changes the variable used in the condition, the condition stays true and the loop runs forever.
The exercise
The task is to print I promise to learn coding. five times with a loop instead of five separate statements. The lesson builds it in four steps:
- Declare the counter:
var counter = 1. It starts at 1 and is avarbecause the loop will change it. - Write the loop header:
while (counter <= 5) { }. The body runs for the values 1, 2, 3, 4 and 5. - Print inside the body:
println("I promise to learn coding."). - Increase the counter in the body with
counter += 1. Without that line,counterstays 1, the condition is always true and the program never stops.
var counter = 1
while (counter <= 5) {
println("I promise to learn coding.")
counter += 1
}
After the loop, counter is 6, the first value that fails the condition. Changing the 5 in the condition changes how many lines print; the message appears only once in the code.
Common mistakes
- Using
<instead of<=. Starting at 1 withcounter < 5gives four passes, not five. - Forgetting the update. This is the infinite loop described above.
- Updating before printing when the printed value matters. In the lesson's exercise the message doesn't use the counter, so the order doesn't matter, but in the next lesson it does.
Key takeaways
while (condition) { ... }repeats the block while the condition is true, checking before each pass.- A loop needs a starting value, a condition and an update that moves toward ending it.
- In Scala the counter is a
varand is increased with+= 1.
