C ++ program design statements starting from scratch

Source: Internet
Author: User
Tags integer numbers
C ++ programming from scratch-general Linux technology-Linux programming and kernel information. The following is a detailed description. As mentioned above, the program is the description of the method, and the description of the method is nothing more than the object of the action plus the action. The action here is represented by a statement in C ++, the action object is the resource that can be operated, but it is a pity that the C ++ language itself only supports one resource-memory. Because the computer can actually operate on more than memory resources, the C ++ language cannot actually be used as the programming language of the underlying hardware Program (even the C language cannot ), however, each compiler vendor provides its own Embedded Assembly statement function (or it may not provide or provide additional syntax to allow you to operate the hardware). For VC, you can use the _ asm statement to add assembly code to the C ++ code to operate other types of hardware resources. This statement is not described in this series.

A statement is an action. C ++ has two types of statements: a single sentence and a compound statement. A composite statement is enclosed by a pair of braces to put multiple single sentences at the same time, for example, {long a = 10; a + = 34 ;}. A single sentence ends with ";", but it may end with "}" because a compound statement is used to insert a single sentence at the end, for example, if () {a --; a ++ ;}. Note that you do not need to write ";" after braces, because it is not a single sentence.

The method is how it is done, and under what circumstances it is done in what order. Because the resources that can be operated in C ++ only have memory, the operation is very simple, just the calculation of memory content and the value assignment, that is, the expression mentioned above. For "What sequence", C ++ enforces that a single or compound statement can only be executed from top down to right from left to right (do not confuse the previous calculation sequence of expressions, it is just a rule in a single sentence ). Finally, we can determine the condition. In order to execute different codes under different circumstances, C ++ defines the jump Statement, which is implemented based on the CPU running rules, the following describes how the CPU executes the machine code.

Machine code running mode

As mentioned above, all the code in C ++ will eventually become the machine code that the CPU can recognize, because the machine code is a method description, it also contains the object of the action and action (or may not contain the object), that is, the identifier of the machine command and memory address or other hardware resources, and all are represented by binary numbers. Normally, these indicate that the binary number of machine code should be put into the memory for efficiency consideration (it can also be placed on the hard disk or other storage devices) during execution ), normally, each machine command can have an address corresponding to it.

The CPU contains a hardware with the same function as the memory used to temporarily record the binary number. It is called a register. The reading speed is much faster than the memory, but the size is much smaller. To speed up reading, the register is removed from the addressing circuit and one register can only store one 32-bit binary number (for 32-bit computers ). The CPU uses one of the registers to record the location of the machine commands to be run, which is called the instruction register here.

When the CPU is running, it retrieves the value of the instruction register, finds the corresponding memory, reads the content of 1 byte, and displays the machine instruction corresponding to the 8-bit binary number, and then perform the corresponding action. Because different commands may have different numbers of parameters (that is, the object of the preceding action), for example, the multiplication Command requires two parameters to multiply them, the reverse operation requires only one parameter. In addition, the multiplication of two 8-bit binary numbers and the multiplication of two 16-bit binary numbers are also different. Therefore, the length of the machine code formed by different commands with different parameters may be different. Each time the CPU executes a machine code, it adds the length of the Code to the instruction register so that the instruction register points to the next machine code, repeat the above process to run the program (this is just a simple description, the actual operation process is much more complicated due to the addition of various technologies, such as high-speed buffering ).

Statement category

In C ++, there are 6 types of statements: Statement declaration, Definition Statement, expression statement, command statement, pre-compiled statement, and comment statement. The declaration statement is described in the next article. The pre-compiled statement is described in another article. The definition statement is a previously seen definition variable, and the definition function and structure are described later. An expression statement is an expression directly connected with ";", such as: 34;, a = 34;, etc, the code for memory value operations is generated based on the definition of the computing function of the operator. A comment statement is a statement used to annotate the code. It is written to people, not to the compiler. The final command statement is a statement containing the following keywords, that is, their usefulness is not to operate on memory, but to implement the "what kind of situation" mentioned above ".

The declaration statements, pre-compiled statements, and comment statements are not converted into machine code, that is, these three statements are not used to operate the computer, but for other purposes. They will be detailed later. Definition statements do not necessarily generate machine code. Only expression statements and command statements generate code (without considering the compiler optimization function ).

You should also note that you can write null statements, namely; or {}, which do not generate any code. They only serve to ensure the correctness of the syntax and will be seen later. The following describes the comment statements and command statements-jump statements, judgment statements, and cyclic statements (in fact, due to exceptions and the introduction of the template technology, some statements are added, the exception and template are described respectively ).

Annotation statement --//,/**/

Annotations are the annotations used for interpretation, that is, some text information, which is used to explain the meaning of the Code to people who are looking at the source code, because the human cognitive space is completely different from that of computers, this will be discussed in detail in the future. You need to write a paragraph for comments. Use "/*" and "*/" to enclose the paragraph as follows:

Long a = 1;
A + = 1;/* a adds the number of people */
B * = a;/* B sets the per capita cost to get the total cost */

The above is for a + = 1; and B * =; two annotation statements are written to describe their meanings (as long as C ++ knows that they are the auto-incrementing of one variable and the auto-multiplication of another variable, but they do not know the meaning ). The above troubles are caused by the need to write "/*" and "*/". Therefore, C ++ provides another comment statement-"/":

Long a = 1;
A + = 1; // a places the number of people, and the number of people adds one
B * = a; // B indicates the per capita cost.

The above is equivalent to the previous one. "//" indicates that starting from it, all the characters after this line are treated as comments, and the compiler will ignore it, that is

Long a = 1; a + = 1; // a places the number of people, and the number of people increases by B * =;

Among them, B * = a; will not be compiled, because the previous "//" has told the compiler that from "//", all the characters after this line are comments, therefore, the compiler does not compile B * = ;. However, if

Long a = 1; a + = 1;/* a places the number of people, and the number of people increases by */B * =;

In this way, the compiler will still compile B * = a; because the comments are included in "/*" and.

Note that the comment statement is not a statement and does not end with ";". It is just another syntax to provide the comment function, just like the pre-compiled statement to be described later, neither a statement nor a compound statement ends with ";", but is still called a statement for reasons of habit.

Jump statement -- goto

As mentioned above, the statements in the source code (in this example, the code written in C ++) are converted into the machine code represented by different binary numbers, then place the order in the memory (this statement is not accurate ). See the following code:

Long a = 1; // assume the length is 5 bytes and the address is 3000
A + = 1; // The address is 3005, And the length is assumed to be 4 bytes.
B * = a; // The address is 3009, assuming the length is 6 bytes.

The above 3000, 3005, and 3009 indicate the locations of the above three statements in the memory, and the so-called jump statement, that is to say, place the preceding 3000 and 3005 statement addresses in the previously mentioned Instruction registers so that the CPU starts to execute from the given position to display a change in the execution sequence. Therefore, there must be a way to express the statement address. C ++ provides a Label for this ).

Write an identifier, followed by ":" To establish a ing, This identifier is bound to its location address, as shown below:

Long a = 1; // assume the length is 5 bytes and the address is 3000
P1:
A + = 1; // The address is 3005, And the length is assumed to be 4 bytes.
P2:
B * = a; // The address is 3009, assuming the length is 6 bytes.
Goto P2;

The above P1 and P2 are labels with values of 3005 and 3009 respectively, and the final goto is the jump Statement, in the format of goto <标号> ;. This statement is very simple. First, a label is defined through ":", and then different labels can be used to jump to different positions when writing a goto statement.

It should be noted that the above intentionally exclusive line of P1 and P2 can be used, that is:

Long a = 1;
P1: a + = 1;
P2: B * =;
Goto P2;

Therefore, it seems that "P1:" and "P2:" are a separate definition statement. To be precise, they should be statement modifiers used to define labels rather than statements, this is an error:

Long a = 1;
P1 :{
A + = 1;
P2: B * =;
P3:
} Goto P2;

Above P3: an error is reported because it does not modify any statements. It should also be noted that P1 is still 3005, that is, "{}" only serves as a combination, but does not actually generate code and does not affect the statement address.

Judgment statement -- if else, switch

If else has previously said that to achieve "what kind of action ", therefore, C ++ normally provides conditional judgment statements to execute different codes based on different conditions. If else format:

If ( <数字> ) <语句1> Else <语句2> Or if ( <数字> ) <语句1>
Long a = 0, B = 1;
P1:
A ++;
B * =;
If (a <10)
Goto P1;
Long c = B;

The code above indicates that only when the value of a is less than 10 will the jump to P1 for repeated execution. The final result is the factorial with the value of c being 10.

The above <数字> Indicates that a number can be placed in the brackets after "if", that is, the expression. When the value of this number is not zero, that is, the logic is true, the program jumps to execute <语句1> If it is zero, that is, logical false <语句2> . That is, if (? 10) goto P1;, which indicates when? If 10 is not zero, goto P1 is executed ;. This is the same as the previous results. Although c is still a ten-step multiplication in the end, it has different meanings and reduces the readability of the Code, unless it is not recommended to write the code for efficiency reasons.

While <语句1> And <语句2> Because it is a statement, you can put anything that is a statement, so you can also do this:

If (a) long c;

It can be said that the above is full, just to illustrate <语句1> Actually, you can put anything that is a statement. However, as we have mentioned earlier, the label definition, comment statement, and pre-compiled statement are not statements. Therefore, when a is not zero, defining the P2 number and writing comments when a is zero "error!" The intention is wrong:

If (a) P2: Or if (! A) // error!
A ++;

But the compiler will not report an error, because the former is actually adding a to a when a is not zero; the latter is actually adding a to a when a is zero. Note that the compound statement is also a statement, so:

If (){
Long c = 0;
C ++;
}

Because a compound statement is used, this statement does not end with ";", but it is still a single sentence, that is:

If ()
If (a <10) {long c = 0; c ++ ;}
Else
B * =;

Although the above seems complicated, it is still a single sentence. When an "else" is written, the compiler looks up for the nearest "if" to match it, therefore, the above "else" matches "if (a <10)", rather than "if (a)" because of the indentation above, therefore, B * = a; is executed only when a is greater than or equal to 10, rather than when a is zero.

Pay attention to the previously written if (a) long c ;. The meaning here is not that if a is not zero, the variable c is defined. The scope issue is described in the next article.
The definition of the switch statement is more or less because of implementation rather than logic reasons like "if else. First, let's look at its format: switch ( <整型数字> ) <语句> .

The above <整型数字> Just like the if statement, it can be a number, but it must be an integer number (the reason is described later ). Then <语句> It is the same as the preceding statement. In <语句> Use the following format: case <整型常数1> :. It defines a label at the corresponding position, that is, the things used by the previous goto statement, indicating that if <整型数字> And <整型常数 1> Equal, the program jumps to "case <整型常数1> : ". Otherwise, the subsequent statement is executed.

Long a, B = 3;
Switch (a + 3)
Case 2: case 3: a ++;
B * =;

The above indicates that if a + 3 is equal to 2 or 3, it will jump to the address of a ++, and then execute a ++. Otherwise, the following statement B * = a is executed ;. This seems ridiculous. What is the purpose? A statement is of course meaningless. To identify multiple statements, you must use a composite statement as follows:

Long a, B = 3;
Switch (a + 3)
{
B = 0;
Case 2:
A ++; // assume the address is 3003
Case 3:
A --; // assume the address is 3004.
Break;
Case 1:
A * = a; // assume the address is 3006.
}
B * = a; // assume the address is 3010

Note that the above "2:", "3:", and "1:" Are All Integer numbers, but they should be understood as labels. Therefore, check the value of a + 3 above. If it is equal to 1, jump to the address marked with "1:", that is, 3006; if it is 2, go to the place marked with 3003 to execute the Code; if it is 3, it will jump to the 3004 position to continue execution. The preceding break statement is specific. It is placed in the statement after the switch to interrupt the program. After the program jumps to the switch, the above is 3010 to execute B * = ;. You can also do this:

Switch (a) if (a) break;

Because it jumps to the corresponding position, if a is-1, a ++ is executed, a --; is executed, and break is executed; jump to the 3010 address and execute B * = ;. In addition, the above B = 0; will never be executed.

Switch indicates the value of a variable. Different values lead to different statements, which is very suitable for the selection of the implementation status. For example, 1 indicates security, 2 indicates dangerous, 3 indicates dangerous, and 4 indicates dangerous, by writing a switch statement, you can determine whether a monster should "escape", "attack", or other actions based on its current state to implement AI in the game. Isn't that strange? The preceding switch can also be implemented through the if statement. Why should we provide a switch statement? If it is just for short, why not provide more abbreviations like this logic scheme by the way, but only provide the abbreviation of a branch selection and the short abbreviation of the loop described later? Because it is proposed out of an optimization technique, just like the loop statements below, their contributions to the logic can be achieved through the if Statement (after all, the logic is judgment ), to a certain extent, they are all based on some optimization technology, but the abbreviated cyclic statements are much larger.

An array is provided. Each element of the array is 4 bytes in size. The switch statement above is as follows:

Unsigned long Addr [3];
Addr [0] = 3006;
Addr [1] = 3003;
Addr [2] = 3004;

For switch (a + 3), a similar statement can be used instead of: goto Addr [a + 3? 1];

The above is the true face of the switch. Note that the above goto statement is incorrect, which is exactly why the switch statement exists. The compiler builds an array of storage addresses for us. Each element of this array is an address, which indicates the address of a certain statement. In this way, with different offsets, you can jump to different locations to execute different statements and then select the status.

Now you should understand why the above must be <整型数字> Because these numbers are used for the subscript or offset of the array, they must be integers. While <整型常数1> It must be a constant because it tells the compiler from the compilation period that its current location should be placed in the nth element of the address array.

After understanding the implementation of the switch, when writing the switch, we should try to close the Integer constants or multiples after each case to reduce the size of the array to be generated, instead of the constant size. That is, case 1000, case 1002, case 2, case 4, and case 6 only use an array of three elements, case 0, case 100, and case 101 require an array of 102 elements. It should be noted that the current compilers are very intelligent. When we find that the latter has only three branches but requires an array of 102 elements, the compiler may use repeated if statements to replace the above array generation.

Switch also provides the Keyword -- default. As follows:

Long a, B = 3;
Switch (a + 3)
{
Case 2:
A ++;
Break;
Case 3:
A + = 3;
Break;
Default:
A --;
}
B * =;

The above "default:" indicates that when a + 3 is not 2 and not 3, a --; is executed, that is, default indicates the default status, but it can also be absent, the statement after the switch is executed directly, so this is possible: switch (a) {} or switch (a);, but it is meaningless.

Loop statement -- for, while, do while

It has just been explained that the provision of loop statements is mainly for the purpose of shorthand, because loop is the most used in method description, and the algorithm is not complex, which does not increase the difficulty of compiler development.

For ( <数字1> ; <数字2> ; <数字3> ) <语句> . The <语句> Same as above. You can use a single sentence or a compound statement. While <数字1> , <数字2> And <数字3> Because it is a number, it is an expression, and thus all the work that can be done by expression statements-the calculation of operators. The for statement is calculated first. <数字1> , Equivalent to initialization, And Then computing <数字2> . If <数字 2> If the value is zero, it indicates logical false. Then, exit the loop and execute the statement after for. Otherwise <语句> And then calculate <数字3> , Which is equivalent to the routine of every loop, and then calculates <数字2> . The above <语句> It is generally called a loop body.

The above design is a process-oriented design concept. When the circular body is regarded as a process, the initialization of this process ( <数字1> And must be executed ( <数字3> . A simple loop is as follows:

Long a, B;
For (a = 1, B = 1; a <= 10; a ++)
B * =;

After the preceding execution, B is the factorial of 10, which is much simpler and more readable than the example given in the preceding if statement -- a = 1, B = 1 is the initialization operation, and AIS is added to each loop. This information is not displayed in the goto and if statements. Because of the statements and numbers that have been repeatedly emphasized above, you can:

Long a, B = 1;
For (; B <100 ;)
For (a = 1, B = 1; a; ++ a, ++ B)
If (B * =)
Switch (a = B)
{
Case 1:
A ++; break;
Case 2:
For (B = 10; B --)
{
A + = B * B ;}
Case 3: a * =;
}
Break;
}

The above looks messy. Note that "case 3:" is in the loop body of a for statement after "case 2:", that is, when a = B returns 1, jump to a ++, and execute the switch statement because of break;, that is, the if statement, that is, the ++ a of the second for statement, ++ B. When 2 is returned, the system jumps to the third for statement to start execution. After the loop is completed, the break is used. The subsequent execution continues. When 3 is returned, the system jumps to a * = a; and then calculates B --. Then, calculates the value of B, checks whether the value is non-zero, and repeats the loop until the value of B is zero, then proceed with the subsequent execution. The above Code does not make any sense. Here it is intentionally written so confusing to further illustrate the concepts of statements and numbers mentioned above. If the code is actually executed, in the past, it was easy to know that it would be an endless loop, that is, a loop that never exits.

Note that C ++ puts forward a special syntax, that is, the preceding <数字1> It can be a variable definition statement instead of a number. for (long a = 1, B = 1; a <10; ++ a, ++ B );. The variables a and B are defined. However, you can only use variable definition statements, but the structure definition, class definition, and function definition statements cannot be written here. This syntax further defines the for statement as a Count loop. The variable definition statement here is used to define the variable that acts as the counter in this loop (the above) to achieve a fixed number of cycles.

Finally, pay attention to <数字1> , <数字2> And <数字3> Optional, that is, (;;);.

While in the format of while ( <数字> ) <语句> , Where <数字> And <语句> The same as above, which means it is obvious that when <数字> If the value is not zero, run <语句> Otherwise, execute the statement after while. <语句> It is called a loop body.

The do while format is do <语句> While ( <数字> );. Note that ";" is followed after the while clause to indicate the end of the sentence. The <数字> And <语句> The same as above, which means it is obvious that when <数字> If the value is not zero, run <语句> Otherwise, execute the statement after while. <语句> It is called a loop body.

Why do C ++ provide the above three loop statements? Shorthand is an important purpose, but more importantly, it can provide certain optimizations. For is designed to be used for a fixed number of cycles, while and do while are used for conditional loops. For the former, the compiler can map the variables previously mentioned for counting into registers to optimize the speed. The latter determines whether the optimization code can be generated based on the intelligence of the compiler.

The main difference between while and do while is that the loop body of the former may not be executed, and the loop body of the latter must be executed at least once. C ++ puts forward the continue and break statements for short purposes. As follows:

For (long I = 0; I <10; I ++)
{
If (! (I % 3 ))
Continue;
If (! (I % 7 ))
Break;
// Other statements
}

When the value of I can be divisible by 3, the following "Other statements" are not executed. Instead, I ++ is calculated directly and I <10 is calculated to determine whether to continue the loop. That is, continue terminates the execution of the current loop and starts the next loop. When the above I value can be divisible by 7, we will not execute the following "Other statements", but jump out of the loop body and execute the for statement. That is, break is used to terminate the running of the loop and immediately jumps out of the loop body. As follows:

While (-- I) do
{{
If (I = 10) if (I = 10)
Continue;
If (I> 20) if (I> 20)
Break;
// Other statements
} While (-- I );
A = I;

The above continue; during execution, the-I will be calculated immediately to determine whether to continue the loop, and break; during execution, the loop body will immediately exit and then execute the subsequent a = I ;.

You should also pay attention to the nesting problem, that is, when else looks for the paired if, it always finds the nearest if, which remains here.

Long a = 0;
P1:
For (long I = a; I <10; I ++)
For (long j = 0; j <10; j ++)
{
If (! (J % 3 ))
Continue;
If (! (J % 7 ))
Break;
If (I * j)
{
A = I * j;
Goto P1;
}
// Other statements
}

After the above continue is executed, j ++ will be calculated immediately, and break; after the execution, the second loop (that is, j's loop) will be withdrawn, and then I ++ will be executed, then it is up to I <10 to determine whether to continue the loop. When goto P1; is executed, the program jumps to the P1 above, that is, long I = a; is executed, and then the I loop is restarted.

It is not recommended to write the goto statement as above, because it breaks down the loop and does not conform to people's thinking habits. Here, we only need to explain that for, while, And do while are not loops, but their respective uses finally show like loops, which are actually just changes in the execution position of the program. It is necessary to clearly understand the implementation of statements so as to clearly understand the actual functions of various statements, and then clarify the meaning of the code written by others. However, if you write your own code and understand the implementation of statements, it will be helpful for some optimization. But when you write a program that is simplified and highly efficient, maintaining its good readability is a programmer's literacy. You should try to cultivate your habit of writing highly readable code.

The long j = 0 in the previous loop is defined multiple times when it is executed multiple times? This is the scope of the variable, which will be explained in the next article.

The content in this article should be very simple. The focus is to understand that the source code is compiled into machine commands and stored in memory during execution. Therefore, each statement corresponds to an address, the jump statement can be used to change the running sequence of the program. In the next article, we will put forward a series of concepts and focus on the significance of the types.
Related Article

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.