Flow control, exceptions and assertions

April 20th, 2008 by Boogie

I got a very low score on the test for flow control, exceptions and assertions. So now I am taking a better look on it and making someĀ  small test programs.

I’m posting them below, maybe somebody else can find them useful.

You are free to use them for your own personal development, but note that all content on this site is copyrighted.

  • For statement

//for (int i = 1, int j = 1; true;){}// does not compile

 

for (int i = 1, j; i < 1;){i++;}// j is not initialized it’s OK, it is never used !!

 

for (int i = 1, j = 1; i < 1;){}

 

int a=2;

for (int i = 1, j = 1; a < 1;){}// the test can operate on different variables

 

for (int i = 1; i<1;);

 

for (int i = 1, j = 1; i < 1 ; out.println(“The increment part” + ” can do something else”)){}

 

for (;;)out.println(“All parts can be omitted!”);

  • Switch statement

byte b = 2;

switch (b){

case 1: break;

//case 128: break;// does not compile !

}

 

int c = 2;

switch (c){

case 1: break;

case 128: break;// OK

}

 

switch (5){// OK

case 1: break;case 128: break;

}

 

switch (5){

case 1: break;

//case 1: break;// does not compile !

}

 

switch (new Integer(5)){// OK

case 1: break;

}

 

switch (new Byte((byte)5)){// OK

case 1: break;

//case 128: break;// does not compile !

}

 

switch (5){

case 1: break;

case 5: out.println(“5 … & falling through”);

default: out.println(“not in here”);

case 6: out.println(“6″);}

  • While statement

//while (boolean b = false){}// does not compile, has to be declared before !

 

boolean b;while (b = false){}// OK

Posted in SCJP |

Leave a Comment

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