Java跳出循环的区别

1.return 跳出returnAndBreak方法

	// 演示示例
	public void returnAndBreak() {
		int i = 0;
		String interleave = "ON";
		
		System.out.println("==== return ====");
		while (i < 10) {
			System.out.println(" OUT==================== ");
			i ++ ;
			if ( interleave.equalsIgnoreCase("ON") ) {
				return;
			}
			System.out.println("i = " + i);
		}
		System.out.println("==== return ====");
	}
	// 输出结果
	==== return ====
	 OUT==================== 


2.break 跳出while (i < 10) {} 循环

	// 演示示例
	public void returnAndBreak() {
		int i = 0;
		String interleave = "ON";
		
		System.out.println("==== break ====");
		while (i < 10) {
			System.out.println(" ==================== ");
			i ++ ;
			if ( interleave.equalsIgnoreCase("ON") ) {
				break;
			}
			System.out.println("i = " + i);
		}
		System.out.println("==== break ====");
	}
	// 输出结果:
	==== break ====
	 ==================== 
	==== break ====


3.if条件判断 + continue 中断循环进行下一次循环(此例演示的加条件判断后,如果条件满足,后面的步骤就不执行了)

	// 演示示例
	public void returnAndBreak() {
		int i = 0;
		String interleave = "ON";
		
		System.out.println("==== continue ====");
		while (i < 10) {
			System.out.println(" ==================== ");
			i ++ ;
			if ( interleave.equalsIgnoreCase("ON") ) {
				continue;
			}
			System.out.println("i = " + i);
		}
		System.out.println("==== continue ====");
	}
	输出结果:
	==== continue ====
	 ==================== 
	 ==================== 
	 ==================== 
	 ==================== 
	 ==================== 
	 ==================== 
	 ==================== 
	 ==================== 
	 ==================== 
	 ==================== 
	==== continue ====

雕虫小技而言,呵呵

你可能感兴趣的:(J2SE)