Pages: [1]   Go Down
Author Topic: incrementation in for ( function  (Read 327 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

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

Code:
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:
for (int i=0; i <= 255; i++){
      //instruction here ;
   }

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

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

thanks for you thoughs to enlight me !
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3350
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Shannon Member
****
Karma: 199
Posts: 11639
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The for loop is equivalent to a while loop in a block:
Code:
for (int i = 7 ; i < 77 ; i+=2)
{
  foo () ;
}
is interpreted as if its
Code:
{
  int i = 7 ;
  while (i < 77)
  {
    foo () ;
    i += 2 ;
  }
}
Logged

[ I won't respond to messages, use the forum please ]

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 286
Posts: 25659
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 31, 2013, 12:30:21 am by AWOL » Logged

"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.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
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 smiley-wink

Logged

Rob Tillaart

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

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

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

Code:
for (initialization; condition; incrementation) {
//instruction;
}

Think of it as:
Code:
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:
for(z = 100; z<=500; z+=100)
will give the same number of iterations (5) as
Code:
for(z = 0; z<=4; z++)
but sometimes you will want to use the value of the variable within the code that follows.
Logged

Cincinnati, OH
Offline Offline
God Member
*****
Karma: 40
Posts: 666
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Beginning students often stumble on for loops because of the order of processing. The syntax of the for loop:

Code:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: