016 break和Contiune语句

/*************016 break和Contiune语句**********************
 * C语言精彩编程百例第16个例子
 * 打印半径为1到10之间的圆的面积.
 */

#include<stdio.h>
void main()
{
	int radius; //存放圆的半径
	double area; //存放圆面积
	for (radius=1;radius<=10;radius++)
	{
		area=3.1416*radius*radius;
		//若圆面积超过120,则跳出for循环,不予输出
		if(area>=120.0)
			break;
		printf("square=%f\n",area);
	}
	//将最大圆面积的半径输出
	printf("now radius=%d\n\n",radius-1);

	for(radius=1;radius<=10;radius++)
	{
		area=3.1416*radius*radius;
		//若面积没有超过60,则不输出而是重新开始循环
		if(area<120.0)
			continue;
		printf("square=%f\n",area);

	}

	printf("now radius=%d\n",radius-1);
}


 对应的汇编

 

	.file	"016.c"
	.def	___main;	.scl	2;	.type	32;	.endef
	.text
LC2:
	.ascii "square=%f\12\0"
LC3:
	.ascii "now radius=%d\12\12\0"
LC4:
	.ascii "now radius=%d\12\0"
	.align 8
LC0:
	.long	776530087
	.long	1074340351
	.align 8
LC1:
	.long	0
	.long	1079902208
	.align 2
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$24, %esp
	andl	$-16, %esp
	movl	$0, %eax
	movl	%eax, -20(%ebp)
	movl	-20(%ebp), %eax
	call	__alloca
	call	___main
	movl	$1, -4(%ebp)  # radius =1
L4:
	cmpl	$10, -4(%ebp) # radius 和 10 比较
	jle	L7            # if radius<=10 跳
	jmp	L5            # 退出循环
L7:
	fildl	-4(%ebp)      # 加载 radius
	fldl	LC0           # 加载 3.1416 // 按照在寄存器内表示方法编译器计算好了
	fmulp	%st, %st(1)   # 31.416*radius
	fildl	-4(%ebp)      # 加载 radius
	fmulp	%st, %st(1)   # st = 31.416*radius*radius
	fstpl	-16(%ebp)     # area = st 
	fldl	-16(%ebp)     # area 装入st
	fldl	LC1           # LC1 装入 st // LC1 是120.0 double类型
	fxch	%st(1)        # 交换
	fucompp               # 比较
	fnstsw	%ax           # 状态字载入ax
	testb	$5, %ah       # 设置状态标志
	je	L5            # 判断条件符合则跳转出循环
	subl	$4, %esp      # printf("square=%f\n",area)
	pushl	-12(%ebp)
	pushl	-16(%ebp)
	pushl	$LC2
	call	_printf
	addl	$16, %esp
	leal	-4(%ebp), %eax # eax = & radius
	incl	(%eax)         # radius++
	jmp	L4             # 执行下次循环
L5:
	subl	$8, %esp       # printf("now radius=%d\n\n",radius-1)
	movl	-4(%ebp), %eax
	decl	%eax
	pushl	%eax
	pushl	$LC3
	call	_printf
	addl	$16, %esp
	movl	$1, -4(%ebp)  # radius =1
L10:
	cmpl	$10, -4(%ebp) # radius 和 10 比较
	jle	L13           #if radius<=10 跳
	jmp	L11           # 退出循环
L13:
	fildl	-4(%ebp)
	fldl	LC0
	fmulp	%st, %st(1)
	fildl	-4(%ebp)
	fmulp	%st, %st(1)
	fstpl	-16(%ebp)
	fldl	-16(%ebp)
	fldl	LC1
	fucompp
	fnstsw	%ax
	testb	$69, %ah     # 从这里开始continue 和 break 出现差别
	je	L12          # 如果条件符合,跳到执行radius++处 
	subl	$4, %esp     # 打印printf("square=%f\n",area);
	pushl	-12(%ebp)
	pushl	-16(%ebp)
	pushl	$LC2
	call	_printf
	addl	$16, %esp
L12:
	leal	-4(%ebp), %eax # 执行radius++
	incl	(%eax)
	jmp	L10
L11:
	subl	$8, %esp       # printf("now radius=%d\n",radius-1);
	movl	-4(%ebp), %eax
	decl	%eax
	pushl	%eax
	pushl	$LC4
	call	_printf
	addl	$16, %esp
	leave
	ret
	.def	_printf;	.scl	2;	.type	32;	.endef


 

<think>根据用户查询提供的引用材料,我将整理breakcontinue语句的核心区别。用户明确要求了解编程语言中这两个语句的区别,且引用内容覆盖了C、Python、Java三种语言的示例。 关键区别归纳: 1. 作用对象:break可作用于循环switch结构;continue仅作用于循环 2. 执行效果:break立即终止整个结构;continue跳过当前迭代剩余代码 3. 嵌套影响:break只终止直接所属的最内层结构;continue不影响外层循环 4. 使用场景:break用于完全退出;continue用于条件性跳过 特别注意引用[1][2]中强调的嵌套循环场景:break只能跳出当前循环层,要跳出多层需配合标记变量;continue则始终只影响当前层迭代。 准备用表格对比核心差异,并给出各语言通用示例。由于用户没有指定语言,示例将包含Python(引用[3])Java(引用[4])片段,同时补充C语言while循环示例说明基础用法。 最后生成相关问题需覆盖:1)语言特性差异 2)嵌套循环处理 3)常见误区 </think>在编程语言中,`break``continue`语句都是用于控制循环流程的关键字,但它们的核心逻辑行为有本质区别: ### 核心区别对比表 | **特性** | **break 语句** | **continue 语句** | |------------------------|----------------------------------------|--------------------------------------| | **主要作用** | 立即终止整个循环或`switch`语句 | 跳过当前迭代剩余代码,进入下轮循环 | | **适用范围** | 循环(`for`/`while`/`do-while`)`switch` | 仅循环结构 | | **执行后的行为** | 循环或`switch`完全结束 | 当前迭代中止,循环条件重新判断 | | **嵌套循环中的影响** | 终止**最内层**循环 | 仅跳过**当前层**循环的本次迭代 | | **典型应用场景** | 搜索到目标后提前退出、错误发生时终止循环 | 过滤特定数据、跳过无效迭代 | ### 代码示例说明 **1. break 终止循环(Python)** ```python for i in range(5): if i == 3: break # 当i=3时完全终止循环 print(i) # 输出: 0 1 2 ``` **2. continue 跳过当前迭代(Java)** ```java for (int i=0; i<5; i++) { if (i == 2) continue; // 跳过i=2的迭代 System.out.print(i + " "); } // 输出: 0 1 3 4 ``` **3. switch 中的 break(C/Java)** ```c switch(num) { case 1: printf("One"); break; // 必须用break防止穿透 case 2: printf("Two"); break; default: printf("Other"); } ``` ### 嵌套循环中的关键差异 ```python for i in range(3): for j in range(3): if j == 1: break # 仅终止内层j循环,i循环继续 print(f"{i},{j}") # 输出: (0,0) (1,0) (2,0) for i in range(3): for j in range(3): if j == 1: continue # 跳过j=1的迭代,继续j=2 print(f"{i},{j}") # 输出: (0,0)(0,2) (1,0)(1,2) (2,0)(2,2) ``` > 在嵌套场景中,`break`仅影响最内层循环,而`continue`仅跳过当前层的本次迭代[^1][^2]。若需跳出多层循环,通常需要配合标志变量(如`found = True` + 外层循环判断)。 ### 使用建议 - 当满足条件后**无需继续任何迭代**时用 `break`(如找到目标值) - 当需要**排除特定情况但继续循环**时用 `continue`(如跳过空值或异常数据) - 在`switch`中务必用 `break` 防止意外穿透到下个`case`[^4] --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值