Branching and Looping Statements - 1

Old irons, this is the blogger's first C language learning blog after he first met C. I hope it can help you.

Article directory

1. What is a sentence?

Two, branch statement

1. if statement

2. switch statement

Three, while loop

1. What is a sentence?

C statements can be divided into the following five categories:

1. Expression statement

2. Function call statement

3. Control Statements

4. Compound statements

5. Empty statement

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. C language has nine control statements.

Can be divided into the following three categories:

1. Conditional judgment statements are also called branch statements: if statement, switch statement;

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

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

Two, branch statement

1. if statement

Grammatical structures:

if(expression)    

              statement;

if(expression)    

              Statement 1;

else    

              Statement 2; //Multi-branch    

if(expression1)    

              Statement 1;

else if(expression2)    

              Statement 2;

else    

              Statement 3;

Let's take these three pieces of code as an example to understand single-branch and multi-branch:

If the expression evaluates to true, the statement executes. 

0 means false, non-zero means true.

#include <stdio.h>
int main()
{
 int age = 0;
    scanf("%d", &age);
    if(age<18)
   {
        printf("未成年\n");
   }
}
#include <stdio.h>
int main()
{
 int age = 0;
    scanf("%d", &age);
    if(age<18)
   {
        printf("未成年\n");
   }
    else
   {
        printf("成年\n");
   }
}
#include <stdio.h>
int main()
{
 int age = 0;
    scanf("%d", &age);
    if(age<18)
   {
        printf("少年\n");
   }
    else if(age>=18 && age<30)
   {
        printf("青年\n");
   }
    else if(age>=30 && age<50)
   { 
        printf("中年\n");
   }
    else if(age>=50 && age<80)
   {
        printf("老年\n");
   }
    else
   {
        printf("老寿星\n");
   }
}

 If a condition is true and multiple statements are to be executed, a code block should be used.

A pair of { } here is a code block.

Dangling else question:

#include <stdio.h>
int main()
{
    int a = 0;
    int b = 2;
    if(a == 1)
        if(b == 2)
            printf("hehe\n");
    else
        printf("haha\n");
    return 0;
}

The else matches his nearest if, so this code prints nothing.

Here we mention the code specification issue:

#include <stdio.h>
int main()
{
    int a = 0;
    int b = 2;
    if(a == 1)
   {
        if(b == 2)
       {
            printf("hehe\n");
       }
   }
    else
   {
         printf("haha\n");
   }       
    return 0;
}

In this way, enclosing it with { } will not cause this kind of problem.

It is very helpful to develop a good coding style.

2. switch statement

The switch statement is also a branch statement.

Often used in multi-branch situations.

switch(integer expression)

{    

          statement item;

}

Statement item:

// are some case statements:

//as follows:

case integer constant expression:    

           statement;

In the switch statement, we can't directly realize the branch, and we can realize the real branch by using it with break.

#include <stdio.h>
int main()
{
    int day = 0;
    switch(day)
   {
        case 1:
            printf("星期一\n");
            break;
        case 2:
            printf("星期二\n");
            break;
        case 3:
            printf("星期三\n");
            break;    
        case 4:
            printf("星期四\n");
            break;    
        case 5:
            printf("星期五\n");
            break;
        case 6:
            printf("星期六\n");
            break;
        case 7:
            printf("星期天\n");    
            break;
   }
    return 0;
}

 Break is to jump out of the current statement and execute the following statement.

Sometimes our needs change, such as:

1. Input 1-5, the output is "weekday";

2. Input 6-7, output "weekend"

So our code should be implemented like this:

#include <stdio.h>
//switch代码演示
int main()
{
    int day = 0;
    switch(day)
   {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            printf("weekday\n");
            break;
        case 6:
        case 7:
            printf("weekend\n");
            break;
   }
    return 0;
}

 Add a break statement after the last case statement.

(The reason for writing this way is to avoid forgetting to add a break statement after the last case statement).

What if the value of the expression does not match the values ​​of any case labels?

In fact, nothing, the result is that all statements are skipped.

The program does not terminate, and no error is reported, because this situation is not considered an error in C.

But what if you don't want to ignore the value of an expression that doesn't match all tags?

You can add a default clause to the statement list by putting the following label

default:

Write anywhere a case label can appear.

When the value of the switch expression does not match the values ​​of all case labels, the statements following the default clause will be executed.

Therefore, only one default clause can appear in each switch statement.

But it can appear anywhere in the statement list, and the statement flow will execute the default clause as if it were a case label.

The switch statement allows nesting, and break jumps out of the currently executing switch statement.

Three, while loop

When we need to do the same thing many times:

We do this with a loop statement.

//while syntax structure

while(expression)

        loop statement;

Let's explain it with a piece of code:

 

From this code, we can see that break can still be used in the loop.

The break in while is used to permanently terminate the loop. 

The function of continue in the while loop is:

continue is used to terminate this cycle, that is, the code after continue in this cycle will not be executed again, but directly jump to the judgment part of the while statement to judge the entry of the next cycle.

#include <stdio.h>
int main()
{
    int i = 1;
 while(i<=10)
 {
    if(i == 5)
   {
        continue;
   }
    printf("%d ", i);
    i = i+1;
 }
 return 0;
}

After this code prints to 4, there will be an infinite loop. 

Thank you for reading, I hope my article is helpful to you. If the blogger's article is helpful to you, please pay attention to it, like it, and support the blogger. Thank you for your attention and likes.

Guess you like

Origin blog.csdn.net/LXW0403/article/details/130182226