[Detailed explanation of branch and loop statements in C language 1] Take you through branch statements

Table of contents

1. What is a statement?

2. Classification of statements

3. Branch statement (selection structure)

1. if statement

2. Hanging else

4.swich statement

 break:

default:

Summarize:


1. What is a statement?

In C language, a sentence ending with a semicolon is called a statement.

2. Classification of statements

In C language, various statements are classified, including the following statements:

1. Expression statement 2. Function call statement 3. Control statement 4. Compound statement 5. Empty statement

The following will focus on control statements. Control statements are used to control the execution flow of the program to implement various structural methods of the program. They are composed of specific statement definers. There are nine types of control statements in C language :

Can be divided into the following three categories:

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

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

3. Turning statements: break statement, goto statement, continue statement, return statement.

3. Branch statement (selection structure)

1. if statement

The basic structure of the if statement is as follows:

//表达结构:
    if(表达式1) //括号里放能判断真假的表达式或变量
               //真就执行语句1,假就跳过。     
        语句1;
    if(表达式2)
        语句2;
    else
        语句3;

//多分支结构:
//程序会先判断if中条件是否为真,接着是else if,
//如果if和else if都为假,就执行else中的语句

    if(表达式1)
        语句1;
    else if(表达式2)
        语句2;
    else
        语句3;

ps: (1) In C language, 0 is false, and non-0 is true.

     (2) In a two-branch or multi-branch statement, after one of if, else if or else is true, the other statements will not be executed.

Warm reminder: In C language, statements are judged from left to right, such as when judging whether a number is between 10 and 20. We cannot write if(10 < x < 20). Instead, write if(10 < x && x < 20). We can test it with the following procedure:

 We can see that the result of the program running is awsome printed out. We judge that the condition in the if is false. Why does awsome still print out?

We can do a simple analysis: x = 22, first judge 10 < 22; the result of this expression is true, which is 1; then continue to judge 1 < 20; it is true. So the statement in the if is executed. Print out awesome.

It turns out that in C language, it is not possible to judge whether a formula meets two or more conditions. If you want to judge, you need to  connect them with &&  or  ||  .

When we want to execute multiple statements in a branch statement after the condition is true, we can use our statement block:

#include <stdio.h>
int main()
{

    if(表达式1)
    {
        语句1;
        语句2;
    }
    else
    {
        语句1;
    }
    return 0;
}

The statements in a pair of { } below if or else are called statement blocks.

2. Hanging else

Let's look at a piece of code below:

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

You might as well guess what the result of this line of code is? Should I print nice or good?

We can take a look at the running results:

 Nothing is output as a result. Because the else statement here does not belong to the first if statement, when we encounter a dangling else statement, it belongs to the incomplete if statement closest to it . If you want it to belong to the first if, you need to complete the second if statement and add an empty else statement, or surround it with a { } within a code block.

Modifications are given here:

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 2;
	//第一种
	//if (a == 1)
	//{
	//	if (b == 2)
	//		printf("nice\n");
	//}

	//第二种
	if (a == 1)
		if (b == 2)
			printf("nice\n");
		else
			;
	else
		printf("good\n");
	return 0;
}

4.swich statement

The switch statement is also a branch statement and is often used in multi-branch statements.

Contains the following keywords: switch berak case default

The basic format of switch is as follows:

switch(整型表达式)
{
    语句项;
}
int main()
{
    int n = 0;
    scanf("%d",&n);
    switch(n)
    {
        case 1:
            //需要执行的语句
            break;
        case 0:
            //需要执行的语句
            break;
        default:
            //若输入的n和前面所有case均不符合
            //就执行此部分
            break;
    }
    return 0;
}

in switch statement

n in switch(n) must be an integer expression.

The case must be followed by an integer constant expression.

The parts of the case are called statement items.

 break:

break: Each statement item in switch must contain break, which can help us achieve true multi-branching. When the switch statement is executed, it will look for cases that meet the conditions from top to bottom based on the value of the expression. When a case that meets the conditions is found, he will execute the content in the case. However, if we do not add break after it, the program will continue to execute subsequent cases until it encounters a beak.

for example:

 Although the input of our code is 0, the program does not encounter break after case 0:, so it will continue to execute until it encounters break or the switch branch ends.

default:

default: What if the value of the expression does not match the values ​​of all case tags ? At this time, the program will skip all statements . But what if you don’t want to ignore values ​​of expressions that don’t match all tags? You can add a default clause to the statement list. When the value of the switch expression does not match the values ​​of all case labels, the statement following the default clause will be executed.

Give a chestnut:

Many-to-one situation in switch statement:

In a switch statement, multiple case labels can correspond to the same execution statement. as follows:

int main()
{
	int n = 0;
	scanf("%d", &n);
	switch (n)
	{
	case 0:
	case 1:
		printf("bbbbb\n");
		break;
	default:
		printf("ccccc\n");
		break;
	}
	return 0;
}

We can see it by running:

 PS:

1. In the switch statement, the position of the default clause is not fixed. It is written in any position where a case label can appear.

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

3. Only one default clause can appear in each switch statement.

4. It can appear anywhere in the statement list, and the statement flow will execute the default clause just like executing a case label.

5. Switch can also have many-to-one situations.

6. Switch statements can be nested.

Summarize:

The above is the knowledge about branching in C language. I hope it can help everyone. Of course, if there is anything incorrect, please correct me.

Guess you like

Origin blog.csdn.net/m0_74459723/article/details/127531668