C program structure

Chapter 4 C program structure statement

This chapter mainly explains the program structure of C language, including three basic sentence structures: order, selection, and loop.

Simple if statement of branch structure

I also remember when I was a child, every time I took the exam, my parents always said that if you can get 100 points, you will buy a toy. It is not difficult to see that this is a judgment sentence, then how to express this judgment statement in C language? This requires the if conditional statement in the branch structure statement in the C language.

The basic structure of a simple if statement is as follows:

The semantics are: if the value of the expression is true, then the statement is executed, otherwise, the statement is not executed. The process can be expressed as the following figure

Then if the test score is 100 points, buy toys. With the code, you can express it as:

Note: if there is no semicolon after if(), write directly{}

Simple if-else statement of the branch structure

Before the exam, the parents will say that if you take 100 points, you can buy toys. Then we will ask if it can’t test 100 points. Parents will say that they can’t test it at this time. It doesn’t matter if they make persistent efforts next time, but there is no reward.

Then, like the above situation, the if-else statement is used in the C language. The basic structure of the simple if-else statement is as follows:

The semantics are: if the value of the expression is true, then block 1 is executed, otherwise block 2 is executed. The execution process can be expressed as the following figure:

If the test score is 100 points, buy toys, otherwise there is no reward. The code can be expressed as:

Note: if there is no semicolon after if(), write {} directly, there is no semicolon after else, write directly {}

Multiple if-else statements for branching structures

Before the exam, the parents said, if you take the test for 90 points or more, buy a toy. If you test between 70 and 90, please have a big meal, otherwise there will be no reward.

So for this multiple condition, multiple if-else statements are used in C, and the structure is as follows:

The semantics are: determining the value of the expression in turn, and when a certain value is true, executing the corresponding code block, otherwise executing the code block n. The execution process of the if-else-if statement is shown in the following figure:

Then the above parents said, using the code to achieve the following:

Note: When a condition is true, other statements of the branch structure are not executed down.

Nested if-else statement of branch structure

Before the exam, the parents said, if you test 90 points or more, you will buy toys, otherwise there will be no rewards. If you can get the top three in the class if you get the toys, you will have a big meal.

So if you want to satisfy all the parents, you need to use nested if-else statements in C language. The meaning of nested if-else statements is to write if-else statements in if-else statements. Its general form is:

The above parents used the code internship as follows

while loop structure

When John was in the English test, the word “computer” was wrong. In order to remember the word, John had to copy 10 times and then copied it 10 times in a bitter and bitter manner. Here we can say that John is copying in a loop.

Then we call this kind of repeated execution of a certain action as a loop. There are three loop structures in the C language. Let’s take a look at the structure of the C language while loop.

The expression represents a loop condition and the execution code block is a loop body . The semantics of the while statement is: the value of the expression is evaluated, and when the value is true (non-zero), the loop body code block is executed. The execution process can be represented by the following figure:

John now wants to be lazy and does not want to copy it. He uses the code to achieve the following:

Printf (“%d: computer\n”,i);

}

The following points should be noted when using the while statement:

1. The expression in the while statement is generally a relational expression or a logical expression. When the value of the expression is false, the loop body is not executed. Otherwise, the loop body is executed all the time.

2, must remember to change the value of the loop variable in the loop body, otherwise, there will be an infinite loop (endless execution).

3. If the loop body includes more than one statement, it must be enclosed in {} to form a compound statement.

Do-while loop

John’s English test, the word “computer” was once again wrong, and John decided to copy it 10 times, but John thought, “I can remember it once I write it, and the rest is left to the computer to help me!.

For the idea of John, you can use the do-while loop in C language. The general form is as follows:

Do

{

Statement

} While (condition)

The semantics of a do-while loop statement is that it executes the execution code block in the loop first, and then determines whether the expression in the while is true. If it is true, it continues the loop; if it is false, it terminates the loop. Therefore, the do-while loop must execute at least one loop statement . The execution process can be represented by the following figure:

According to the characteristics of the do-while statement, John’s code is implemented as follows:

Printf(“%d : computer \n”,i);

} While(i<10);

Note: When using a do-while structure statement, there must be a semicolon after the while brackets.

For loop

There is also a more flexible loop in the C language, which can completely kill the first two loop structures because it is more direct and simple than the first two loop structure syntax. Its general form is:

Its implementation process is as follows:

The first step: execute expression 1, initialize the loop variable;

The second step: judge expression 2, if its value is true (non-zero), execute the code block in the for loop body, and then execute downward; if its value is false (0), the loop ends;

The third step: execute expression 3;

Step 4: Execute the second step after executing the code block in the for loop;

Step 5: The loop ends and the program continues to execute downwards.

The execution process can be represented by the following figure:

Example: John is determined to copy the word 10 times computer, using the for loop to achieve the following:

Printf(“%d:Computer\n”,i);

}

Note: the semicolon in the for loop must be written

In a for loop, expression 1 is one or more assignment statements that control the initial value of the variableexpression 2 is a relational expression that determines when to exit the loop; expression 3 is the step of the loop variable The value defines how the control loop variable changes after each iteration. These three parts are separated by a semicolon (;) .

Use the for statement should pay attention to :

1. The expressions 1, 2, and 3 in the for loop can default, but the semicolon (;) cannot be defaulted.

2. Omit “Expression 1 (loop variable initial value)”, indicating that no initial value is assigned to the loop variable. Such as:

Printf(“%d:Computer\n”,i);

}

4, omitting “Expression 3 (loop variable increment)”, no other processing, the loop is always executed (infinite loop). Such as:

Printf(“%d : Computer\n”,i);

}

Note: Infinite loops can be solved using the break that will be discussed later.

5. Expression 1 can be an assignment expression that sets the initial value of a loop variable, or it can be another expression. Such as:

6. Expression 1 and Expression 3 can be a simple expression or multiple expressions separated by commas. Such as:

The result of the operation is

7. Expression 2 is generally a relational expression or a logical expression, but it can also be a numeric expression or a character expression, and the loop body is executed as long as its value is non-zero.

8. The variables in each expression must be defined before the for a loop. Such as:

Print a 9×9 multiplication table using a for loop

Please use the for loop to print the 9*9 multiply table in reverse order. The printed result is shown below:

http://img.mukewang.com/553499380001035e04880145.jpg

Program analysis:

Observe the numbers on both sides of the multiplier. The former is decremented and the latter is incremented.

Leave a Reply

Your email address will not be published. Required fields are marked *