C 语言中的goto、 break 和 continue 语句使用

一、goto 语句

goto 语句为无条件跳转,goto 语句后面带一个标识符,该标识符是同一个函数内某条语句的标号。标号可以出现在任何可执行语句的前面,并且以一个冒号“:”作为后缀。

/*************************************************************************
  > File Name: test.c
  > Author: Wenfei6316
  > Mail: 2500873570@qq.com 
  > Created Time: 2018年02月22日 星期四 19时03分59秒
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int i, ch;
input:
	printf("Please input a number:\n");
	scanf("%d", &i);
	if (ch = getchar() != '\n')
	{
		while (ch = getchar() != '\n');
		printf("The number you inputted was wrong, please input again!\n");
		goto input;		
	}
	printf("i = %d.\n", i);
	return 0;
}

goto 语句后的标识符不能是关键字。

由于goto 语句无条件跳转,因此会破坏原有的函数的结构性,如果函数比较复杂,很有可能因为goto 跳转会让原本想要实现的某种功能被直接跳过了,同时多重嵌套跳转会让别人比较难读懂程序,故能不用 goto 语句尽量不要用。

二、break 和 continue 语句

1、break 只能使用在循环语句(for(),while(),do...while())和 switch 语句中,if 语句不能直接使用 break,只有当 if 语句在循环语句或 switch  中配合才能使用(其实还是相当于 break 用在循环语句和 switch 语句中),表示直接跳出循环语句或者 switch 语句。

/*************************************************************************
  > File Name: test.c
  > Author: Wenfei6316
  > Mail: 2500873570@qq.com 
  > Created Time: 2018年02月22日 星期四 19时03分59秒
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int x = 8;
	for(; x>0; x--)
	{
		if (x%3)
		{
			printf("%d\n,", x--);
			break;
		}
		printf("%d\n,", --x);
	}
	return 0;
}

输出结果:8
/*************************************************************************
  > File Name: test.c
  > Author: Wenfei6316
  > Mail: 2500873570@qq.com 
  > Created Time: 2018年02月22日 星期四 19时03分59秒
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	char *a = "you";
	char b[] = "Welcome you to China!";
	int i, j=0;
	char *p = NULL;

	for (i=0; b[i]!='\0'; i++)
	{
		if (*a == b[i])
		{
			p = a;
			for (j=i; *p!='\0'; j++)
			{
				if (*p != b[j])
					break;
				p++;
			}
			if (*p == '\0')
				break;
		}
	}
	printf("%s\n", &b[i]);
	return 0;
}

输出结果:you to China!
/*************************************************************************
  > File Name: test.c
  > Author: Wenfei6316
  > Mail: 2500873570@qq.com 
  > Created Time: 2018年02月22日 星期四 19时03分59秒
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	char *a = "yours";
	char b[] = "Welcome you to China!";
	int i, j=0;
	char *p = NULL;

	for (i=0; b[i]!='\0'; i++)
	{
		if (*a == b[i])
		{
			p = a;
			for (j=i; *p!='\0'; j++)
			{
				if (*p != b[j])
					break;
				p++;
			}
			if (*p == '\0')
				break;
		}
	}
	printf("%s\n", p);
	printf("%s\n", &b[i]);
	return 0;
}

输出结果:rs


2、continue 只能使用在循环语句(for(),while(),do...while())语句中,if 和 switch 语句后不能直接使用 continue,只有当 if 和 switch 语句在循环语句中配合才能使用(其实还是相当于 continue 用在循环语句语句中),表示直接跳出本次循环, 特别注意对于 for 循环 continue 只是把{}后面语句跳出,for 里面的迭代语句(如:i++)仍然执行



/*************************************************************************
  > File Name: test.c
  > Author: Wenfei6316
  > Mail: 2500873570@qq.com 
  > Created Time: 2018年02月22日 星期四 19时03分59秒
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int x = 8;
	for(; x>0; x--)
	{
		if (x%3)
		{
			printf("%d\t", x--);
			continue;
		}
		printf("%d\t", --x);
	}
	printf("\n");
	return 0;
}

输出结果:	8	5	4	2
/*************************************************************************
  > File Name: test.c
  > Author: Wenfei6316
  > Mail: 2500873570@qq.com 
  > Created Time: 2018年02月22日 星期四 19时03分59秒
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	char *a = "you";
	char b[] = "Welcome you to China!";
	int i, j=0;
	char *p = NULL;

	for (i=0; b[i]!='\0'; i++)
	{
		if (*a == b[i])
		{
			p = a;
			for (j=i; *p!='\0'; j++)
			{
				if (*p != b[j])
					continue;
				p++;
			}
			if (*p == '\0')
				break;
		}
	}
	printf("%s\n", &b[i]);
	return 0;
}

输出结果:you to China!
/*************************************************************************
  > File Name: test.c
  > Author: Wenfei6316
  > Mail: 2500873570@qq.com 
  > Created Time: 2018年02月22日 星期四 19时03分59秒
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	char *a = "you";
	char b[] = "Welcome you to China!";
	int i, j=0;
	char *p = NULL;

	for (i=0; b[i]!='\0'; i++)
	{
		if (*a == b[i])
		{
			p = a;
			for (j=i; *p!='\0'; j++)
			{
				if (*p != b[j])
					continue;
				p++;
			}
			if (*p == '\0')
				continue;
		}
	}
	printf("%s\n", &b[i]);
	return 0;
}

输出结果:
/*************************************************************************
  > File Name: test.c
  > Author: Wenfei6316
  > Mail: 2500873570@qq.com 
  > Created Time: 2018年02月22日 星期四 19时03分59秒
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	int i;

	for (i=0; i<4; i++)
	{
		printf("When i is: %d\n", i);
		switch (i)
		{
		default:
			printf("******\n");
		case 1:
			printf("*\n");
			break;
		case 2:
			printf("**\n");
			continue;		
		case 3:
			printf("***\n");
			break;
		}
	}
	return 0;
}

输出结果:
When i is: 0
******
*
When i is: 1
*
When i is: 2
**
When i is: 3
***



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值