5

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
  • 111
  • 1
  • 1
  • 8

3 Answers3

7

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.

P0W
  • 46,614
  • 9
  • 72
  • 119
7

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;
    }
}
CygnusX1
  • 20,968
  • 5
  • 65
  • 109
-1

The c complier always used second condition.

therefore j<2 is used.

use this for loop

      for(i=0,j=0;j<10;i++,j++)
Dev
  • 3,410
  • 4
  • 17
  • 16