For Loop statement execution sequence

Source: Internet
Author: User

In a technology QQ group, I tried to make a C question. I thought it was a test of the For Loop execution process.

Original question: in C language, the output result of the following code is () (select one)

Void main ()
{
Int I = 0, j = 10;
For (I = 0; I <j; I ++, j --)
{
If (j = 10)
{
Printf ("% d, % d", I, j );
J --;
}
J ++;
}
Printf ("% d, % d", I, j );
}

A) 0, 10
9,9

B) 0, 10
10, 10

C) 10, 10

D) 9,11

Many people in the group have chosen B, and the questioner has been asking for the correct results.

The correct result is:.

Haha, good basic knowledge.

Later, I wrote a code by hand, and you can understand the order of the for loop operation.


Int print1 (int I, int j)
{
Printf ("execution judgment condition, I = % d, j = % d", I, j );
Return 1;
}

Void main ()
{
Int I = 0, j = 10;

For (I = 0; (print1 (I, j) & (I <j ));
I ++, j --, printf ("execute the third statement in for (), I = % d, j = % d", I, j ))
{
If (j = 10)
{
Printf ("% d, % d", I, j );
J --;
}
J ++;
Printf ("execute the last sentence in the for loop body, I = % d, j = % d", I, j );
}
Printf ("% d, % d", I, j );
}

The running result is:

Execution condition, I = 0, j = 10
0, 10

Execute the last sentence in the for loop body, I = 0, j = 10
Execute the third statement in for (), I = 1, j = 9

Execution condition, I = 1, j = 9
Execute the last sentence in the for loop body, I = 1, j = 10
Execute the third statement in for (), I = 2, j = 9

Execution condition, I = 2, j = 9
Execute the last sentence in the for loop body, I = 2, j = 10
Execute the third statement in for (), I = 3, j = 9

Execution condition, I = 3, j = 9
Execute the last sentence in the for loop body, I = 3, j = 10
Execute the third statement in for (), I = 4, j = 9

Execution condition, I = 4, j = 9
Execute the last sentence in the for loop body, I = 4, j = 10
Execute the third statement in for (), I = 5, j = 9

Execution condition, I = 5, j = 9
Execute the last sentence in the for loop body, I = 5, j = 10
Execute the third statement in for (), I = 6, j = 9

Execution condition, I = 6, j = 9
Execute the last sentence in the for loop body, I = 6, j = 10
Execute the third statement in for (), I = 7, j = 9

Execution condition, I = 7, j = 9
Execute the last sentence in the for loop body, I = 7, j = 10
Execute the third statement in for (), I = 8, j = 9

Execution condition, I = 8, j = 9
Execute the last sentence in the for loop body, I = 8, j = 10
Execute the third statement in for (), I = 9, j = 9

Execution condition, I = 9, j = 9

9,9
--------------

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.