branching and looping (1)

Preface
In the past, we used four chapters to get acquainted with C language and understand the learning process of C language. Next, we will start to explain C language in detail. Today we are going to learn branch if switchstatements

  • branch statement

if
switch

1. What is a statement
C language can be divided into the following five statements

expression statement
function call statement
control statement
compound statement
empty statement

Today we're going to talk about control statements

Control statements are used to control the execution flow of the program to realize various structural modes of the program. They are composed of specific statement definers. C language has nine control statements.
Can be divided into the following three categories:

  1. Conditional judgment statements are also called branch statements: if statement, switch statement;
  1. Loop execution statement: do while statement, while statement, for statement;
  1. Turn statement: break statement, goto statement, continue statement, return statement.

branch statement

  1. if statement

1 Grammatical structure

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

When we write the if statement, it is best to add {} to form a good habit, so that we can easily find the code error. If there are multiple statements, we must also add {}, so we better add {}, as follows Example to illustrate.

#include<stdio.h>
int main()
{
    
    
	int a = 0;
	scanf("%d", &a);
	if (a >= 0 && a <= 18)
	{
    
    
		printf("未成年\n");
	}
	else if (a > 18 && a <= 50)
	{
    
    
		printf("中年\n");
	}
	else if (a > 50 && a < 100)
	{
    
    
		printf("老年\n");
	}
	else
	{
    
    
		printf("老不死\n");
	}

	return 0;
}

In the above code, if ifthe expressions in the brackets are true, then the following statements will be executed. You can see that we have added the above statements, {}which looks very comfortable and easy to check. There can also be multiple statements in the brackets. For example, we It is possible if (a >= 0 && a <= 18) { printf("未成年\n"); }加上if (a >= 0 && a <= 18) { printf("未成年\n"); printf(”不能抽烟\n“); }to perform these two statements without adding brackets, and an error will be reported

In C, zero means false and nonzero means true.

dangling else

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

In the above code, do you think that the first one is paired ifwith the latter else, but in fact it is paired with the nearest else, then look at the following code.

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

Everyone should understand this, so when we write code, we must optimize the code. Optimization is not that you have less code, but that it looks easy to understand.

int num=2;
if(2==num)//也可以写成if(num==2)
    printf("1334");
     return 0;

In the compiler, we write it so that it 在if(num=2)will not report an error, but if it is written it if(2=num)will report an error, so here I suggest that you can write it as the second type, so that it is convenient for everyone to check.

== is to judge whether they are equal
= is to assign

Here are two exercises, try them out

Determine whether a number is odd or not,
output all odd numbers from 1 to 100

#include<stdio.h>
int main()
{
    
    
	int a = 0;
	scanf("%d", &a);
	if (1 == a % 2)
	{
    
    
		printf("奇数\n");
	}
	else
	{
    
    
		printf("偶数\n");
	}
	return 0;
}

#include<stdio.h>
int main()
{
    
    
	int i = 1;
	while (i <= 100)
	{
    
    
		if (1 == i % 2)//判断是否是奇数
			printf("%d ", i);
		    i++;

	}
	return 0;
}

The above is the answer, please write it yourself first, and then compare it, of course there is more than one answer.

2. switch statement

**switch** is also a branch statement, often used in multi-branch situations.

We want to output a code such as Monday to Sunday.
1 means output Monday
2 means output Tuesday
3 means output Wednesday
4 means output Thursday
5 means output Friday
6 means output Saturday
7 means output Sunday

To complete the above code, we can use the if statement to complete, but we are very troublesome, we can use the switch statement, which simplifies the code, we can use the if statement to demonstrate first.

#include<stdio.h>
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	if (n == 1)
	{
    
    
		printf("星期一");
	}
	else if (n == 2)
	{
    
    
		printf("星期二");
	}
	else if (n == 3)
	{
    
    
		printf("星期三");
	}
	else if (n == 4)
	{
    
    
		printf("星期四");
	}
	else if (n == 5)
	{
    
    
		printf("星期五");
	}
	else if (n == 6)
	{
    
    
		printf("星期六");
	}
	else if (n == 7)
	{
    
    
		printf("星期日");
	}
	
	return 0;
}

The above is our code. It is ifcompleted with statements. Isn’t it very complicated? Let’s use switch to complete it.

switch(整型表达式)
{
    
    
    语句项;//语句项是一些case语句
}//如下
case 整形常量表达式:
    语句;

This is the format, let's learn it first, and now we start coding.

#include<stdio.h>
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	switch (n)
	{
    
    
	case 1:
		printf("星期一\n");
	case 2:
		printf("星期二\n");
	case 3:
		printf("星期三\n");
	case 4:
		printf("星期四\n");
	case 5:
		printf("星期五\n");
	case 6:
		printf("星期六\n");
	case 7:
		printf("星期日\n");
	}
	return 0;
}

Although the above code can be printed to feel similar to if, it is still different. Let's take a look at the screenshot printed below
insert image description here

You can see that when I output 1, I want to print Monday, but it also prints the following, which does not meet the result I want. Next, I will add a break after each case statement, let’s see the effect

#include<stdio.h>
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	switch (n)
	{
    
    
	case 1:
		printf("星期一\n");
		break;
	case 2:
		printf("星期二\n");
		break;
	case 3:
		printf("星期三\n");
		break;
	case 4:
		printf("星期四\n");
		break;
	case 5:
		printf("星期五\n");
		break;
	case 6:
		printf("星期六\n");
		break;
	case 7:
		printf("星期日\n");
		break;
	}
	return 0;
}

insert image description here

We can see that when I want to print Thursday, only Thursday will be printed, which is different from the above, and will not continue to print the following

The actual effect of the break statement is to divide the statement list into different branching parts.
According to the above code, if we want to write another code now

When we output one to five, print weekday
and print weekend when output 6.7

#include<stdio.h>
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	switch (n)
	{
    
    
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		printf("weekday\n");
		break;
	case 6:
	case 7:
		printf("weekend\n");
		break;
	}
	return 0;
}

insert image description here
insert image description here
We can see the above two results, and everyone should understand here.
It is recommended
to add a break statement after the last case statement. The reason for writing this way is to avoid forgetting to add a break statement after the last case statement in the past

Speaking of this, do you think that you have finished learning the switch, no!
Smart people may have doubts, what should we do when we output 9 in the above code, what if the expressed value does not match the value of all the case tags?
In fact, it's nothing, the structure is that all the statements are skipped. The program does not terminate, and no error is reported, because this situation is not considered an error in C. But what if you don't want to ignore the value of an expression that doesn't match all tags? You can add a default clause to the statement list, so we will now talk about the default statement
default

Write anywhere a case label can appear. When the value of the switch expression does not match the values ​​of all case labels, the statements following the default clause will be executed.
Therefore, only one default clause can appear in each switch statement. But it can appear anywhere in the statement list, and the statement flow will execute the default clause as if it were a case label.

int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	switch (n)
	{
    
    
	case 1:
		printf("星期一\n");
		break;
	case 2:
		printf("星期二\n");
		break;
	case 3:
		printf("星期三\n");
		break;
	case 4:
		printf("星期四\n");
		break;
	case 5:
		printf("星期五\n");
		break;
	case 6:
		printf("星期六\n");
		break;
	case 7:
		printf("星期日\n");
		break;
	default:
		break;//default可以放任何地方
	}
	return 0;
}

There is an exercise below, you can do it


int main()
{
    
    
	int n = 1;
	int m = 2;
	switch (n)
	{
    
    
	case 1:
		n++;
		m++;
	case 2:
		n++;
			switch (n)
			{
    
    
			case 1:
				n++;
			case 2:
				m++;
				break;
			case 3:
				n++;
			}
	case 3:
		m++;
	}
	printf("m=%d n=%d", m, n);
	return 0;
}

The above is an exercise for everyone, I will not publish the answer, because as long as you read the above explanation carefully, I believe you can find it if you are smart.

epilogue

That’s all for today’s sharing, let’s go step by step and get better together, Aoli!

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/131304282