2. Repeating instructions
To repeat a set of instructions, we can use
a while loop, which checks a condition
(like an if statement) and continues as
long as the condition is true.
while (condition to check){
instructions;
}
3. A word of advice
If the while loop checks a certain variable,
make sure that:
A) The variable has a value before the loop
starts.
B) That variable can change inside the loop.
x = 1;
while (x<5){“output”} will never end
while (x<5) {x++; “output”} will stop once x is 5.
4. Incrementing
Incrementing (+1) and decrementing (-1)
are useful tools for iterating in loops.
int x = 0;
x++; //means x = x+1; called post-increment
++x; // means x = x+1; called pre-increment
x+=1; // means x = x+1;
Decrementing:
--x;
x--;