for-loop part 1

Now we're going to learn about the for loop

In line 5 we've declared a new variable, ledNumber.

The datatype int means that it can store whole numbers, for example 2 or 504.

In line 6 you can see a for loop begins.

A for loop is used to execute commands several times.

The for loop has 3 parameters that need to be separated by a semi-colon:

a) initialisation: ledNumber=1
b) condition check: ledNumber<5
c) update: ledNumber++

In line 6 the loop is run for the first time:

Step 1: initialisation: the expression ledNumber=1 initialises the variable ledNumber with the value 1.

Step 2: condition check: is ledNumber smaller than 5?

Step 3: if the condition is true, the commands are executed. In this example, the command is:

bob3.setLed(ledNumber, WHITE);

...let's continue to the next info box...

Step 4: update the variable ledNumber. The command ledNumber++ increases ledNumber by 1.

Step 5: Repeat from step 2. A new run is started, whereby ledNumber has the value 2 this time.

Steps 2 and 5 are repeated until the condition check returns false!

Compile the program and test it on your BOB3.
1: What does the programme do?
 You haven’t completed anything yet!
 Super, so far so good…
 Super, you got it all right!
 Sadly not correct…
 One answer is correct
 A couple are correct…