We are incrementing the variable cwhile it is lowerorequal than 42. If the variable is higher than 42, the condition will not be true and the block will not be ran.
The previous example do not work because we declare a variable v in the while block and we are using it out of it scope.
For
The for is very similar to the while statement but we can have an initialization statement and an iteration statement in addition to the condition that the while have.
The initialization that occurred in the for must stay in the for's scope, otherwise the code will not compile due to an error.
Synopsis
if we would change this code to use a while statement, it will look like this:
We can see that both statement are pretty similar but the for can let you do more things because it have more options.
Example:
We are incrementing the variable c while a variable i declared in a for is lower than 42.