Loading...

for-loop part 1

__info__

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.

__info__

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 parameter that need to be separated by a semi-colon:

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

__info__

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

Step 1: initialisation: the variable ledNumber=1 initialises ledNumber with 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...

__info__

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!

__task__

Compile the program and test it on your BOB3.




__quiz__

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…
Fahre mit der Maus über die Symbole!