Flow Control

From Suhrid.net Wiki
Revision as of 10:32, 9 September 2011 by Suhridk (talk | contribs) (→‎Enhanced for loop)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

If Statement

  • The expression in an if-statement must evaluate to a boolean expression.
  • The else part is optional for an if-statement.
  • The else belongs to the closest preceding if that doesn't have an else. Example:
if(a > b) System.out.println("a > b");
if(a > 1) System.out.println("a > 1");
else System.out.println("a < 1");

//The else belongs to if(a>1)

Switch Statement

  • General form:
switch(expression) {
       case constant1: code block
       case constant2: code block
       default: code block
  • The switch statement has probably the most restrictions on usage.
  • Expression has to evaluate to char, byte, short, int or enum.
  • This implies that only variables that can be implicitly promoted to int can be used in the expression.
  • NO long, float or double.
  • The case constant must evaluate to the same type as the switch expression.
    • This means if the expression returns byte, then the case constants should be within the range of byte (-128 to 127).
    • An enum expression must use enums as the constants.
  • The case constant must be a COMPILE TIME constant.
    • This means that we can use a constant typed literally like case 1: or case 2:
    • Or the variable should be a primitive final variable that is assigned a literal value.
    • An enum value can be used as a case constant, but it should be unqualified..
    • The enum should be included in an import statement and the compiler will internally static import the enum constants for the switch case
    • ALL Wrapper variables cannot be used as case constants even if they are marked as final. Why because compiler will try to unbox the wrapper using Integer.intValue() for e.g. - which wont be a compile time constant.


final int a = 1;
final int b;
b = 2;
int x = process();
switch(x) {
   case a : 
   case 2 : 
   case b : //Error. b is not a compile-time constant
  • Default case is used when the expression doesnt match any of the other case constants. The default case can be located anywhere.


  • while
  • do-while

Basic for loop

  • Basic structure

for(init; condition; iteration) {

 loop body;


  • All three parts are optional; The semicolons are compulsory though.
  • All three parts are independent - they don't need to operate on the same variable.
  • Declaration and Initialization:
    • Multiple variables of the same type can be declared and initialized here.
    • A previously declared can be initialized here as well.
  • Conditional Expression: A single boolean expression. As long as this is true, the for loop keeps executing.
  • Iteration Expression
    • The iteration expression is executed AFTER the loop body runs.
    • Multiple iteration expressions can be specified.
    • Typically an increment or decrement expression.
    • Although any expression can be used here including method calls.

Enhanced for loop

  • Used to loop through an array or a collection. Basically anything that implements Iterable.
  • Has two components.

for(declaration : expression)

  • Declaration : Has to be a newly declared variable of the same type as of the array we are accessing.
  • IMPORTANT: It has to be a different variable name, if a variable with the same name already exists within the same scope.
  • Expression: Must be the array or the collection. Can even be multi-dimensional arrays.

Break and Continue

  • break stops the execution of the loop from which it is called.
  • continue stops only the current iteration of the loop from which it is called and the next iteration of the same loop to start.

Labeled Break and Continue

  • A label is a valid java identifier that ends with a ":"
  • It must be placed just before the statement being labeled.Any statement can be labeled.
  • A labeled break or continue must have the label within the loop - cannot break/continue to labels outside the loop.
  • Continue must be used only within a loop.
  • A continue will cause a for loop to execute the iteration expression, but a break will not.
  • Bizarre: A labeled break can be used outside a loop as well. If you break to a label, it will jump to the end of the statement.