Zerglurker C language tutorial 007 -- code execution sequence, zerglurker language tutorial

Source: Internet
Author: User

Zerglurker C language tutorial 007 -- code execution sequence, zerglurker language tutorial

In software development, the Code has three basic execution sequence:

The code is executed one by one from the entry until it is returned or ended.

After a set condition is executed cyclically, the Code repeats one or more parts until the execution ends after certain conditions are met.

Conditional Execution Code first determines certain conditions. If some conditions are met, partial code is executed. If some conditions are not met, another part of code is executed.

In reality, these three execution sequences are staggered. You have me, and I have you.

The following is the code of the last lesson. Let's take a look at it and you will understand what I mentioned above:

# Include <stdio. h> # include "public. h "int main (int argc, char * argv [], char * env []) {maid (10); return 0;} void maid (unsigned int nTotal) {int nFirst = 1; int nSecond = 1, nThird; unsigned int I; if (nTotal = 0) {return; // if the number of outputs required by the user is 0, then nothing is output} printf ("% d \ n", nFirst); if (nTotal = 1) {// if the number of outputs required by the user is 1, return;} // output the first two numbers to printf ("% d \ n", nSecond); if (nTotal <= 2) // If you specify no more than 2 outputs, return; for (I = 2; I <nTotal; I ++) {nThird = nFirst + nSecond; printf ("% d \ n", nThird); nFirst = nSecond; nSecond = nThird ;}}
The first is the main function, which contains 4-8 lines of the entire code.

This is a typical sequence of code execution. Step by step from the function entry.

The Code logic for sequential execution is clear and there is no ambiguity.

Then there is a complicated Fibonacci function (10-34 rows)

12-14 rows are executed sequentially. It mainly declares and initializes variables.

15-17 rows are a typical conditional execution sequence. The code in braces is executed only when nTotal is 0. Otherwise, the code is skipped directly.

Note: = indicates a value, while = indicates logic judgment. However, in C/C ++, a poor setting is that even a value assignment expression has a return value.

For example, nFirst = 1.

If you make this decision

If (nFirst = 1 ){

Printf ("done! \ N ");

}

Well, that output function printf ("done! \ N "); will always be executed. Because the value of the value assignment expression is always equal to the value assigned to it

That is, the value of nFirst = 0 is always equal to 0.

So when you want to determine whether nFirst is equal to 1, you must use = instead of =

In addition, the expression indicates no; yes; the statement

If the parentheses following the logical judgment keyword can only be written into expressions and cannot be written into statements

However, pay attention to the cycle module in line 27-33.

There are two statements and an expression in the brackets after.

Unlike what you see (I = 2; I <nTotal; I ++), only the first I = 2 and the last I ++ are statements, the expression in the middle

This syntax feature is unique to the for keyword and does not have a semicolon.

Of course, you can write the code as follows:

        for (i = 2;i < nTotal;i++){nThird = nFirst + nSecond;printf("%d\n", nThird);nFirst = nSecond;nSecond = nThird;}
Let's debug it to see how the code will be executed.

The procedure is as follows:

Select the first line of the above code, and then select debug-New breakpoint-interrupt at function, as shown in:

Of course, you can also left the mouse button in front of the code line number

Or, press Ctrl + B to create a breakpoint.

After the breakpoint is set up, press F5 to start debugging. Note that F5 is not Ctrl + F5

If everything goes well, you will see the following content:

The yellow arrow indicates the line of code to be executed by the current program

At this time, you can press F10 to execute code one by one.

Then you can observe how the code in the for statement is executed.

If you want to release debugging, you only need to clear the breakpoint in red on the line where the breakpoint is set, and then press F5.

But now you can take a single step to see how the code is executed in the for module.

If there is no accident, you will find that I = 2 is executed only once, and I ++ is executed from the second loop; I <nTotal is executed every time

During debugging, if you stop the mouse over a variable, the current value of the variable is displayed.

In this way, you can observe how I changes.

Now, the three execution sequences of the Code have been completed.

You can practice code debugging well. In the future, this skill will accompany your entire development career.

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.