C language selection structure (branch statement)

Table of contents

1. Selection structure and conditional judgment

2. Use the if statement to realize the selection structure

2.1 The selection structure in C language is mainly realized by if statement. In order to let everyone further understand the application of if statement, let us give an example

2.2 General form of if statement

2.3 Dangling else

3. Use the switch statement to realize the multi-branch selection structure

The form of the switch statement 

Preface : When we first learned C language, it was a sequential structure, which is the simplest program structure. In the sequential structure, each language is executed in a top-down order. After the previous statement is executed, a statement is automatically executed, which is unconditional and not used for any judgment . In fact, in many cases, it is necessary to decide whether to perform a specified operation according to whether a certain condition is satisfied, or to choose one of two or more given operations, which is the problem to be solved by the selection structure.

Before we learn about the selection structure, let's get acquainted with the statement

C statements can be divided into the following five categories:

  1.  expression statement
  2. function call statement
  3. control statement
  4. compound statement
  5. empty statement

The selection structure (branch statement) and loop structure (loop statement) learned today are control statements

Control statements are used to control the execution flow of the program to realize various structural modes of the program ( C language supports three structures: sequential structure, selection structure, and loop structure ), which are composed of specific statement definers, and C language has nine types control statement.

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 statement: break statement, goto statement, continue statement, return statement

1. Selection structure and conditional judgment

There are many situations in real life that require judgment and choice. For example:

  • If you fail the exam, make up the exam (judging whether you pass or not)
  • Traveling on weekends (judging whether it is a weekend)
  • Input a number and output its absolute value. The following statement can be written
	if (x <= 0)
		printf("%d", x);
	else
		printf("%d", -x);

We have to deal with these problems, the key lies in the " conditional judgment ".

The selection structure requires a conditional judgment before proceeding to the next operation.

There are two selection statements in C language:

  1. if statement: a selection structure used to implement two branches
  2. switch statement: used to implement multi-branch selection structure

2. Use the if statement to realize the selection structure

2.1 The selection structure in C language is mainly realized by if statement. In order to let everyone further understand the application of if statement, let us give an example

Example: Input two real numbers and output these two numbers from small to large

Problem-solving idea: Compare the two numbers, if a<b, output directly; if a>b, exchange the two numbers, and then output. Use the if statement to judge.

To execute multiple statements, use the code block "{}"      because the if statement can only manage one statement by default

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d%d", &a, &b);
	if (a > b)
	{
        //将a,b的值互换
		int tmp = a;
		a = b;
		b = tmp;
	}
	printf("a=%d b=%d\n", a, b);
	return 0;
}

2.2 General form of if statement

(1) if (expression)

                statement 1

(2) if (expression)

                statement 1

        else

                statement 2

(3) if (expression 1)

                statement 1

          else if(expression2)

                statement 2

           else if(expression2)

                Statement 3

           else

                Statement 4 

Note: Expressions cannot be written consecutively Example: 1<x<10 

if(1<x<10)
//如果输入的x是11
//1<11表达式为真   返回1
//表达式将变为
if(1<10)
//1<10永远成立  就会执行if中的语句,会输出错误的结果

Should be written as: x>1&&x<10      

2.3 Dangling else

 According to the above code, we can see that else is not combined with the first if, and VS also compares else and the second if by default.

Else matching: else is matched with the closest if

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

When we write such code and write the equal sign (==) as the assignment symbol (=) by mistake, our wrong code will continue to run, so we can write the variable on the right, so that we can't run the wrong code.

3. Use the switch statement to realize the multi-branch selection structure

The form of the switch statement 

switch(整型表达式)
    case 整形常量表达式:
    语句;

If we want to output the day of the week today, we can use the switch statement 

 We found that entering 3 will print out all the cases after 3. We need to add a break statement after each case to jump out of the switch statement ( case determines the entry, break determines the exit  )

illustrate:

  1. The expression in the brackets of the stitch statement is of integer type (including character type)
  2. The statement body contains multiple statements beginning with the keyword case and at most one line of statements beginning with default. The constants followed by case, for example: case 1 or case 'a' and default are used as labels to mark a position. Execute the switch statement, first calculate the value of the expression behind the switch, and then compare it with each csae label , if it is the same as the constant in a certain case label, the flow will go to the statement after the caes label . If there is no case constant matching the switch expression, the process will go to the statement after the default label.
  3. Multiple case labels can share a set of execution statements: Example
case 1:
case 2:
case 3:
case 4:
case 5:
    printf("weekday\n");
    

Note: The caes label is only used as a label: the matching entry label is found according to the value of the switch expression, and the condition check is no longer performed. After the statement after a case label is executed, it will continue to execute from this label without judging. So make reasonable use of breaks.

The knowledge of the selection structure is finished here. I hope that after reading this, you can have a deeper understanding of the selection structure and gain something. If there is something wrong, you can point it out, and thank you for your support.

Guess you like

Origin blog.csdn.net/2301_76207836/article/details/130121866