Flow control, exceptions and assertions 2

April 20th, 2008 by Boogie

Continuing going through the chapter 5 from the SCJP book, I find :

  • The “Enhanced For Loop”

int[] a = {1,2,3};

for (int n : a)

out.println(“next element: “ + n);

 

int[][] a2 = {{1},{1,2},{3}};

for (int[] n : a2)

out.println(“next element: “ + n);

for (int n : a2[1])

 

out.println(“next element: “ + n);

for (Object n : a2)out.println(“next element: “ + n);

  • Break and Continue statements

//labl1: int a = 1;// does not compile

 

int a;

labl1: a = 1;// OK, you can label this instruction

 

for (int i=1;i < 5; i++){

//break labl1;// does not compile

}

 

LblOuter:

for (int i=1;i < 5; i++){

LblInner:

for (int j=1;j < 5; j++){

if (((i+j)%6) == 0){

out.println(“Breaking out of LblOuter”);break LblOuter;

}

if (((i+j)%3) == 0){

out.println(“Breaking out of LblInner”);break LblInner;

}

out.println(“Going to the next iteration of LblInner”);

continue LblInner;

}

}

    Posted in Uncategorized |

    Leave a Comment

    Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.