incrementation in for ( function

Hi

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

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

for (int i=0; i <= 255; i++){ 
      //instruction here ; 
   }

CASE #2

for (int i=0; i <= 255; 1){ 
      //instruction here ; 
   }

CASE 3

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

thanks for you thoughs to enlight me !

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.

The for loop is equivalent to a while loop in a block:

for (int i = 7 ; i < 77 ; i+=2)
{
  foo () ;
}

is interpreted as if its

{
  int i = 7 ;
  while (i < 77)
  {
    foo () ;
    i += 2 ;
  }
}

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

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.

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

steph23:
Hi

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

for (initialization; condition; incrementation) {

//instruction;
}

Think of it as:

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.

for(z = 100; z<=500; z+=100)

will give the same number of iterations (5) as

for(z = 0; z<=4; z++)

but sometimes you will want to use the value of the variable within the code that follows.

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

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.

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.