-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 ?

2
  • Could you make your question more clear? Commented Apr 27, 2017 at 12:53
  • 4
    This wouldn't compile. Should the third comma be a semicolon? Commented Apr 27, 2017 at 12:56

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"true" would probably be clearer than "non-zero" here.
@Quentin I don't think so, since the type of the result is int and people expect true to be of type _Bool (or bool).
0
#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.

1 Comment

When they posted this, their problem appeared to be a , and they didn't ask why how. I just pasted the correct syntax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.