Chapter 5: Flow Control, Exceptions and Assertions Flashcards
To which IF belongs the else statement?
if(exam.done()) //1
if(exam.getScore() < 0.61) //2
System.out.println(“Try again”)
else System.out.println(“java master”)
2
The most inner if.
What is is the result of the following expression? int y = 5; int x = 2; if((x > 3) && (y < 2) | true) { System.out.println("true") }
It will print nothing.
x is not greather than 3, && is a short circuit operator so it will not evaluate the rest of the statements.
What is the result of the following?
if(b = true) {} // 1 if(x = 2) {} // 2
1, evaluates to true. An assingment wil return the assigned value.
2, compiler error 2 cannot be evaluated to a boolean
What are valid evaluation types for a switch statement?
Primitive types that can be implicitly cast to int (char, byte, short, int) or Enum.
What is the result of the following code? int b = 2; int x = 0; switch (x) { case b: System.out.println("b"); break; case 2 > x: break; }
Compiler error!
A case constant must evaluate to the same type and must be a compile time constant! (mark it final or use a direct literal).
2 > x is not valid in a switch. It can only check for equality.
What is the result? byte b = 2; switch(b) { case 128: }
Compiler error!
128 doesn’t fit into a byte.
What is the result? int x = 10; switch(x) { case 20: case 40: case 40: }
Compiler error!
You can’t define the same case statements multiple times.
What does the break keyword do in a switch statement?
It will break the fall through of the switch cases. The first case evaulated to be equal en will execute all following cases until a break; is found.
What is the result? int x = 2; switch(x) { case 2: System.out.println("2"); default: System.out.println("default"); case 3: System.out.println("3"); }
2, default, 3
The default case works just like any other case for fall through. The only difference is, the case itself will only match with the values that are not provided as cases.
What is the result?
do {
System.out.println(“loop”);
} while (false);
It will print loop once.
The do-while executes once and checks the expression afterwards. If evaluated to true it will execute again.
What is the result?
for(int x = 0, y = 1; (x > 0), (y > 5); x++, System.out.println(“loop”)) {
System.out.println(“test”);
}
Compiler error!
The for-loop contains three parts (seperated by a ;). The 1st (Declaration and Initialization) may contain multiple statements seperated by a ,. The same applies for the third part, the Iteration Expression. The third part can conain any piece of code, not just increment operations. The second part may only contain a single expression!
What can a loop cause to stop (apart from the condition conditioning to false)?
break, return, System.exit(); or an Exception.
What is the result?
for( ; ; ) {
System.out.println(“test”);
}
An endless loop.
The three parts are all optional.
What does the continue keyword do (unlabeled context)?
It will move on to the next iteration of the inner most loop.
What is the result? outer: for(int i = 0; i < 5; i++) { while(true) { someLabel: System.out.println("inner"); break outer; } System.out.println("outer"); } System.out.prinln("end");
It will print “inner end”
The break statement is a labelled break. And will break out of the outer loop instead of the inner loop if used without a label.
someLabel points to a statement. But can’t be called upon, but is perfectly valid (but is has no functional use).