Why are the results different?

Why do I get different results depending on where the println(i) is placed in the block?

var i = 0
while (i < 8) {
  println(i)  
  i ++
}
println("Done")


var i = 0
while (i < 8) {  
  i ++
  println(i)
}
println("Done")

Hey Sotenna,

In the first example, you print the value of ā€œiā€ before you increase its value by one. In the second example, you print the value of ā€œiā€ after you change its value. As a result, the first example will give an output of:

0
1
...
6
7

while the second example gives an output of:

1
2
...
7
8

Hopes this helps!

4 Likes