4. Selection structure (if statement, switch statement, relational operator, logical operator)

C language supports three structures
1. Sequential structure
2. Selection structure
3. Loop structure

C language mainly uses if and switch statements to implement branch structures

1.if statement

1.1 if
format:

if(条件表达式){
    
    
	语句
}

If the conditional expression is true (true), the statement is executed; if it is not true (false), the statement is not executed. The
essence of the statement: in C language, 0 is false and non-0 is true, that is, the conditional expression is non-0, then the statement is executed; if If it is 0, the statement will not be executed.

Example: Determine whether a number is a positive number. If it is a positive number, print "It is a positive number"

int main() {
    
    
	int num = 0;
	scanf("%d", &num);
	if (num>0)
	{
    
    
		printf("%d是正数",num);
	}
	return 0;
}

Insert image description here



Note: The if statement here can only control the first statement below by default without {}, so it is recommended to use braces {} to include it.

int main() {
    
    
	int num = 0;
	scanf("%d", &num);
	if (num>0)
		printf("%d是正数\n",num);
		printf("%d是个数", num);//没有{}包括 所以不受if语句影响
	return 0;
}

Because if only controls the following statement, no matter whether num is greater than 0 or not, the content of the second line will be printed
Insert image description here
the same as else. In the same way, if there is no brace {}, it will only affect the next statement.


1.2 else
format:

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

If the conditional expression is true (true), statement 1 is executed.
If the conditional expression is not true (false), statement 2 is executed.

Example: Determine whether a number is a positive number. If it is a positive number, print "It is a positive number", otherwise print "It is not a positive number"

int main() {
    
    
	int num = 0;
	scanf("%d", &num);
	if (num>0)
	{
    
    
		printf("%d是正数",num);
	}
	else
	{
    
    
		printf("%d是非正数", num);
	}
	return 0;
}

1.3 Nested if (else if)
format:

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

If conditional expression 1 is true (true), execute statement 1;
if conditional expression 1 is not true (false), then conditional expression 2 is judged. If conditional expression 2 is true (true), statement 2 is executed; otherwise Execute statement 3

In fact, it is equivalent to adding a pair of if...else...statements in the else statement.

Enter a person's age. If the age is <18 years old, print "Youth".
If the age is between 18 and 44 years old, print "Old Age".
If the age is between 45 and 59 years old, print "Middle-aged"
if the age is between 60 and 89 years old. If you are 90 years old or above , print
"senior"

int main() {
    
    
	int age = 0;
	scanf("%d", &age);
	if (age < 18 && age >= 0) {
    
    
		printf("少年");
	}
	else if (age <= 44)
	{
    
    
		printf("青年");
	}
	else if (age <= 59)
	{
    
    
		printf("中老年");
	}
	else if (age <= 89)
	{
    
    
		printf("老年");
	}
	else if(age >= 90)
	{
    
    
		printf("老寿星");
	}
	else
	{
    
    
		printf("报错!");
	}
	return 0;
}

1.4 The dangling else problem

	int a = 0;
	int b = 2;
	if (a == 1)
		if (b == 2)
			printf("11111\n");
	else
		printf("22222\n");
	return 0;

What does this code output? I guess most people will choose 22222.
In fact, this code outputs nothing.

Remember a rule, if there is no {}, else matches the nearest if


We add {} to divide it into blocks, so it is clear

	int a = 0;
	int b = 2;
	if (a == 1) 
	{
    
    
		if (b == 2)
			printf("hehe\n");
		else
			printf("haha\n");
	}
	return 0;

If you want else and else in the first line to match, adding {} appropriately will make the logical structure of the code clearer and make the code more readable.

	int a = 0;
	int b = 2;
	if (a == 1)
	{
    
    
		if (b == 2)
			printf("hehe\n");
	}
	else
	{
    
    
		printf("haha\n");
	}
	return 0;

2. Relational operators

The expressions used for comparison in C language are called relational expressions.
The operators used in relational expressions are called relational operators.
0 means false
and non-0 means true
Insert image description here
. Notes:

Note point 1: The relational operator == and assignment = are two completely different symbols and have completely different meanings.
a==1 compares the variable a with 1. If it is true, it is non-0; otherwise it is 0
a = 1 is to assign the value 1 to variable a

Note 2: Multiple relational operators should not be used directly together
, such as a < b < c. Although this is a legal expression, it sometimes does not express the meaning we want.
Reason: Relational expressions in C language are evaluated from left to right
a < b < c Our perspective wants to express that b is greater than a and less than c
(a<b) < c. The compiler's perspective first obtains the result x of the relational expression a<b, and then obtains the result x of the relational expression x<c Result
The result of the relational expression (a<b) is only 0 or non-0, (then 0 or non-0) is less than c for comparison, so the meaning of the expression is completely different

For example: If the age is greater than or equal to 18 years old and less than or equal to 36 years old, youth will be output.

int age = 0;
scanf("%d", &age);
if(18<=age<=36)
{
    
    
printf("⻘年\n");
}
return 0;

In the above code, if 12 is entered at this time, the compiler first compares 18<=12, if it is not true, it returns 0; then 0<=36, if it is true, it returns non-0. The statement in the if is executed, but it is inconsistent with what we actually need to express
. .


int age = 0;
scanf("%d", &age);
if(18<=age && age<=36)
{
    
    
printf("⻘年\n");
}
return 0;

At this time, the AND operator (&&) should be added among the logical operators to make it correctly express what we mean.

3. Logical operators

Logical operators provide logical judgment functions. There are mainly three operators:
Insert image description here

3.1 Logical negation operator (!)

	int a = 0;//条件表达式中0为假
	if (!a)
	{
    
    
		printf("true");
	}
	else 
	{
    
    
		printf("false");
	}
	return 0;

Result: true

3.2 Logical AND operator (&&)

	int a = 0;
	int b = 1;
	if (a && b)// 0 && 1 结果为 0
	{
    
    
		printf("true");
	}
	else
	{
    
    
		printf("false");
	}
	return 0;

Result: false

3.3 Logical OR operator (||)

	int a = 0;
	int b = 1;
	if (a || b)// 0 || 1 结果为 0
	{
    
    
		printf("true");
	}
	else
	{
    
    
		printf("false");
	}
	return 0;

Result: true

3.4 Exercise: Judgment of leap year

Enter a year to determine whether it is a leap year.
Leap year:
1. It is a leap year if it is divisible by 4 and not divisible by 100.
2. It is a leap year if it is divisible by 400.

//代码1
int main()
{
    
    
	int year = 0;
	scanf("%d", &year);
	if (year % 4 == 0 && year % 100 != 0){
    
    
		printf("%d是闰年\n",year); 
	}
	else if (year % 400 == 0) {
    
    
		printf("%d是闰年\n", year);
	}
	return 0;
}
//代码2
int main()
{
    
    
	int year = 0;
	scanf("%d", &year);
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		printf("%d是闰年\n", year);
	return 0;
}

3.5 Short circuit

A characteristic of C language logical operators. As mentioned above, the calculation order of relational operators is from left to right, and the same is true for logical expression operators. That is, in == logical expressions, the order of judgment logic is from left to right ==.
If the logical expression on the left side of a logical expression satisfies the conditions corresponding to the logical operator, the logical expression on the right side will no longer be evaluated, commonly known as 'short circuit'

Short circuit condition
Logical AND operator

int month = 2;
if(month >= 3 && month <= 5){
    
    //左边0 即 不成立
	printf("短路,右边不需要执行")
}

The AND operator (&&)
is not valid if the result of the expression on the left is 0, and the expression on the right does not need to be executed.

Logical OR operator

int month = 2;
if(month >= 1 || month <= 0){
    
    //左边非0 即 成立
	printf("未短路,右边需要执行")
}

The OR operator (||)
is established if the result of the expression on the left is non-0, and the expression on the right does not need to be executed.

4.switch statement

4.1 switch

The switch statement is a special if...else...structure used to determine situations where there are multiple results.

Format:

switch(条件表达式){
    
    //结果必须为整型
	case value0:
		语句0;
		break;
	case value1:
		语句1;
		break;
	case value2:
		语句1;
		break;
	default:
		语句d;
		break;
}

=Note:
1. The conditional expression must be an integer expression
2. The value in case value must be an integer constant, and there is a space after case


Example:
Enter 1-5 to print Monday to Friday
. Enter 6-7 to print holidays.

int main() {
    
    
	int weekday = 0;
	scanf("%d", &weekday);
	switch (weekday){
    
    
		case 1:
		{
    
    
			printf("星期一");
			break;
		}
		case 2:
		{
    
    
			printf("星期二");
			break;
		}
		case 3:
		{
    
    
			printf("星期三");
			break;
		}
		case 4:
		{
    
    
			printf("星期四");
			break;
		}
		case 5:
		{
    
    
			printf("星期五");
			break;
		}
		default:
		{
    
    
			printf("休息日");
			break;
		}
	}
	return 0;
}

4.2 break

The meaning of break is to terminate. After each case statement is executed, break must be added to jump out of the switch statement.
Otherwise, the following case statements will continue to be executed until a break is encountered or the switch statement is executed.

int main() {
    
    
	int weekday = 0;
	scanf("%d", &weekday);
	switch (weekday){
    
    
		case 1:
			printf("星期一");
		case 2:
			printf("星期二");
		case 3:
			printf("星期三");
		case 4:
			printf("星期四");
		case 5:
			printf("星期五");
			break;
		}
		default:
		{
    
    
			printf("休息日");
			break;
		}
	}
	return 0;
}

There is no break in the above codes 1-4, and there is no break until 5.
Therefore, after entering 1, all the statements in case 2-4 below will be executed until the break in the case 5 statement is encountered. The program ends.

Insert image description here



Example:
Enter 1-5 and output working days.
Enter 6-7 and output rest days.

	int weekday = 0;
	scanf("%d", &weekday);
	switch (weekday){
    
    
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		{
    
    
			printf("星期五");
			break;
		}
		case 6:
		case 7:
		{
    
    
			printf("休息日");
			break;
		}
	}

Cases can be combined and stacked like this


4.3 default

int main() {
    
    
	int weekday = 0;
	scanf("%d", &weekday);
	switch (weekday){
    
    
		case 1:
		{
    
    
			printf("星期一");
			break;
		}
		case 2:
		{
    
    
			printf("星期二");
			break;
		}
		case 3:
		{
    
    
			printf("星期三");
			break;
		}
		case 4:
		{
    
    
			printf("星期四");
			break;
		}
		case 5:
		{
    
    
			printf("星期五");
			break;
		}
		default:
		{
    
    
			printf("休息日");
			break;
		}
	}
	return 0;
}

The value obtained by the conditional expression after the switch cannot match all case statements, and the default clause is executed.
Deafult means the default meaning
: enter numbers other than 1-5, and the results are all rest days.

Guess you like

Origin blog.csdn.net/qq_45657848/article/details/131885211