The strongest C language tutorial in history ---- branch and loop (1)

content

foreword

Preparation before class

Content overview:

1. What is a statement?

2. Branch statement (selection structure)

2.1if statement

2.1.1 Dangling else

2.1.2 Comparison of if writing forms


foreword

For a C language rookie, it is undoubtedly a shame to say these words, but I have the confidence and ability to speak the C language for everyone, and systematically guide you to the C language. I also hope that everyone can believe me, support me, and answer In the next few months, I will continue to update this series, of course, my notes will be updated synchronously, thank you for your support! If you think the content is good, please give a small like and support!

Preparation before class

Content overview:

branch statement

if

switch

loop statement

while

for

do while

I believe everyone has heard a sentence: C language is a structured programming language, but why do you say that? Think about what we do in our life, there are three situations, the language expression is not very good, let me show you a picture first!

 This picture is really not very good, but I think everyone should be able to understand what I mean. These three methods of doing things or solving problems also correspond to the three structures in the C language (I have to sigh the original programming design language. Abstract generalization ability), this place needs to tell you that there are only these three structures in the C language , and we are talking about branch and loop statements today, which correspond to branch and loop structures, so why don’t I talk about sequential structures? ?

Because in the computer, without the participation of branch and loop structure, all programs are the default sequential structure, that is, the program is executed sequentially, so the sequential structure is not necessary. The basis of programming is also the basic internal logic of the computer. To a certain extent, in the process of branching and selecting structures, the sequence structure is also being carried out.

1. What is a statement?

C language statements can be divided into the following five categories:

(1) Expression statement (eg y=x+3; assuming that variables y and x are defined)

(2) Function call statement (MAX(x,y); assuming that the function MAX() has been defined)

(3) Control Statement

(4) Compound statement (a statement formed by compounding multiple statements)

(5) Empty statement (for example; a semicolon itself can be used as a statement, called an empty statement. As for the role of an empty statement, we will talk about it later)

Either of the above statements must end with a semicolon!

The branch and loop statements introduced today are control statements.

Control statements are used to control the execution flow of the program to realize various structural modes of the program. They are composed of specific statement definers. There are nine control statements in C language.

It can be divided into the following three categories:

1. Conditional judgment statement (also called branch statement): if statement, switch statement;

2. Loop execution statements: do while statement, while statement, for statement;

3. Turn to statement: break statement, goto statement, continue statement, return statement.

2. Branch statement (selection structure)

You meet a girl you like, if you confess, she becomes your girlfriend and you are with her;

If you don't confess, she becomes someone else's girlfriend, and you watch her lying in someone else's arms and regret it.

This is the choice!

 

 Of course, there are also multiple options. If your confession is successful, she becomes your girlfriend; on the contrary, if you fail to confess, she says: You are a good person, if you do not confess at all, then she eventually becomes someone else girlfriend. (If you meet someone you like, you must be brave and confess!!)

2.1if statement

What is the grammatical structure of the if statement?

If the expression in the brackets of if() is true, the following statement will be executed; if it is false, the following statement will not be executed, and the statement following the else will be executed, then what is true? What is fake?

According to the C language, 0 is false and non-0 is true.

Let me show you the code below!

The first is the single-branch case:

Then there is the double branch case:

 Next is the multi-branch case:

Hey, many students want to ask when they see this, is it possible to not write the previous age>=18 in the later conditional judgment in the case of multi-branch? Obviously, of course it is possible, because when age<18 is not satisfied before, age must be greater than 18 at this time, then according to reason, there is no problem if we do not write age>=18 , but this place, It is recommended that students still want to add it, why? We will write code in the future, not only for ourselves, but also for others to see. After adding it, the structure will be very clear and clear, which is convenient for others to view, and also conducive to our later maintenance and debugging.

Seeing this, I believe that students will also have such questions, that is, can we write the conditional expression (age>=18&&age<60) like this? (18<=age<60) I believe that everyone will have such doubts. In fact, it is normal to have such doubts, because we write like this in mathematics, but is it okay to write like this? Let's go straight to the code!

 

When the students saw the first picture, hey, I felt that there was no problem, but when they saw the second picture, something went wrong! Why does this problem occur? Next, I will analyze it for you! We know, > >= < <= == , these operators are relational operators, if the result is true, it returns 1, if the result is false, it returns 0, and the combination direction is from left to right (refer to about The combination direction of operators, I will share the combination direction of various operators at the end of this article in the form of pictures) , in the second example, we input 100, first of all, after the first if It is obvious that 100>18, does not meet the condition, and then enters the first else if statement to judge, 18<=100, is true, returns 1, and then 1<60, is true , in the end, the result of returning 1 is valid, so "youth" will be output. I believe everyone will understand it after seeing it. It cannot be written in this way in a computer. Although there is no problem in grammar, it cannot express us logically. Means, can not meet our requirements, so don't use it like this !

Seeing that many students have problems again, is it okay for us to directly follow a variable after if()? Yes, no problem at all! Because it has been said above, the c language stipulates that 0 is false and non-0 is true, so there is no problem with if() directly followed by variables ! If the variable value is not 0, then the judgment result is true, and the following statement is executed. If the variable value is 0, then the judgment result is false, and the latter statement is not executed.

At the same time, let me mention by the way that the return value of the assignment expression is the assigned value itself , that is, in if(i = n) (n is a constant), if n is 0, then the return value is 0, if n is not 0, then the return value is n, that is, the return value is not 0. If it is true, the following statement will be executed. At the same time, I will tell you that the printf and scanf functions also have return values. You can check it yourself after class!

If you want to have a deeper understanding of if, you can read my other blog about if, that article has a more in-depth exploration of if!

If the condition is true, to execute multiple statements, you should use a code block! In fact, I've used code blocks all over the place, so what are code blocks? In the C language, the code blocks enclosed by {} are called code blocks. Next, I will show you the code, what will happen if you don't use {}!

 

Everyone has seen that if we want to follow the if with multiple statements, we will make an error without {}, which is determined by the C language syntax itself, because in this case, there will be no matching if for the following else. By default, if can only be followed by one statement, and the computer treats the statement in {} as a statement , so if we want to follow the if with multiple statements, we must use {} to form a statement block .

Just to show you the code:

 My advice to you all. That is, no matter how many statements are followed, we add {} . Why do we say this? The first reason is to avoid that we may forget to add {} when we follow multiple statements after if; the second reason is The code block is more concise and organized; the third reason is that it is convenient for us to add code to it later.

2.1.1 Dangling else

Here is a piece of code for you, I hope you can give the output result!

I believe that some of you will give the output of "hehe", some will give the output of "haha", of course, some will give some other results, everyone has their own ideas, this does not make people Surprise, then let me tell you that the output result is blank, will you be surprised? You may think that what I said is false, then I will show you the code!

Yes, the output result is indeed blank. This result is really surprising. The knowledge point we need to learn in this question is that else always matches the nearest If! 

How to understand this sentence? Let's take the following code as an example to explain it to you!

First of all, if you only look at this picture, it is easy to misunderstand that the following else will match with if (a==1), but we should remember that else conforms to the principle of proximity, that is, it always matches the nearest unpaired if , Look forward to the previous if statement from this else. The first unpaired if statement found is if(b==2). I believe it is not difficult for everyone to understand this. Let me show you the original code face!

Seeing this, everyone should be able to understand it. First of all, if (a==1) is judged, it is not true, and then the following if (b==2) and else statements are not executed. We can understand it like this: if and else consists of a statement.

In fact, we can improve this code to make it clearer and easier for us to understand:

Wouldn't it be more helpful to add a code block like this to everyone's understanding? So code specification is very important!

In this place, I hope everyone can develop a good code writing style, so that the code is beautiful, compliant, and easy to understand! Here I recommend a book called "High-Quality C-C++ Programming", which contains a lot of good code writing styles and some good habits. You can go to the Internet to find it, and you should be able to find the electronic version. , If I have time, I will also write some related blogs based on the content of the book! If you really can't find it, let me know in the comment area and I will share it in the comment area.

2.1.2 Comparison of if writing forms

First of all, let's look at code 1 and code 2. They express exactly the same meaning, that is, if the condition is true, execute return x; the statement returns x, otherwise, execute return y; return y, but code block 2 expresses the meaning. It is easier to understand than code block 1, and the logic is clearer. I hope everyone can write code like code 2 when writing code!

//代码1

if (condition) {
    return x;
}

return y;

//代码2

if(condition)
{
    return x;
}

else
{
    return y;
}

//代码3

int num = 5;

if(num == 5)
{
    printf("hehe\n");
}

//代码4

int num = 5;

if(5 == num)
{
    printf("hehe\n");
}

Next, let's look at code 3 and code 4. They express exactly the same meaning, so why should I tell you this point? It is because there are always many students writing if(num==5) so that they are written as if(num=5). Note that although the results of the two are the same (whether the latter expression is executed), the meaning they express But it is completely different. The former is for judgment, while the latter is for assignment. After executing this statement, the value of num in the former has not changed, and the latter, regardless of whether the original value of num is 5, is executed after the if After (num=5), the value of num will become 5.

Their judgment results are indeed the same, why? As I said before, the return result of the assignment expression is the value assigned, and the value assigned in the second statement is 5, so the return value is 5, which is a non-zero number, which is true, and the former is because num itself is equal to 5, so the judgment result is true, both are true, and the following statements are executed.

Although the second of these two ways of writing is wrong, the result is indeed correct, but the consequences are completely different. I hope everyone can pay attention!

Then I will tell you why I want to show you code 3 and code 4. According to our normal logic, we should write it like code 3, but as I told you before, it will be easy for us to write code 3 like this. Write it as if(num=5), which changes the value of the variable, so why write code 4 like this? Just to avoid making such mistakes! Show everyone the code and everyone will understand!

 

 In this case, if we still write in the wrong way, it is obvious that the program will report an error, why is it reporting an error? We can only assign values ​​to variables, that is, when we write an assignment statement, the left side of = must be a variable, not a constant! In this way, once the compiler prompts us that the program is wrong, as soon as we check it, we know that we have made an old mistake again, and we can correct it in time! Therefore, it is recommended that you put constants on the left side of relational operators when writing conditional judgment expressions in if()!

Well, today's branch and loop (1) is here. If you think it's good, please give a little like and follow, and we will continue to update it later!

The operator associative sequence list shared with you:

 

 

 

Guess you like

Origin blog.csdn.net/m0_57304511/article/details/120982188