C language branch and loop statement

Table of contents

1. Branch statement

1. if statement

 2. switch statement

 Note on the switch statement:

 Second, the loop statement

continue:

1. while loop

2. for loop

3. do.......while loop


1. Branch statement

1. if statement

There are several forms of the if statement, and I will give you examples one by one below:

(1)、

if(expression)

     statement;

The first one, there are no curly brackets {} after the if statement , so only one statement can be added , not multiple statements. For example, the understanding of the following code is wrong

Enter the above statement without braces, it may be understood that if a>5, then output a and a+1, if not greater than, then no output, right? In fact, this understanding is correct after adding braces, if not Increase the brackets, then if can only be followed by one statement, the real meaning is that if a>5, then output a, and then the following output a+1 has nothing to do with the constraints of the if statement, so it will eventually output a + 1 , that is, the value of 5, as shown in the figure:

Everyone should remember the points that need to be paid attention to in this kind of if statement without braces. If you don’t want to accidentally fall into the pit, it is recommended to write the if statement at ordinary times. It is best to add curly braces habitually.

(2)、

if(expression1)

     Statement 1;

if(expression2)

     Statement 2;

This form of if statement needs to judge whether expression 1 and expression 2 are satisfied one by one. If expression 1 or expression 2 is satisfied, then execute statement 1 and statement 2 corresponding to expression 1 and expression 2, and there is no other Points to note.

(3)、

if(expression)

    Statement 1;

else

    Statement 2;

For the second type of if statement, it means that if the condition in the parentheses after if is met, statement 1 is executed, and if the condition after if is not satisfied, statement 2 is executed. An example helps to better understand:

  As shown in the figure above, it can be clearly seen that a satisfies the condition after the if statement, so the output a>4, the above is the place to pay attention to in the second if statement

(4)、

if(expression1)

    Statement 1;

else if(expression2)

    Statement 2;

else

    Statement 3;

In the third case, understand that if the expression 1 after if is satisfied, execute statement 1, if expression 1 is not satisfied, then judge whether expression 2 is satisfied, if satisfied, execute statement 2, otherwise execute expression 3 .

The following example illustrates:

As shown in the figure above, first judge whether a is equal to b, if not, then judge whether a is greater than b, if a is not greater than b, then execute the statement in the last else.

That's all for the if statement. I believe everyone understands the four different expressions of the if statement. 


 2. switch statement

First understand the syntax of the switch statement:

switch(integer expression)

{statement item;}

There are two types of statement items:

case integer constant expression:

        Statement 2;

default:

        Statement 3;

The above is the specific syntax of the switch statement, where default is the condition that all the case statements are not satisfied, the statement 3 after the default will be executed, but the default can be added or not , and it will be more convenient to use with the default.

Next, an example will be given to illustrate the usage of the switch statement in more detail:

 As shown in the figure above, inputting 1, 2, and 3 will output the corresponding statement. If the input is not 123, it will print "input error", but have you noticed that a break is added after each case statement and default statement; this It is because if no break is added, the program will continue to execute the next line of code after executing the corresponding code. For example, if no break is added, the following situation will occur:

 Note on the switch statement:

After executing the statement in case 2:, the statements in case 3 and default are all executed one by one, so every time you write a switch statement, you must pay attention to adding a break at the end of each case statement; to avoid the above picture Error shown in ! !


 Second, the loop statement

In the topic, you may encounter break and continue in the loop. The usage of these two is actually very simple: break:

If a break is encountered in the loop statement, it will jump out of the loop immediately, and the subsequent statements do not need to continue to execute, and there is no need to continue to judge whether the condition of the expression is satisfied, that is, the loop is permanently terminated!

continue:

If continue is encountered in the loop statement, then skip the part behind this loop and jump directly to the judgment part to judge whether the next loop is needed!

1. while loop

The syntax of while loop is as follows:

while(expression)

          {loop statement;}

See if the syntax of the while loop is very simple. In fact, the while loop is to judge whether the condition of the expression is satisfied. If it is satisfied, the loop statement is executed . After the loop statement is executed , it is judged whether the expression is satisfied . If it is satisfied Then continue to execute, if not satisfied, then jump out of the loop.

Let's take an example:

 As can be seen from the above, i=5, i<8, enter the loop, then i++, and so on, loop three times, print out three and enter the loop.

Next is the case of a while loop with a break :

After entering the loop printing for the first time, i++ becomes 6, and the second time entering the loop i==6 enters the if statement, and then break jumps out of the loop, so this situation is only printed once, everyone knows how to use it.


2. for loop

The syntax for a for loop is as follows:

for(expression1;expression2;expression3)

      loop statement;

It should be noted that do not modify the loop variable in the for loop , because it will affect the number of loops!

It is recommended to use the form of closing before opening , such as i=0; i<5; the advantage of this is that if <5 in the judgment condition, it will cycle 5 times, and it will be more convenient without counting the number of cycles .

Among them, expression 1 is initialization, expression 2 is condition judgment, and expression 3 is adjustment value. Here is an example with ccontinue :

Originally i = 0; i < 3; is the loop 3 times, thus printing 3 times "enter the loop!", but in the third loop i = = 2, it entered the if statement, which executes continue, jump After the next print statement, jump directly to the condition part, i++ becomes 3, and if i<3 is not satisfied, the loop cannot enter, so only two "enter the loop!" are printed.

One more thing to note:

If the judgment part in the for loop is omitted, the judgment result will always be true!

For example, in the above example, if the expression 2:i<3 is omitted, the program will loop infinitely because there are no restrictions.


3. do.......while loop

The basic syntax of the do.......while loop is as follows:

do

   loop statement;

while(expression);

The difference between this loop statement and the previous two is that the previous for loop and while loop both judge whether the condition is met before deciding whether to enter the loop, while the do....while loop enters the loop once, Then judge whether to enter the cycle again!

So the number of do....while loop cycles is greater than or equal to 1! ! !

For example:

i=0 itself does not satisfy the condition of i>0, but the do.....while loop enters the loop first and then judges the condition, so it prints once first, and jumps out of the loop if the judgment condition is not satisfied.

The do.......while loop is relatively simple, but everyone should pay attention, there is a semicolon after the while , don't forget it! 


This time the branch loop statement is over, see you next blog ^_^ ^_^

Guess you like

Origin blog.csdn.net/m0_64411530/article/details/123907991