Cannot understand for loop with two variables

20,642

Solution 1

for(i=0,j=0;i<3,j<2;i++,j++)

is equivalent to

for(i=0,j=0;j<2;i++,j++)

The comma expression takes on the value of the last expression.

Whichever condition is first, will be disregarded, and the second one will be used only.

Solution 2

The for loop consists of:

for(START_STATEMENT; CONDITION_EXPRESSION, LOOP_EXPRESSION) BODY_BLOCK

Where:

  • START_STATEMENT is any single statement, which may include variable declaration. If you want to declare 2 variables, you can write int i=0, j=0, but not int i=0; int j=0 because the latter are actually 2 statements. Also node, that variable declaration is a part of statement, but cannot be a part of (sub) expression. That is why int i=0, int j=0 would also be incorrect.

  • CONDITION_EXPRESSION is any single expression that evaluates to a boolean value. In your case you are using a coma operator which has the following semantics: A, B will do:

    • evaluate A (it will evaluate, not just ignore)
    • ditch the result of A
    • evaluate B
    • return B as the result

    In your case: i<3,j<2 you are comparing i<3, you are just ignoring the result of this comparison.

    Comma expressions are useful when the instructions have some side effects, beyond just returning a value. Common cases are: variable increment/decrement or assignment operator.

  • LOOP_EXPRESSION is any single expression that does not have to evaluate to anything. Here you are using the comma expression again, ignoring the result of the left-hand-side. In this case however, you are not using the result anyway, and just using the ++ side effect - which is to increment the values of your variables.

  • BODY_BLOCK is either a single statement or a block, encapsulated with curly braces.

The above for can be compared to:

{
    START_STATEMENT;
    while(EXPRESSION) {
        BODY_BLOCK;
        LOOP_EXPRESSION;
    }
}
Share:
20,642
Arun
Author by

Arun

Updated on June 08, 2020

Comments

  • Arun
    Arun almost 4 years

    When I use two variables in a for loop with different conditions two conditions like I have used below i<3,j<2 the for loop is always executing till the second condition fails.

    #include<iostream>
    #include<conio.h>
    using namespace std ;
    int main()
    {
    int i,j ;
    for(i=0,j=0;i<3,j<2;i++,j++)
    {
        cout<<"hello" ;
    }
    getch() ;
    return 0 ;
    } 
    

    In that code, hello is printed 2 times. Why?

    If I use i<3,j<10, "Hello" is printed 10 times. I can't understand why the first condition is being neglected. Is it compiler dependent or something else?

    Every thing works normal if I replace with conditions like || (OR) or &&(AND).An other thing is that I cannot initialize i and j in the for loop itself, it is showing me an error, but works fine when I declare variables in C style or one variable outside the for loop, why is it so?

    Compiler I have used is Orwell Dev C++.
    Thanks in advance.

  • Arun
    Arun over 10 years
    But why is it that i cant declare i and j in the loop itself.The second part of the question in the post
  • P0W
    P0W over 10 years
    @Arun may be you should re-visit for loop, from a good book
  • David Schwartz
    David Schwartz over 10 years
    @Arun You don't show us the code that generated the error nor the specific error you got, so we could only guess.
  • pt12lol
    pt12lol over 10 years
    Why 'continue' instruction? It doesn't modify anything.
  • CygnusX1
    CygnusX1 over 10 years
    Not really needed. It was there in previous (incorrect) version which I quickly corrected, but left it there.
  • pt12lol
    pt12lol over 10 years
    Ok, so I'd like it ;)