Introduction to C language (3)

Foreword
  Life will face all kinds of fork in the road, choose the road that suits you is the best.

What is a statement?

C statements can be divided into the following five categories:

  • expression statement
  • function call statement
  • control statement
  • compound statement
  • empty statement

Control statements are covered later in this article.
  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). They are composed of specific statement definers. There are nine types in C language control statement.
  Can be divided into the following three categories:
  1. Conditional judgment statement is also called branch statement: 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.

#include<stdio.h>
int main()
{
    
    
	3 + 5;//表达式语句
	;//空语句
	printf("iloyo.");//函数调用语句
	return 0;
}

1. if statement

Syntax structure of if statement:

if(表达式)
	语句1;
else
	语句2;

//单分支-if语句可以不加else
if(表达式)
	语句;

//多分支
if(表达式1)
	语句1;
else if(表达式2)
	语句2;
else
	语句3

//嵌套使用
if(表达式1)
    语句1;
else
{
    
    
    if(表达式2)
    	语句2;
    else
    	语句3}

If the result of the expression is true, the statement executes
In C language: 0 means false, non-zero means true.

If the condition is true, to execute multiple statements, you should use a code block: { }

#include<stdio.h>
int main()
{
    
    
	if(表达式)
	{
    
    
		语句列表1;
	}
	else
	{
    
    
		语句列表2;
	}
	return 0;
}

Example:

//代码1
#include<stdio.h>
int main()
{
    
    
	int age;
	scanf("%d", &age);
	if (age < 18)
		printf("未成年");
	return 0;
}

//代码2
#include<stdio.h>
int main()
{
    
    
	int age ;
	scanf("%d", &age);
	if (age < 18)
		printf("未成年\n");
	else
		printf("成年\n");
	return 0;
}

It is assumed that a person's life can be divided into: childhood (1-10 years old), youth (11-35 years old), middle age (36-60 years old), old age (61-years old). Knowing a person's age, how do you know what stage of life he is in?

#include<stdio.h>
int main()
{
    
    
	int age = 20;
	//int age;
	//scanf("%d",&age);
	if (age >= 1 && age <= 10)
		printf("幼年");
	else if(age>10&&age<=35)
		printf("青年");
	else if(age>35&&age<=60)
		printf("中年");
	else
		printf("老年");
	return 0;
}

1.1 Floating else

The following is a dangling else statement, which is intended to match the first if statement, and the output should be "haha". Why is there an error?
insert image description here
Reason 1: By default, if can only control one statement. If you want to control multiple statements, you must add { }, and else is the same as above.
Reason two: if-else proximity principle, else is matched with the nearest if.
The correction is as follows:

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

1.2 Comparison of writing forms of if

The following code 2 and code 4 are better, the logic is clearer, and it is not easy to make mistakes.

//代码1
if (condition)
{
    
    
	return x;
}
return y;
//代码2
if(condition)
{
    
    
	return X ;
}
else
{
    
    
	return y;
}

//代码3
int num = 1;
if(num == 5)
{
    
    
	printf("hehe\n");
}
//代码4
int num = 1;
if(5 == num)
{
    
    
	printf("hehe\n");
}
  • When the constant is judged to be equal to the variable, put the constant on the left side of "=="" to avoid mistakes
  • In the function body, as long as it encounters return, it will return, and the following statements will not be executed
  • Develop a good coding style so that you will not lose points in the written test

1.3 Exercises

1. Determine whether a number is odd or not
2. Output odd numbers between 1-100

Scroll down to see the answer

//第一题
#include<stdio.h>
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	if (n % 2 == 1)
	{
    
    
		printf("%d是奇数\n",n);
	}
	else
	{
    
    
		printf("%d不是奇数\n",n);
	}
	return 0;
}
//第二题
//代码1
#include<stdio.h>
int main()
{
    
    
	int i = 1;
	while (i <= 100)
	{
    
    
		printf("%d ", i);
		i = i+2;
	}
	return 0;
}

//代码2
#include<stdio.h>
int main()
{
    
    
	int i;
	for (i = 1; i <= 100; i++)
	{
    
    
		if (i % 2 != 0)
		{
    
    
			printf("%d ", i);
		}
	}
	return 0;
}

2. switch statement

The switch statement is also a branch statement.
It is often used in multi-branch situations, and the order is not emphasized in the syntax.
Grammatical structures:

switch (整型表达式)
{
    
    
	语句项;
}

What is a statement item?

//由一些case语句组成,如:
case 整型常量表达式:
	语句;

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
Use the if statement to express as follows:

#include<stdio.h>
int main()
{
    
    
	int day = 0;
	scanf("%d", &day);
	if (1 == day)
		printf("星期1\n");
	else if (2 == day)
		printf("星期2\n");
	else if (3 == day)
		printf("星期3\n");
	else if (4 == day)
		printf("星期4\n");
	else if (5 == day)
		printf("星期5\n");
	else if (6 == day)
		printf("星期6\n");
	else if (7 == day)
		printf("星期天\n");
	else
		printf("输入错误\n");
	return 0;
}

The switch statement means:

#include<stdio.h>
int main()
{
    
    
	int day = 0;
	scanf("%d", &day);
	switch (day)
	{
    
    
	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;
}

If it is expressed in the form of if...else if...else, it is too complicated, but the switch statement is more concise in writing.

Several ways of writing switch statements:

switch(常量)
{
    
    
    case 常量1:
        printf(" ");
        break;
    case 常量2:
        printf(" ");
        break;
}
switch(常量)
{
    
    
    case 常量1:
    case 常量2:
        printf(" ");
        break;
    case 常量3:
        printf(" ");
}
witch(常量)
{
    
    
    defult:
        printf("输入错误");
        break
    case 常量1:
    case 常量2:
        printf(" ");
        break;
    case 常量3:
        printf(" ");
        break;        
}

2.1 break in switch statement

In the switch statement, we can't directly realize the branch, and we can realize the real branch by using it with break.
for example:

Requirements: Input 1-5, the output is "weekday",
   input 6-7, output "weekend"

Code:

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

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

When writing a switch statement, it is best to add a break statement after the last case statement.
(You can avoid forgetting to add a break statement after the previous last case statement)

2.2 default clause

What if the value of the expression does not match the value of any case label?

In fact, nothing, the result is that all statements are skipped.
The program will not terminate, nor will it report an error, because this situation is not considered an error in the C language.

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, and write the following label
 defaut:
in any position where a case label can appear.
When the value of the switch expression does not match the value of all case labels, the statement following the default clause will be executed.

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.

2.3 Exercises

After reading the above content, have you learned to use the switch statement?
Let's see what the result of this code is?

#include <stdio.h>
int main()
{
    
    
	int n = 1;
	int m = 2;
	switch (n)
	{
    
    
	case 1:
		m++;
	case 2:
		n++;
	case 3:
		switch (n)//switch允许嵌套使用
		{
    
    
		case 1:
			n++;
		case 2:
			m++;
			n++;
			break;
		}
	case 4:
		m++;
		break;
	default:
		break;
	}
	printf("m = %d, n = %d\n", m, n);
	return 0;
}

Answer:
insert image description here


conclusion

The above is my sharing of if statement and switch statement, I hope it will be helpful to you.
  If the road is obstructed and long, it will come soon; if you walk without stopping, the future can be expected.
  See you in the next article.
insert image description here

Guess you like

Origin blog.csdn.net/iLoyo_/article/details/131458244