Go Down

### Topic: incrementation in for ( function (Read 586 times)previous topic - next topic

#### steph23

##### May 30, 2013, 05:27 pm
Hi

i'm bringing a basic understanding question regarding the arguments of the function

Code: [Select]
`for (initialization; condition; incrementation) {//instruction;}`

It seems incrementation is always the variable adding "++" when incrementing 1
so what I can't get is the difference between those 3 exemples :

CASE #1
Code: [Select]
`for (int i=0; i <= 255; i++){       //instruction here ;    } `

CASE #2
Code: [Select]
`for (int i=0; i <= 255; 1){       //instruction here ;    } `

CASE 3
Code: [Select]
`for (int i=0; i <= 255; a++){       //instruction here ;    } `

thanks for you thoughs to enlight me !

#### Arrch

#1
##### May 30, 2013, 05:31 pm
Case 1 is the proper way to do it.

Case 2 and 3 will cause an infinite loop because neither affect the value of i, which is what is checked in the condition.

#### MarkT

#2
##### May 30, 2013, 05:53 pm
The for loop is equivalent to a while loop in a block:
Code: [Select]
`for (int i = 7 ; i < 77 ; i+=2){  foo () ;}`
is interpreted as if its
Code: [Select]
`{  int i = 7 ;  while (i < 77)  {    foo () ;    i += 2 ;  }}`
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### AWOL

#3
##### May 30, 2013, 06:10 pmLast Edit: May 31, 2013, 07:30 am by AWOL Reason: 1
Quote
[i'm bringing a basic understanding question regarding the arguments of the function

It may look like a function, but it isn't and those are not arguments.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### robtillaart

#4
##### May 30, 2013, 09:49 pm
Code: [Select]
`for (int i=0; i <= 255; 1){       //instruction here ;    }`
Technically it is valid (compilable) code, but highly unusual. If the instructions in the loop modify the var i  it can be a finite loop. Otherwise it will be infinite.

Code: [Select]
`for (int i=0; i <= 255; a++){       //instruction here ;    }`
Same here as above, with the remark that - the var a - must be declared and initialized before the loop and it will increment with the number of times the for loop is executed, still unusual, but it might just do exactly what you need

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### Henry_Best

#5
##### May 31, 2013, 05:43 am

Hi

i'm bringing a basic understanding question regarding the arguments of the function

Code: [Select]
`for (initialization; condition; incrementation) {//instruction;}`

Think of it as:
Code: [Select]
`for (initialize the variable; limit of the variable; step of the variable) {//instruction;}`
Step can be any integer (positive or negative) and is a step towards the limit of the variable.
Code: [Select]
`for(z = 100; z<=500; z+=100)`
will give the same number of iterations (5) as
Code: [Select]
`for(z = 0; z<=4; z++)`
but sometimes you will want to use the value of the variable within the code that follows.

#### econjack

#6
##### May 31, 2013, 03:48 pm
Beginning students often stumble on for loops because of the order of processing. The syntax of the for loop:

Code: [Select]
`for (expression1; expression2; expression3) {   // Loop Statement Body: one or more statements controlled by the for loop}// First statement after closing for loop brace`

where:
expression1: often used to initialize a variable that controls the loop. This expression is only executed once, when the loop first begins.
expression2: often a relational test that evaluates to True or False. If True, the Loop Statement Body is executed. If False, program continues with the statement following the closing for loop brace.
expression3: If expression2 was logic True, when the Loop Statement Body completes, control is sent to expression3. Often this statement changes the state of the variable initialized in expression1.
If neither expression3 or statements in the Loop Statement Body do not change the state of the loop and expression2 was True when the loop began, expression2 will always be True,
resulting in an infinite loop.

#### PaulS

#7
##### May 31, 2013, 03:54 pm
Quote
Step can be any integer (positive or negative) and is a step towards the limit of the variable.

Step doesn't have to be an integer. It can be a float, a character, etc. Whatever makes sense for the situation.

Go Up

Please enter a valid email to subscribe