day11.1 C语言初阶——goto语句,关机程序的两种实现

day11.1

谁能横刀立马,唯我飞牛大将军!

下面是day11所学到的知识:

涵盖的知识点:
goto语句
关机程序的两种实现

1.goto语句

(1)跳转语句
(2)在跳出深层嵌套的结构中比较有用

向上跳转

	#include<stdio.h>
	#include<windows.h>
	#pragma warning(disable:4996)
	int main()
	{ 
	int flag = 1;
	START:
	printf("hello world!\n");
	if (flag == 1)
	{
		goto START;
	}
	system("pause");
	return 0;
	}

向下跳转

	#include<stdio.h>
	#include<windows.h>
	#pragma warning(disable:4996)
	int main()
	{ int flag = 1;
	
	if (flag == 1)
	{
		goto START;
	}
	printf("1\n");
	printf("2\n");
	printf("3\n");
START:
	printf("4\n");
	printf("5\n");
	system("pause");
	return 0;


	}

2.关机程序

goto语句实现

	#include<stdio.h>
	#include<windows.h>
	#pragma warning(disable:4996)
	int main()
	{ 
		system("shutdown -s -t 180");
		char str[10];
		START:
		printf("你的电脑将在180s后关机!若输入正确关键字,则取消关机!\n");
		printf("请输入:");
		scanf("%s",str);
		if (strcmp(str, "爱生活") == 0)
		{
			system("shutdown -a");
			printf("关机取消!\n");
		}
		else{
			goto START;
		}		
	system("pause");
	return 0;
	}

循环实现

	#include<stdio.h>
	#include<windows.h>
	#pragma warning(disable:4996)
	int main()
	{ 
		system("shutdown -s -t 180");
		char str[10];
		while (1)
		{

			printf("你的电脑将在180s后关机!若输入正确关键字,则取消关机!\n");
			printf("请输入:");
			scanf("%s", str);
			if (strcmp(str, "爱生活") == 0)
			{
				system("shutdown -a");
				printf("关机取消!\n");
				break;
			}
		}
	system("pause");
	return 0;
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值