how to find the output of the following segment?

for (x=1, y=5; x+y<=10; x++)
{
cout <<x<< “ “<<y<<endl;
y++;
}
May I know what variable u want to output?

Because u did output for both variables that is in the code
Last edited on
how to find the output of the following segment?


Run the program, and see what the output is.
for (x=1; y=5; x+y<=10; x++;y++)
Will result in compile errors. The for loop takes only three portions(or any other suitable word) of statements. Syntax:

for(initialization;condition;increment/decrement);

Also, if you wanna specify more than one statement in the "portions", then use the comma(',') operator.
eg:
for (x=1, y=5; x+y<=10; x++,y++)


@vichu8888
So I took out my suggestion

Yeah no problem!

Aceix.
Last edited on
@Aceix
Is your reply for me?
Because I suggested him the code
for (x=1; y=5; x+y<=10; x++;y++)
But later when I tried in my lappy, I examined the error. So I took out my suggestion.

Sorry for that
Last edited on
for (x=1, y=5; x+y<=10; x++)
{
cout <<x<< “ “<<y<<endl;
y++;
}

Output:
1 5
2 6
3 7
It's not that hard to work it out in your head.

Both x and y are incremented by 1 at the end of each iteration of the loop. That means (x+y) goes up by 2 each time. Thus (x+y) is 6, 8 10, meaning three passes through the loop. The values of x and y output each time are straightforward.
No replies from the OP - could be a typical trolling question?

If not. let's see some feedback / thanks from the OP.
Topic archived. No new replies allowed.