Java中关键字continue、break和return的区别

时间:2025-01-28 07:00:47

Java中关键字continue、break和return的区别:

continue:跳出本次循环继续下一次循环

break:   跳出循环体,继续执行循环外的函数体

return:   跳出整个函数体,函数体后面的部分不再执行

有代码才有真相:

<span style="font-family:SimSun;font-size:14px;"><span style="font-family:SimSun;font-size:14px;">public class JavaBese {
	public static void main (String[] args) {	
		int j = 3;
		for (int i = 0; i < 5; i++) {
		if (i == j)  {
			continue;
		 }
       		 ("i = " + i);
	}
	("循环结束");
	}
}</span></span>

打印结果为:

i = 0

i = 1

i = 2

i = 4

循环结束

如果是break,打印结果为:

i = 0

i = 1

i = 2

循环结束

如果是return, 打印结果为:

i = 0

i = 1

i = 2