When you know how many times you want to repeat a block of code, use a FOR-LOOP.
THEORY
This is how you have to program it:
remember in Java it is usual to count from 0
Instead of saying, play 5 times, do you say play from 0 until 4, which is 5 times, if you move in steps of one.
EXTRA INFO
If you move with bigger steps you don’t do 5 repetitions.
e.g. in steps of two: you can do 3 repetitions
example
This is how it looks in code, if you want to play a loop that prints 5 times the sentence WHAT ARE THOOSEE!
DON’t DO IT LIKE THIS YOU GET AN ERROR!
Or this:
DO IT LIKE THIS!
You can optimise the code, this is how people commonly use a for-loop:
so instead of:
- declaring the variable i first, and then initialising i,
in the for-loop you can do all at ones.
- This code
is the same as this code