First knowledge of branch statements

What is a branch statement before understanding it?

Statement: a statement separated by a semicolon in C language, such as printf("hehe");

1, if else branch statement

The following code

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


int main()
{
	int age =10;//Branch statements execute only one sentence by default
	if (age >= 18)
		printf("adult\n");
	else
		printf("under age\n");
	return 0;
}

The output is as follows

If you print and output the code like this at this time

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


int main()
{
	int age =20;//Branch statements execute only one sentence by default
	if (age >= 18)
		printf("adult\n");
	else
		printf("under age\n");
	printf("Can't fall in love\n");
	return 0;
}

The output results are as follows

We can see that the result we want to see is not like this. How can we become what we want? At this point, we need to add a brace to make it a code block. The code is as follows

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


int main()
{
	int age =20;//Branch statements execute only one sentence by default
	if (age >= 18)
		printf("adult\n");
	else
		{
        printf("under age\n");
	    printf("Can't fall in love\n");
        }
	return 0;
}

The output results are as follows

Therefore, if you want to execute multiple statements with a branch statement, you need to add braces to make it a code block.

2, switch branch statement

switch statement is also a branch statement, which is often used in the case of multiple branches.

If I want to enter the number 1 on the keyboard at this time; You can display Monday on the screen; And so on until Sunday

The code I entered is as follows

#define  _CRT_SECURE_NO_WARNINGS
#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;
}

Now I enter 1 and see the result as follows

Then enter the number 2 and try again. The result is as follows

The above situation occurs because after entering 1, it is executed in the order of case statements, and there is no meaning to stop. Therefore, all the statements of case1-7 are implemented.

So how to implement the above statement?

At this time, try adding break after each case statement.

The code is changed as follows

#define  _CRT_SECURE_NO_WARNINGS
#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 output result of its input number 1 is as follows

You can see that the above statement can be realized by adding break after each case statement.
Therefore, if you want to realize the real "branch", you should add "break" after each case statement.

Another program: now when you want to enter the number 1-5, the screen will appear "working day", and when you enter the number 6-7, the screen will appear "rest day".
The code implementation is as follows

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


int main()
{
	int day = 0;
	scanf("%d", &day);
	switch (day)
	{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		printf("weekdays\n");
		break;
	case 6:
	case 7:
		printf("Rest Day\n");
		break;
	}
	return 0;
}

The output results are as follows


But now I input the wrong number. What if I input the number 8 now?
Enter the number 8 and the result is as follows

It can be seen that the program will not report an error after inputting the number 8, but I want to input a number other than 1-7 to display the input error. What should I do at this time?
At this time, we introduce the default statement to see how the code will run

The update code is as follows

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


int main()
{
	int day = 0;
	scanf("%d", &day);
	switch (day)
	{
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		printf("weekdays\n");
		break;
	case 6:
	case 7:
		printf("Rest Day\n");
		break;
	default:
		printf("Input error\n");
		break;
	}
	return 0;
}

The output results are as follows


You can see that the introduction of the default keyword solves the above problem.
 

 

 

Keywords: C C++

Added by jlarson on Mon, 21 Feb 2022 13:09:04 +0200