精讲java语言中return,continue,break的区别

1.break的用法

        break语句主要用于switch语句和循环结构。在循环结构中,如果执行了break语句,那么就退出循环,接着执行循环结构下面的第一条语句。如果是嵌套循环,那么break就只跳出它所在的那一层循环,对于外层的任何一个循环都无能为力。

public class BreakDemo {
	public static void break1 (){
		for(int i = 0; i < 10; i++){
			for(int j = 0; j < 10 ;j++){
				System.out.println("这是第"+j+"次输出");
				break;//这里的break只可以退出内层的for循环
			}
		}
	}
        public static void main(String[] args) {
		break1();
	}
}
运行结果

其运行结果如上图。分析:在每次进入内层for循环的时候,j的值均为0,而输出完结果之后就直接由break语句把内层for循环跳出,又由于外层for循环要循环十次,所以就有十个输出结果

2continue的用法:

    continue语句并没有真正的退出循环,而是只结束了本次循环的执行,开始对下一个循环条件进行判断,

for循环下的continue用法实例代码如下:

package com.xunhuanDemo;

public class Continue {
	public static void continue1(){
		for(int i = 0; i < 10; i++){
			for (int j = 0; j < 10; j++) {
				if(j == 7){//如果把这里的if换成while运行结果是有区别的
					continue;
				}
				System.out.println("this is j:"+j);
			}
			System.out.println("this is i"+i);
		}
	}
	public static void main(String[] args) {
		continue1();
	}
}

运行结果如下:

this is j:0
this is j:1
this is j:2
this is j:3
this is j:4
this is j:5
this is j:6
this is j:8
this is j:9
this is i0
this is j:0
this is j:1
this is j:2
this is j:3
this is j:4
this is j:5
this is j:6
this is j:8
this is j:9
this is i1
this is j:0
this is j:1
this is j:2
this is j:3
this is j:4
this is j:5
this is j:6
this is j:8
this is j:9
this is i2
this is j:0
this is j:1
this is j:2
this is j:3
this is j:4
this is j:5
this is j:6
this is j:8
this is j:9
this is i3
this is j:0
this is j:1
this is j:2
this is j:3
this is j:4
this is j:5
this is j:6
this is j:8
this is j:9
this is i4
this is j:0
this is j:1
this is j:2
this is j:3
this is j:4
this is j:5
this is j:6
this is j:8
this is j:9
this is i5
this is j:0
this is j:1
this is j:2
this is j:3
this is j:4
this is j:5
this is j:6
this is j:8
this is j:9
this is i6
this is j:0
this is j:1
this is j:2
this is j:3
this is j:4
this is j:5
this is j:6
this is j:8
this is j:9
this is i7
this is j:0
this is j:1
this is j:2
this is j:3
this is j:4
this is j:5
this is j:6
this is j:8
this is j:9
this is i8
this is j:0
this is j:1
this is j:2
this is j:3
this is j:4
this is j:5
this is j:6
this is j:8
this is j:9
this is i9

这里发现,每一次输出j是没有等于7的时候,是因为当j == 7时,就因为continue语句的原因直接跳出了内层循环。但是不影响下面当j ==8,9,10时候的输出。因为continue只是使得程序跳出当前本次条件下的一层循环,并不会对接下来的循环造成影响。

while循环下的continue代码实例如下:

package com.xunhuanDemo;

public class Continue {
	/*public static void continue1(){
		for(int i = 0; i < 10; i++){
			for (int j = 0; j < 10; j++) {
				if(j == 7){//如果把这里的if换成while运行结果是有区别的
					continue;
				}
				System.out.println("this is j:"+j);
			}
			System.out.println("this is i"+i);
		}
	}*/
	public static void continue2(){
		int i = 0;
		while(i < 10){
			i++;
			if(i == 5){
				continue;
			}else{
				System.out.println(i);
			}
		}
		
	}
	public static void main(String[] args) {
		continue2();
	}
}

运行结果如下:

由运行结果可以看出,while循环中使用continue和for循环中使用continue的效果是一样的,都是跳出本次本条件下的循环,对之后的循环没有造成影响。

3.return语句

  如果在程序中遇到return语句,那么代码就退出该函数的执行,返回到函数的调用处,如果是main()函数,那么结束整个程序的运行。

代码如下:

package com.xunhuanDemo;

public class returnDemo {
	public static void return1(){
		for (int i = 0; i < 10; i++) {
			System.out.println("i的值为:"+i);
			if(i == 6){
				return;
			}
		}
		System.out.println("第6次的时候已经去了主方法");
	}
	public static void main(String[] args) {
		return1();
		System.out.println("第6次之后,这句话输出");
	}
}

其运行结果如下:

可见,当i == 6时,由于return语句的作用,使得函数return1()不再向下运行,而是直接返回到了主方法main()方法,然后输出了main方法中的那句话。

猜你喜欢

转载自blog.csdn.net/My_name_is_ZwZ/article/details/81511641
今日推荐