C language - selection structure

Select structure

The selection structure is used to judge the given conditions, judge some conditions according to the judgment results, and control the program flow according to the judgment results

1, If else statement

1. Single selection

Single selection can be realized through if statement. The syntax and execution process of if statement are as follows:

if(expression)
{
     Code block (executed when the return value of the expression is true)
}

When the program runs to the if statement, it will be determined by the expression. If the return value of the expression is true, it will enter the code block to execute the statement. If it is false, it will skip the code block and continue to execute. (braces can be omitted when there is only one sentence in a contemporary code block statement.)

Take a simple example

Example: judge whether 10 is a multiple of 3

int main()
{
	int i = 10;

		if (i % 3 == 0) //The expression result is false 
			printf("yes");//Not executed within code block
	return 0;
}

 2. Double selection

Double selection is realized by if else statement. The syntax and execution process of if else statement are as follows:

(else matching: else matches the nearest if)

if(expression)
{
   Statement 1 (executed when the return value of the expression is true)
}
else
{
   Statement 2 (executed when the return value of the expression is false)
}

 

 

When the program runs to the if statement, it will be determined by the expression. If the return value of the expression is true, execute statement 1 and false, and execute statement 2.

Take a simple example

Example: judge whether the students pass the grade

#include<stdio.h>
int main()
{
	int i = 0;
	scanf("%d", &i);

	if (i >= 60)
		printf("pass");
	else
		printf("fail,");
	return 0;
}

3. Multiple choices

Multiple selection is realized by else if statement. The syntax and execution process of else if statement are as follows:

if(Expression 1)
{
   Statement 1 (executed when the return value of expression 1 is true)
}
else if(Expression 2)
{
   Statement 2 (executed when the return value of the expression is true)
}
else if(Expression 3)
{ 
    Statement 3 (executed when the return value of the expression is true)
}

else 
{
   Code block (executed when the above expressions are false)
}

 

 

When the program runs to the if statement, it will be determined by the expression. If the return value of expression 1 is true, it will enter the code block under if to execute statement 1. If it is false, it will enter expression 2. Of course, if else can implement more than these four branches. More choices can be realized by increasing the number of else if# statements.

Take a simple example

Example: age segmentation

#include <stdio.h>
int main()
{
    int age = 0;
    scanf("%d", &age);
    if (age < 18)
    {
        printf("juvenile\n");
    }
    else if (age >= 18 && age < 30)
    {
        printf("youth\n");
    }
    else if (age >= 30 && age < 50)
    {
            printf("middle age\n");
    }
    else if (age >= 50 && age < 80)
    {
        printf("old age\n");
    }
    else
    {
        printf("God of Longevity\n");
    }
    return 0;
}

II switch Statements

The switch statement is also a branch statement. It is often used in the case of multiple branches. Else if statements can also implement multi branch situations, but in some cases, using else if to implement it will make the code too complex.

For example:

Input 1, output Monday

Input 2, output Tuesday

Input 3, output Wednesday

Input 4, output Thursday

Input 5, output Friday

Input 6, output Saturday

Input 7, output Sunday

If else if is used

#include<stdio.h>
int main()
{
	int day = 0;
	scanf("%d", &day);

	if (1 == day)
		printf("Monday");
	else if (2 == day)
		printf("Tuesday");
	else if (3 == day)
		printf("Wednesday");
	else if (4 == day)
		printf("Thursday");
	else if (5 == day)
		printf("Friday");
	else if (6 == day)
		printf("Saturday");
	else
		printf("Sunday");
	return 0;
}

Then the code will be too complex, and there will be a lot of using switch

The syntax and execution flow of the switch statement are as follows:

switch(Integer expression)
{
    //There can be any number of case statements in a switch.
    case Integer constant expression:
    sentence;
}

 

case is an entry, which is entered according to the value of the input integer expression, and then run in sequence.

The switch statement alone cannot be used to realize the branch. It needs to be used with break to realize the real branch.

For example:

#include <stdio.h>
int main()
{
    int day = 0;
    scanf("%d", &day);
    switch (day)
    {
    case 1:
        printf("Monday\n");
    case 2:
        printf("Tuesday\n");
    case 3:
        printf("Wednesday\n");
    case 4:
        printf("Thursday\n");
    case 5:
        printf("Friday\n");
    case 6:
        printf("Saturday\n");
    case 7:
        printf("Sunday\n");
    }
    return 0;
}

When the input values are 2 and 4 respectively, the output results are as follows:

It is far from the desired effect, and it can be solved with break.

#include <stdio.h>
int main()
{
    int day = 0;
    scanf("%d", &day);
    switch (day)
    {
    case 1:
        printf("Monday\n");
        break;
    case 2:
        printf("Tuesday\n");
        break;
    case 3:
        printf("Wednesday\n");
        break;
    case 4:
        printf("Thursday\n");
        break;
    case 5:
        printf("Friday\n");
        break;
    case 6:
        printf("Saturday\n");
        break;
    case 7:
        printf("Sunday\n");
        break;
    }
    return 0;
}

 

The actual effect of the break statement is to divide the statement list into different branch parts.

switch is often used with default statements as well as break statements

If the expressed value does not match the value of all case tags, all statements will be skipped. The program will not terminate or report an error, because this situation is not considered an error in C.

If you do not want to ignore the values of expressions that do not match all labels, you can add a default clause to the statement list.

When the value of the switch expression does not match the value of all case tags, the statement after the default clause will be executed.

For example:

#include <stdio.h>
int main()
{
    int day = 0;
    scanf("%d", &day);
    switch (day)
    {
    case 1:
        printf("Monday\n");
        break;
    case 2:
        printf("Tuesday\n");
        break;
    case 3:
        printf("Wednesday\n");
        break;
    case 4:
        printf("Thursday\n");
        break;
    case 5:
        printf("Friday\n");
        break;
    case 6:
        printf("Saturday\n");
        break;
    case 7:
        printf("Sunday\n");
        break;
    default:
        printf("Input error");
    }
    return 0;
}

The switch statement can have an optional default case that appears at the end of the switch. Default case can be used to perform a task when none of the above cases is true. The break statement in the default case is not required.

Keywords: C Back-end

Added by SheepWoolie on Mon, 17 Jan 2022 15:37:32 +0200