Branching and Looping (3)

Hello, everyone, we have met. Today I have finished talking about branching and looping in C language. Today I will talk about a goto statement. Later, I will update some topics for everyone to do, so that everyone can learn better.

The 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)//处理错误情况
        

In the above code, it is as if we encounter an error and have to jump out of the loop, but if we use break to break out of the loop, at least three breaks are required. If it is goto, we will jump directly to error and proceed to the next step.

The following is an example of using a loop to replace the goto statement.

The following is a shutdown program.
As long as the program is running, the computer will shut down within one minute. Input: I am a pig, cancel the shutdown.
Before writing code, we must first understand one thing

This is the command prompt on our computer. When we enter shutdown -s -t 60, it means that the computer will automatically shut down within 60 seconds.
When we enter shutdown -a to cancel the shutdown
insert image description here

After understanding the above, we started to write our code

int main()
{
    
    
   system()//这个库函数是用来执行系统命令的
   return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	char input[20] = {
    
    0};
	system("shutdown - s - t 60");//表示电脑还有60秒就关机
again:
	printf("请注意:你的电脑还有60秒就会关机,输入“我是猪”就取消关机\n");
	scanf("%s", input);
	//判断
	
	if (strcmp(input, "我是猪") == 0)
	{
    
    
		system("shutdown -a");
	}
	else
	{
    
    
		printf("请重新输入\n");
		goto again;
	}
	return 0;
}

The above is our complete code. It said that it can be replaced by other codes. Now we use other codes instead.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    
    
	char input[20] = {
    
     0 };
	system("shutdown - s - t 60");//表示电脑还有60秒就关机

	printf("请注意:你的电脑还有60秒就会关机,输入“我是猪”就取消关机\n");
	scanf("%s", input);
	//判断

	while (1)
	{
    
    
		if (strcmp(input, "我是猪") == 0)
		{
    
    
			system("shutdown -a");
			break;
		}
	}
	return 0;
}

This is what the while loop replaces

We use a simple code to demonstrate the understanding of the goto statement, so that our branches and loops are also learned, and I will update the topic for everyone later. Thank you everyone, that's all for today's sharing.

Guess you like

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