break continue return
2044 ワード
package com.hna;
import org.junit.Ignore;
import org.junit.Test;
public class CommonTest{
/**
* break
*
*/
@Test
@Ignore
public void testBreak(){
int arr[][] = {{1,2,3},{4,5,6,7},{9}};
boolean found = false;
for(int i=0;i<arr.length && !found;i++) {
for(int j=0;j<arr[i].length;j++){
System.out.println("i=" + i + ",j=" + j);
if(arr[i][j] == 5) {
found = true;
break;
}
}
}
}
/**
* break for ,
*/
@Test
// @Ignore
public void testBreak2(){
int arr[][] = {{1,2,3},{4,5,6,7},{9}};
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[i].length;j++){
System.out.println("i=" + i + ",j=" + j);
if(arr[i][j] == 5) {
break;
}
}
}
}
/**
* , break ,
*/
@Test
@Ignore
public void testBreak3(){
ok:
for(int i=0;i<10;i++) {
for(int j=0;j<10;j++) {
System.out.println("i=" + i + ",j=" + j);
if(j == 5) break ok;
}
}
}
}
package com.hna;
/**
*
* @author lixueli
* break for for
* return
* continue
*/
public class ReturnBreakTest {
public static void main(String [] args){
print();
System.out.println("Print end!");
}
public static void print(){
int num=10;
for(int i=0;i<num;i++){
System.out.println(i);
if(i==2)
continue;// break,return
System.out.println("i="+i);
}
System.out.println("Print starting!");
}
}