Hi,
Just a quick question regarding a for loop and how it works.
for (byte i = 0; i < 5; i++)
What happens the first time through this code?
Does it just set i to 0 or is i set to 1?
Just trying to find out how it works.
Thanks,
Martin
Hi,
Just a quick question regarding a for loop and how it works.
for (byte i = 0; i < 5; i++)
What happens the first time through this code?
Does it just set i to 0 or is i set to 1?
Just trying to find out how it works.
Thanks,
Martin
Arnak:
Hi,Just a quick question regarding a for loop and how it works.
for (byte i = 0; i < 5; i++)
What happens the first time through this code?
Does it just set i to 0 or is i set to 1?
Just trying to find out how it works.
Thanks,
Martin
Print the value of i in the body of the statement, and learn first hand what i is.
Hi Paul,
Thanks for the reply.
I have a problem doing that as I have the Arduino set up and running but with a Midi connection so it sends garbage to the Serial monitor.
I'll have to re-upload the test code and see what I get.
OK, done that it sets it to 0 the first time through...
Thanks,
Martin
The for loop has 3 expressions:
where:
exp1: usually initializes a loop counter that is used to control the loop. It is executed only once the when
the loop is first entered. e.g., i = 0;
exp2: usually a conditional test to decide whether another pass through the loop is warranted. e.g. i < 10
exp3: usually changes the value of the loop counter. e.g., i++
The execution sequence is:
exp1 (only once) when evaluation is completed, control passed to exp2
exp2
if exp2 evaluates to logic True, the statements in the loop body are executed. If not True, program
control passes to first statement after the End brace that marks the end of the loop body. If it is
True, statements in loop body are executed and control is passed to exp3
exp3
After expression is evaluated, control passes to exp2 to see if another passes is need.
Infinite loops occur when exp2 never evaluates to logic False.