Branch and loop statements (super detailed explanation)

branch statement

  • if
  • switch

loop statement

  • while
  • for
  • do while

goto statement

1. What is a sentence?

C statements can be divided into the following five categories:

  1. expression statement
  2. function call statement
  3. control statement
  4. compound statement
  5. empty statement

Part of the statement code display:

#include <stdio.h>

int main()
{
    
    

	3 + 5;//表达式语句
	printf("hehe\n");//函数调用语句
	;//空语句 - 有时候我们需要一条语句,但是这条语句什么都不需要做,就可以使用空语句

	return 0;
}

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;
  2. Loop execution statement: do while statement, while statement, for statement;
  3. Turn statement: break statement, goto statement, continue statement, return statement.

2. Branch statement

If you study hard, get a good offer during school recruitment, and reach the pinnacle of life.
If you don't study, graduation is equivalent to unemployment, go home and sell sweet potatoes.
This is choice!
insert image description here

2.1 if statement

What is the grammatical structure of the if statement?
Grammatical structures:

if (expression)
statement;
if (expression)
statement 1;
else
statement 2;
//multi-branch
if (expression 1)
statement 1;
else if (expression 2)
statement 2;
else
statement 3;

Code demo:

int main()
{
    
    
	//如果年龄大于等于18-成年人
	int age = 0;
	scanf("%d", &age);
	if (age >= 18)
		printf("成年人\n");
	return 0;
}

int main()
{
    
    
	//如果年龄大于等于18-成年人,否则打印-未成年人
	int age = 0;
	scanf("%d", &age);
	if (age >= 18)
		printf("成年人\n");
	else
		printf("未成年人\n");
	return 0;
}

//age<18  - 未成年
//18~30   - 青年
//31~50   - 中年
//51~70   - 中老年
//71~99   - 老年
//>99     - 老寿星

int main()
{
    
    
	int age = 0;
	scanf("%d", &age);
	if (age < 18)
		printf("未成年\n");
	else
	{
    
    
		if (age >= 18 && age <= 30)
			printf("青年\n");
		else
		{
    
    
			if (age >= 31 && age <= 50)
				printf("中年\n");
			else
			{
    
    
				if (age >= 51 && age <= 70)
					printf("中老年\n");
				else
				{
    
    
					if (age >= 71 && age <= 99)
						printf("老年\n");
					else
						printf("老寿星\n");
				}
			}
		}
	}
	return 0;
}
代码改进:
int main()
{
    
    
	int age = 0;
	scanf("%d", &age);
	if (age < 18)
		printf("未成年\n");
	else if (age >= 18 && age <= 30)
		printf("青年\n");
	else if (age >= 31 && age <= 50)
	    printf("中年\n");
	else if (age >= 51 && age <= 70)
		printf("中老年\n");
	else if (age >= 71 && age <= 99)
		printf("老年\n");
	else
		printf("老寿星\n");
	return 0;
}
进一步改进:
int main()
{
    
    
	int age = 0;
	scanf("%d", &age);
	if (age < 18)
		printf("未成年\n");
	else if (age <= 30)
		printf("青年\n");
	else if (age <= 50)
		printf("中年\n");
	else if (age <= 70)
		printf("中老年\n");
	else if (age <= 99)
		printf("老年\n");
	else
		printf("老寿星\n");
	return 0;
}

To explain:
If the expression evaluates to true, the statement executes.
How to express true and false in C language?

0 means false, non-zero means true.

If the condition is true and multiple statements are to be executed, how should a code block be used.


int main()
{
    
    
	int age = 0;
	scanf("%d", &age);
	if (age >= 18)
	{
    
    //代码块
		printf("成年了\n");
		printf("谈恋爱\n");
	}
	return 0;
}

2.1.1 Floating else

When you write code like this:

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

Amendment:

  • Appropriate use of {} can make the logic of the code clearer.
  • Code style matters
#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;
}

Else matching: else matches the nearest if.

2.1.2 Comparison of writing forms of if


//代码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)//能检测出num=5赋值操作的错误
{
    
    
    printf("hehe\n");
}

Code 2 and Code 4 are better, the logic is clearer, and it is less prone to errors.

2.1.3 Exercises

  1. Check if a number is odd
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	if (n % 2 == 1)
	//if(1 == n%2)
		printf("YES\n");
	else
		printf("NO\n");

	return 0;
}
  1. Output odd numbers between 1-100
//案例一:
int main()
{
    
    
	//while
	int i = 1;
	while (i <= 100)
	{
    
    
		if (i % 2 == 1)
			printf("%d ", i);
		i++;
	}
	return 0;
}
//案例2:
int main()
{
    
    
	//while
	int i = 1;
	while (i <= 100)
	{
    
    
		printf("%d ", i);
		i = i + 2;//i+=2;
	}
	return 0;
}

2.2 switch statement

The switch statement is also a branch statement.
Often used in multi-branch situations.
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

Then I didn't write if...else if...else if is too complicated, then we have to have a different grammatical form.
This is the switch statement.

switch(integer expression)
{ statement item; }

And what is the statement item?

//It is some case statement:
//As follows:
case integer constant expression:
statement;

2.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:
insert image description here
Make a modification: add break after each statement:

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

operation result:
insert image description here

Sometimes our needs change:

  1. Input 1-5, the output is "weekday";
  2. Input 6-7, output "weekend"

So our code should be implemented like this:

int main()
{
    
    
	int day = 0;
	scanf("%d", &day);//4
	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;
}

operation result:
insert image description here

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

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).

2.2.2 default statement

What if the value of the expression does not match the value of any case label?
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 in the statement list, and write the following label default in any position where 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.
good programming habits

It is a good habit to put a default clause in each switch statement, and you can even add a break after it.

2.2.3 Exercises

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

Case result:
insert image description here

3. Loop statement

  • while
  • for
  • do while
    insert image description here

3.1 while loop

We've got that, the if statement:

if (condition)
statement;

When the condition is met, the statement after the if statement is executed, otherwise it is not executed.
But this statement will only be executed once.

Because we find that many practical examples in life are: we need to do the same thing many times.
So how do we do it?
The C language introduces us: while statement , which can realize loop.

//while syntax structure
while (expression)
loop statement;

While statement execution flow:
insert image description here
For example, we implement:

Print the numbers 1-10 on the screen

include <stdio.h>
int main()
{
    
    
 int i = 1;
 while(i<=10)
 {
    
    
 printf("%d ", i);
 i = i+1;
 }
 return 0;
}

operation result:
insert image description here

3.1.1 continue and break in while loop

continueIntroduction
Code Examples

int main()
{
    
    
	int i = 1;
	while (i <= 10)
	{
    
    
		i++;
		if (i == 5)
			continue;

		printf("%d ", i);
	}

	return 0;
}

insert image description here
Summary:
The function of continue in the while loop is:

continue is used to terminate this cycle, that is, the code after continue in this cycle will not be executed again.

break introduction:

Code example:

int main()
{
    
    
	int i = 1;
	while (i <= 10)
	{
    
    
		if (i == 5)
			break;

		printf("%d ", i);
		i++;
	}
	return 0;
}

Running results:
insert image description here
Summary:
The function of break in the while loop:

In fact, as long as a break is encountered in the loop, all subsequent loops will be stopped and the loop will be terminated directly.
So: the break in while is used to permanently terminate the loop.

Supplement: getchar and putchar

  • getchar() - get (enter) a character
  • When getchar reads a character successfully, it returns the ASCII code value of the character
  • EOF is returned when the read fails or the end of the file is encountered
  • putchar() - output a character

Code Demonstration:
Case 1

int main()
{
    
    
	int ch = getchar();
	printf("%c\n", ch);

	return 0;
}

Running result:
insert image description here
only one character can be entered, how to enter it multiple times?
Case two:

int main()
{
    
    
	int ch = 0;
	
	while ((ch = getchar()) != EOF)
	{
    
    
		putchar(ch);
	}
	return 0;
}

operation result:
insert image description here

  • Windows environment press ctrl+z getchar returns EOF

Case 3: Only output ASCII of 0...9

int main()
{
    
    
	int ch = 0;
	while ((ch = getchar()) != EOF)
	{
    
    
		if (ch < '0' || ch > '9')
			continue;

		putchar(ch);
	}
	return 0;
}

operation result:
insert image description here

Case 4:

int main()
{
    
    
	char password[20];
	printf("请输入密码:>");
	scanf("%s", password);//scanf函数在读取字符串的时候,遇到空格就不再读取
	int ch = 0;
	while (getchar() != '\n')
	{
    
    
		;
	}
	printf("请确认(Y/N):");
	ch = getchar();

	if ('Y' == ch)
		printf("确认成功\n");
	else
		printf("确认失败\n");

	return 0;
}

3.2 for loop

We already know about the while loop, but why do we need a for loop?
First look at the syntax of the for loop

3.2.1 Syntax

for(expression1; expression2; expression3)
loop statement;

Expression 1
Expression 1 is the initialization part, which is used to initialize the loop variable.
Expression 2
Expression 2 is the condition judgment part, which is used to judge the termination of the loop.
Expression 3
Expression 3 is an adjustment part, which is used to adjust the loop condition.

Actual question:

Use a for loop to print the numbers 1-10 on the screen.

int main()
{
    
    
	int  i = 0;
	for (i = 1; i <= 10; i++)
		printf("%d ", i);
	return 0;
}

insert image description here

Execution flowchart of for loop:
insert image description here
compare for and while:

int i = 0;
//实现相同的功能,使用while
i=1;//初始化部分
while(i<=10)//判断部分
{
    
    
    printf("hehe\n");
    i = i+1;//调整部分
}
//实现相同的功能,使用for
for(i=1; i<=10; i++)
{
    
    
    printf("hehe\n");
}

It can be found that the three necessary conditions of the loop still exist in the while loop, but due to the style problem, the three parts are likely to deviate far away, so the search and modification are not concentrated and convenient enough. Therefore, the style of the for loop is better; the frequency of the for loop is also the highest.

3.2.2 continue and break in for loop

In the for loop, if it encounters continue, it will skip the code behind the continue, and go directly to the adjustment part of the loop, which is
somewhat different from the continue in the while loop

Code demo:

int main()
{
    
    
	int  i = 0;

	for (i = 1; i <= 10;
	{
    
    
		if (i == 5)
			continue;

		printf("%d ", i)
	}
	return 0;
}

operation result:
insert image description here

break also terminates the loop directly in the for loop, as long as a break is encountered, the loop will end directly
break is the same as in the while loop and the for loop

Code demo:

int main()
{
    
    
	int  i = 0;
	for (i = 1; i <= 10; i++)
	{
    
    
		if (i == 5)
			break;
		printf("%d ", i);
	}
	return 0;
}

Running result:
insert image description here
Supplement:
Observe the following code:

代码一:
int main()
{
    
    
	int i = 0;
	for (i = 0; i < 10; i++)
	{
    
    
			printf("%d ", i);
	}
	return 0;
}

代码二:
int main()
{
    
    
	//C99 
	for (int i = 0; i < 10; i++)
	{
    
    
		printf("%d\n", i);
	}
	printf("i = %d\n", i);
	return 0;
}

Code 2 running result:
insert image description here

Case 2 shows that the value of i cannot be printed

3.2.3 The loop control variable of the for statement

suggestion:

  1. Do not modify the loop variable in the body of the for loop to prevent the for loop from getting out of control.
  2. It is recommended that the value of the loop control variable of the for statement be written in the way of "open interval after closing before"
int main()
{
    
    
	int arr[10] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	//0~9
	int i = 0;
	for (i = 0; i < 10; i++)//[0,10)
	{
    
    
		printf("%d ", arr[i]);
	}

	//for (i = 0; i <= 9; i++)//[0,9]
	//{
    
    
	//	printf("%d ", arr[i]);
	//}
	//return 0;
}

3.2.4 Some for loop variants

Case number one:

#include <stdio.h>
int main()
{
    
    
 //代码1
 for(;;)
 {
    
    
 printf("hehe\n");
 }

The initialization part, judgment part, and adjustment part in the for loop can be omitted, but it is not recommended to omit it for beginners, which may easily cause problems
.

Case two:

int main()
{
    
    
	int i = 0;
	int j = 0;
	//这里打印多少个hehe?
	for (i=0; i < 3; i++)
	{
    
    
		for (j=0; j < 3; j++)
		{
    
    
			printf("hehe\n");
		}
	}
	return 0;
}

operation result:
insert image description here

int main()
{
    
    
    int i = 0;
    int j = 0;
    //如果省略掉初始化部分,这里打印多少个hehe?
    for (; i < 3; i++)
    {
    
    
        for (; j < 3; j++)
        {
    
    
            printf("hehe\n");
        }
    }
}

Running results:
insert image description here
Case 3:

//代码4-使用多余一个变量控制循环
 int x, y;
    for (x = 0, y = 0; x<2 && y<5; ++x, y++)
   {
    
    
        printf("hehe\n");
   }
 return 0;
}

operation result:
insert image description here

3.3 do...while() loop

3.3.1 do statement Syntax

do
loop statement;
while(expression);

3.3.2 Execution process

insert image description here

3.3.3 Features of the do statement

The loop is executed at least once, and the usage scenarios are limited, so it is not often used.

Code demo:

int main()
{
    
    
	int i = 10;
	do
	{
    
    
		printf("%d\n", i);
	} while (i < 10);
	return 0;
}

operation result:
insert image description here

3.3.4 continue and break in do while loop

  • The break and continue in the do while loop are exactly the same as in the while loop
  • break is used to terminate the loop, and continue is to skip the code behind this loop and go directly to the judgment part

code demo;

int main()
{
    
    
	int i = 1;
	do
	{
    
    
		if (i == 5)
			continue;
		printf("%d ", i);
		i++;
	} while (i<=10);

	return 0;
}

operation result:
insert image description here


Loop statement example:


Case 1: Calculate n factorial

//5!= 1*2*3*4*5 = 120
//2!= 1*2
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	//循环产生1~n的数字
	int i = 0;
	int ret = 1;
	for (i = 1; i <= n; i++)
	{
    
    
		//ret = ret * i;
		ret *= i;
	}
	printf("%d\n", ret);
	return 0;
}

Case 2: Calculate 1!+2!+3!+...+10!

int main()
{
    
    
	int n = 0;
	//循环产生1~n的数字
	int i = 0;
	int ret = 1;
	int sum = 0;
	//1!+2!+3! = 1 + 2 + 6 = 9
	//1*1
	//1*1*2
	//1*1*2*3
	//1*1*2*3*4
	//1*1*2*3*4*5
	for (n = 1; n <= 10; n++)
	{
    
    
		ret = 1;
		for (i = 1; i <= n; i++)
		{
    
    
			ret *= i;
		}
		sum += ret;
	}
	printf("%d\n", sum);
}
//进行优化:
int main()
{
    
    
	int n = 0;
	//循环产生1~n的数字
	int i = 0;
	int ret = 1;
	int sum = 0;
	//1!+2!+3! = 1 + 2 + 6 = 9
	//1*1
	//1*1*2
	//1*1*2*3
	//1*1*2*3*4
	//1*1*2*3*4*5
	for (n = 1; n <= 10; n++)
	{
    
    
		ret *= n;
		sum += ret;
	}
	printf("%d\n", sum);
	return 0;
}

Case 3: Find a specific number n in an ordered array.

//1 2 3 4 5 6 7 8 9 10
//0 1 2 3 4 5 6 7 8 9
//7
int main()
{
    
    
	int arr[10] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	int k = 0;
	scanf("%d", &k);//7
	//遍历
	int i = 0;
	int flag = 0;
	for (i = 0; i < 10; i++)
	{
    
    
		if (arr[i] == k)
		{
    
    
			printf("找到了,下标是:%d\n", i);
			flag = 1;
			break;
		}
	}
	if (flag == 0)
		printf("找不到\n");
	return 0;
}

Optimization: Binary Search (Binary Search)

int main()
{
    
    
	int arr[] = {
    
     1,2,3,4,5,6,7,8,9,10 };
	//printf("%d\n", sizeof(arr));//40 - 数组的总大小,单位是字节
	//printf("%d\n", sizeof(arr[0]));
	//printf("%d\n", sizeof(arr)/sizeof(arr[0]));//
	int sz = sizeof(arr) / sizeof(arr[0]);
	int k = 0;
	scanf("%d", &k);
	int left = 0;
	int right = sz-1;
	int flag = 0;
	while (left<=right)
	{
    
    
		int mid = (left + right) / 2;
		if (arr[mid] < k)
			left = mid + 1;
		else if (arr[mid] > k)
			right = mid - 1;
		else
		{
    
    
			printf("找到了,下标是:%d\n", mid);
			flag = 1;
			break;
		}
	}
	if (flag == 0)
		printf("找不到了\n");
	return 0;
}

Case 4: Write code to demonstrate that multiple characters move from both ends and converge toward the middle

//welcome to bit!!!!!!!
//*********************
//w*******************!
//we*****************!!
//wel***************!!!
//...
//welcome to bit!!!!!!!
#include <string.h>
#include <windows.h>
int main()
{
    
    
	char arr1[] = "welcome to bit!!!!!!!";
	char arr2[] = "*********************";
	//char arr[] = "abc";
	//a b c \0
	//0 1 2
	//strlen(arr) - 3
	//sizeof(arr)/sizeof(arr[0]);
	//4-2
	int left = 0;
	int right = strlen(arr2) - 1;
	while (left<=right)
	{
    
    
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		printf("%s\n", arr2);
		Sleep(1000);
		system("cls");//清空屏幕
		left++;
		right--;
	}
	printf("%s\n", arr2);
	return 0;
}

Case 5: Write code to implement, simulate the user login scenario, and only log in three times. (It is only allowed to enter the password three times. If the password is correct, it will prompt to log in. If it is entered incorrectly three times, it will exit the program.

//"123456"
//strcmp 函数是比较字符串的大小的,头文件string.h
//>   >0
//==  0
//<   <0
#include <string.h>
int main()
{
    
    
	int i = 0;
	char password[20] = {
    
    0};
	for (i = 0; i < 3; i++)
	{
    
    
		printf("请输入密码:>");
		scanf("%s", password);
		if (strcmp(password, "123456") == 0)
		{
    
    
			printf("登录成功\n");
			break;
		}
		else
		{
    
    
			printf("密码错误\n");
		}
	}
	if (i == 3)
		printf("三次密码错误,退出程序\n");
	return 0;
}

Case 6: Guess the Number Game Realization

//电脑随机生产一个1~100之间的数
//接下来我们猜数字
//如果猜小了,就告诉你猜小了
//如果猜大了,就告诉你猜大了
//如果猜对了,那就恭喜你,游戏结束
//
#include <stdlib.h>
#include <time.h>
void menu()
{
    
    
	printf("**************************\n");
	printf("******  1. play     ******\n");
	printf("******  0. exit     ******\n");
	printf("**************************\n");
}
void game()
{
    
    
	//1. 生成1~100的随机数
	//rand函数生产的伪随机数
	//rand函数生产的随机数的范围是:0~RAND_MAX(32767)
	//rand函数在生产随机数之前,要使用srand函数设置随机数的生成器
	int ret = rand()%100+1;//0~99+1 --> 1~100
	printf("%d\n", ret);
	//RAND_MAX;
	
	//2. 猜数字
	int guess = 0;
	while (1)
	{
    
    
		printf("请猜数字:>");
		scanf("%d", &guess);
		if (guess < ret)
			printf("猜小了\n");
		else if (guess > ret)
			printf("猜大了\n");
		else
		{
    
    
			printf("恭喜你,猜对了\n");
			break;
		}
	}
}

//邮戳
//time - 函数可以返回一个时间戳

int main()
{
    
    
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
    
    
		menu();
		printf("请选择>:");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

4. goto statement

The C language provides goto statements that can be abused at will and labels that mark jumps.
Theoretically, the goto statement is not necessary, and in practice, the code can be easily written without the goto statement.
However, the goto statement is still useful in some occasions. The most common usage is to terminate the processing of the program in some deeply nested structures
.

For example: Jump out of two or more layers of loops at a time.
The use of break in this case of multi-layer loop cannot achieve the purpose. It can only exit from the innermost loop to the previous loop.

for(...)
    for(...)
   {
    
    
        for(...)
       {
    
    
            if(disaster)
                goto error;
       }
   }
error:
 if(disaster)

You cannot jump across functions:

Code demo:

void test()
{
    
    
flag:
	printf("haha\n");

}
int main()
{
    
    
	printf("hehe\n");
	goto flag;//err-goto 只能在同一个函数内部跳转,不能跨函数
	return 0;
}

The following is an example of using the goto statement, and then replacing the goto statement with a loop implementation:
a shutdown program:

//关机程序

//程序运行起来后,1分钟内关机
//如果,输入:我是猪,就取消关机

int main()
{
    
    
	char input[20] = {
    
     0 };
	system("shutdown -s -t 60");//开始倒计数关机

again:
	printf("你的电脑在1分钟内就会关机,如果输入:我是猪,就取消关机\n");
	scanf("%s", input);
	if (strcmp(input, "我是猪") == 0)
	{
    
    
		system("shutdown -a");
		printf("你很配合,已经取消关机\n");
	}
	else
	{
    
    
		goto again;
	}
	return 0;
}

And if the goto statement is not applicable, you can use a loop:

int main()
{
    
    
	char input[20] = {
    
     0 };
	system("shutdown -s -t 60");//开始倒计数关机

	while (1)
	{
    
    
		printf("你的电脑在1分钟内就会关机,如果输入:我是猪,就取消关机\n");
		scanf("%s", input);
		if (strcmp(input, "我是猪") == 0)
		{
    
    
			system("shutdown -a");
			printf("你很配合,已经取消关机\n");
			break;
		}
	}
	return 0;
}

This content has come to an end, I believe that you have gained a lot from reading this content. Looking forward to seeing you in the next article! ! !

Guess you like

Origin blog.csdn.net/2201_75642960/article/details/131769892