-2

I want to know how for loop is processed in below condition.

void main()
{  
    int i,j;
    For(i=1,j=1;i<=5,j<=10,i++,j++)
    {
        printf("%d%d",i,j);
    }
}

sorry for typo mistake I correct my syntax here

For(i=1,j=1;i<=5,j<=10;i++,j++)

answer of this -1122334455667788991010

How's that possible because loop for I will be iterate for only 5 times how's that possible ? I want to know how loop will be executed ?

Pradnya Bolli
  • 1,872
  • 1
  • 16
  • 36

2 Answers2

4

This won't compile, there's only one ; in the for which is a syntax error.

I'll assume it should read like this:

for(i=1, j=1; i<=5, j<=10; i++, j++)

then it would step both i and j to 10.

This is because the for-loop's middle part, the condition, reads i<=5,j<=10 which is a use of the comma operator where perhaps a boolean and (&&) would be better.

It will evaluate i<=5, throw away that result, and then evaluate j<=10, running the loop for as long as that value is non-zero.

unwind
  • 378,987
  • 63
  • 458
  • 590
-1
#include <stdio.h>

int main(int argc, char** args){
    for(int i = 0, j=0; i<10&&j<10; i++, j++){
        printf("%d, %d\n", i, j);
    }
}

The semi colon's seperate the terms of the for statement. (intializer; condition; action at end of loop) You can do what you like for the sections.

derM
  • 12,209
  • 4
  • 44
  • 80
matt
  • 9,626
  • 3
  • 20
  • 33
  • When they posted this, their problem appeared to be a `,` and they didn't ask why how. I just pasted the correct syntax. – matt Apr 27 '17 at 12:58