Order of execution for loop statement headers

Source: Internet
Author: User

it's a refresher. The problem is relatively basic, but also more important.

For loops can be said to be indispensable in every program, the statement header consists of three parts: initialization, interpretation conditions, an expression.

But what is the order of execution of these three parts, which we are concerned about, is also discussed in this article.

Let's look at a simple piece of code.

#include <stdio.h>

int main ()
{
    int x, A;
    
    for (x=0,a=0; a<=1 &&!x++; a++)
    {
        a++;
    }
    
    printf ("%d", a);
    return 0;
}

The code is simple, but print the result?

Some friends may take it for granted that the print result is 1.

But is it really 1? Copy to the compiler run, miraculously found, is actually 2.

The following is an analysis of this simple code.


The first step: Initialize the definition, x = 0, a = 0;

Step Two: A is less than 1, X is not 1, in line with the cyclic conditions

The third step: X + + After the increase of 1.

Fourth Step: Enter the circulation body, the a++,a is increased to 1.

Step Fifth: The a++,a in the execution loop statement header is 2.

Sixth step: The execution circular statement Head's interpretation condition, a=2>1, therefore does not satisfy, does not execute the circular statement.

Step Seventh: Print a value of 2.


How, now the suddenly enlightened. (If you still don't understand ...) I'm sorry about that. )


From this example, we can draw the following conclusions:

The order of execution is: initialization--judgment--the function body--and then the 3rd sentence of the For loop--the judgment--the function body---the third sentence of the For loop--judgment ...


Here are some questions to answer:

1, the first time such as initialization i=0, whether the following i++ or ++i is also performed.

First initialize, then judge, i++ will not execute

2, the first time, after the initialization of this sentence is not, is the first implementation of i++ or restrictive judgments.

Execute i++ First

3, now know is the first implementation of i++, and then execute the judgment statement, i++ is not after + +, such as I for 1, that i++ after the execution of judgment statements, I is a few.

I was 2.

4, as if i++ and ++i in for () The result is the same.

All the same, the order of the execution of the statement header determines that both of the two ways of writing are the same, are i++ or this ++i after the two sentences are executed and then judged, at this time (two cases) I have added 1.

Analysis of so much, I believe we all understand.

Leave a classic example for everyone to analyze.

#include <stdio.h>

int main ()
{
    int i = 0, j = Ten;
    For (i=0 i<j; i++,j--)
    {
        if (j==10)
        {
            printf ("%d,%d", i,j);
            j--;
        }
        j + +;
    }
    printf ("%d,%d", i,j);
    return 0;
}

Print results:

0,10 9,9

If you do not understand, you can refer to the following example of the above example step-by-step printing, should be helpful to understand.

#include <stdio.h>

int print1 (int i,int j)
{
    printf ("Execute judgment conditions, i=%d,j=%d \ n", i,j);
    return 1;
}

int main ()
{
    int i = 0, j = Ten;
    
    For (I=0 (Print1 (i,j)) && (i<j));
        I++,j--, printf ("Execute the third statement in for (), i=%d,j=%d \ n", i,j))
    {
        if (j==10)
        {
            printf ("%d,%d", i,j);
            j--;
        }
        j + +;
        printf ("Execute the last sentence in the body for the loop, i=%d,j=%d\n", i,j);
    }
    printf ("%d,%d", i,j);
    return 0;
}

Print results:

Execute judgment Conditions , i=0,j=10

0,10 Perform for the last sentence in the body of a loop , i=0,j=10

executes The third statement in for () , i=1,j=9

Execute judgment Conditions , I=1,j=9

Perform for the last sentence in the body of a loop , i=1,j=10

executes The third statement in for () , i=2,j=9

Execute judgment Conditions , I=2,j=9

Perform for the last sentence in the body of a loop , i=2,j=10

executes The third statement in for () , i=3,j=9

Execute judgment Conditions , I=3,j=9

Perform for the last sentence in the body of a loop , i=3,j=10

executes The third statement in for () , i=4,j=9

Execute judgment Conditions , I=4,j=9

Perform for the last sentence in the body of a loop , i=4,j=10

executes The third statement in for () , i=5,j=9

Execute judgment Conditions , I=5,j=9

Perform for the last sentence in the body of a loop , i=5,j=10

executes The third statement in for () , i=6,j=9

Execute judgment Conditions , I=6,j=9

Perform for the last sentence in the body of a loop , i=6,j=10

executes The third statement in for () , i=7,j=9

Execute judgment Conditions , I=7,j=9

Perform for the last sentence in the body of a loop , i=7,j=10

executes The third statement in for () , i=8,j=9

Execute judgment Conditions , I=8,j=9

Perform for the last sentence in the body of a loop , i=8,j=10

executes The third statement in for () , i=9,j=9

Execute judgment Conditions , 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.