C language - selection structure (branch statement)

Table of contents

1. Select structure

2. Use of selection structures

2.1 Single branch selection structure

2.2 Two-branch selection structure

2.3 Multi-branch selection statement

2.3.1 Realize multi-branch selection statement through else if

2.3.2 switch statement realizes multi-branch selection statement 

2.4 Nested branch statements


1. Select structure

The selection structure is used to judge a given condition, and the next execution process is determined by the result of the judgment.

Common selection structures include single-branch selection structure, double-branch selection structure, multi-branch selection structure and nested branch structure

 

2. Use of selection structures

2.1 Single branch selection structure

Grammatical structures

if(expression)

        statement;

 

Implementation process

 

Example: Check if a number is even

#include <stdio.h>

int main()
{
	int input = 0;
	printf("请输入一个数");
	scanf("%d", &input);

	//判断输入的数是否为偶数
	if (input % 2 == 0)
	//若不加花括号则默认只执行if语句后面一条语句
	{
		printf("输入的数为偶数\n");
	}

	return 0;
}

2.2 Two-branch selection structure

 Grammatical structures

if(expression)
  statement 1;
else
  statement 2;

 

 Implementation process

 

 

Example: Determine if a number is even or odd

#include <stdio.h>

int main()
{
	int input = 0;
	printf("请输入一个数");
	scanf("%d", &input);

	//判断输入的数是否为偶数
	if (input % 2 == 0)
	{
		printf("输入的数为偶数\n");
	}
	else
	{
		printf("输入的数为奇数\n");
	}
	return 0;
}

Note:

If and if else statements, {} may not be written behind, and only the statement after if else will be executed at this time

	//代码1
	if (condition)
		return x;
	return y;

	//代码2
	if (condition)
	{
		return x;
	}
	else
	{
		return y;
	}

The execution results of code 1 and code 2 are the same. In contrast, the logic of code 2 is clearer and less error-prone 

 {} can execute multiple statements in the code block, and the logic of the code is clearer. It is recommended to add {} when using

2.3 Multi-branch selection statement

2.3.1 Realize multi-branch selection statement through else if

Grammatical structures

 if (expression 1)
  statement 1;
else if (expression 2)
  statement 2;

else if(expression3)

  statement 3;
else
  statement 4;

Implementation process

Example: Determine whether a number is positive, negative or 0

#include <stdio.h>

int main()
{
	int num = 0;
	printf("请输入一个数");
	scanf("%d", &num);

	if (num > 0)
	{
		printf("输入的数为正数\n");
	}
	else if (num < 0)
	{
		printf("输入的数为负数\n");
	}
	else
	{
		printf("输入的数为0\n");
	}

	return 0;
}

Note:

1. The selection structure is matched from top to bottom. Once a certain condition is matched, the entire conditional statement ends.

Even if the condition can be matched later, it will not be executed again .

2. You can not write else after using if else if

2.3.2 switch statement realizes multi-branch selection statement 

 The switch statement is often used in multi-branch situations.

When there are many branches, the form of using the else if statement is more complicated. At this time, we can use the switch statement to achieve

Grammatical structures

switch(integer expression){

        case constant expression 1:

                Statement 1;

                Statement 2;

                 ...

                break;

        case constant expression 2:

                Statement 1;

                ...

                break;

        case constant expression 3:

                statement 1

                ...

                break;

        default:

                default statement

                break;

}

After calculating the value of the integer expression, compare the obtained value with the value of the constant expression after each case one by one. When the value of the expression is equal to the value of a certain constant expression, the following statement is executed. Until a break statement is encountered . If the value of the expression is different from the constant expressions after all cases, the statement after default is executed.

(default is equivalent to else, if it does not match all the above conditions, execute default)

Example 1:

#include <stdio.h>

int main()
{
	int day = 0;
	scanf("%d", &day);

	switch (day)
	{
		//若不加break,则会向下执行后面的语句,直到遇见break
	case 1:
		printf("星期一\n");
	case 2:
		printf("星期二\n");
	case 3:
		printf("星期三\n");
	case 4:
		printf("星期四\n");
	case 5:
		printf("星期五\n");
		break;
	case 6:
		printf("星期六\n");
	case 7:
		printf("星期天");
	default:
		printf("输入错误!\n");
	}

	return 0;
}

operation result

 

 Example 2:

#include <stdio.h>

int main()
{
	int day = 0;
	scanf("%d", &day);

	switch (day)
	{
	case 1:
	case 2:
	case 3:
	case 4:;
	case 5:
		printf("工作日\n");
		break;
	case 6:
	case 7:
		printf("周末\n");
		break;
	default:
		printf("输入错误!\n");
		break;
	}

	return 0;
}

Note: 

1. The switch statement is different from the if statement, it can only judge whether the value of the expression is equal to the specified constant, while if can calculate and judge various expressions

2. The switch statement must be followed by an integer expression

3. There can be any number of case statements in a switch, and the case must be followed by a constant

4. default can be omitted

5. The order of case and default can be reversed, and attention should be paid to the following break statement when reversed.

6. It is recommended to add a break statement after the last case statement to avoid forgetting to add a break statement after the last case statement when adding a new case statement.

 

2.4 Nested branch statements

Both switch statements and if-else statements can be nested 

Implementation process

Example 1:

#include <stdio.h>

int main()
{
	double score = 0.0;
	printf("请输入分数");
	scanf("%d", &score);

	if (score >= 0.0 && score <= 100)
	{
		if (score >= 85)
		{
			printf("优秀\n");
		}
		else if (score >= 75)
		{
			printf("良好\n");
		}
		else if (score >= 60)
		{
			printf("及格\n");
		}
		else
		{
			printf("不及格\n");
		}
	}
	else
	{
		printf("输入错误!\n");
	}

	return 0;
}

Example 2:

#include <stdio.h>

int main()
{
	int a = 0;
	int b = 5;
	if (a > 0)
		if (b > 0)
			printf("b = %d", b);
	else
			printf("a = %d", a);
	return 0;
}

operation result

 

 

 Note: The C language stipulates the nearest matching principle of if and else, that is, else is paired with the nearest unpaired if above it , regardless of the writing format

Add {} after the execution statement, and the matching if else keeps the same indentation, which can make the logic clearer and less error-prone

 

Example 3:

#include <stdio.h>

int main()
{
	int a = 2;
	int b = 3;

	switch (a)
	{
	case 1:
		printf("a = 1\n");
		break;
	case 2:
		printf("a = 2\n");

		switch (b)
		{
		case 1:
			printf("b = 1\n");
			break;
		default:
			printf("b != 1\n");
			break;
		}

		break;
	default:
		break;
	}

	return 0;
}

Guess you like

Origin blog.csdn.net/2301_76161469/article/details/130301975