switch and loops rules!

Writing code using the Switch Statements:

  • The only legal expression in an if statement is a Boolean expression, in other words an expression that resolves to a Boolean or Boolean variable.
  • Without for Boolean assignments (=) that cane mistaken fir Boolean equality (==) tests

Boolean x = false;

If (x = true) {  } // an assignment, x will always be true.

  • Curly braces are optional for if blocks that have only one conditional statement. But watch out misleading indications.
  • Switch statements can evaluate only Enums or byte, short, int and char data types. You can’t say

long s = 30;

switch(s){     }

  • The case constant must be literal or final variable, or a constant expression, including an enum. You cannot have a case that includes a non final variable, or a range of values.
  • If the condition in switch statement matches a case constant, execution will run through all code in the switch following the matching case statement until a break statement ir the end of the switch statement encountered.
  • In other words, the matching case is just the entry point int to the case block, but unless there’s a break statement, matching case is only case code that runs.
  • The default key word should be used in a switch statement if you want to run some code when none of the case values match the conditional values.
  • The default block can be located anywhere in the switch block, so if no case matches, the default block will be entered, and if the default doesn’t contain a break, the code will continue to execute (fail through) to the end of the switch or until the break statement is encountered.

Writing code using loops:

  • A basic for statement has three parts: declaration and/or initialization, Boolean evaluation, and the iteration expression.
  • If a variable incremented or evaluated with in a basic for loop, it must be declare before the loop, or within the for loop declaration.
  • A variable declared (not just initialized) within the basic loop declarations cannot be accessed outside the for loop (in other words, code bellow the for loop won’t be able to use the variable).
  • You can initialize more than one variable of the same type in the first part of basic for loop declarations; each initialization must separate with a coma.
  • An enhanced for statement (new as of Java 5), has two parts, the declarations and the expression. It is used only to loop through the arrays or collections.
  • Which has enhanced for, the expression is the array or collection through which you want to loop.
  • With an enhanced for, the declaration is the block variable, whose type is compatible with the elements of the array or collection, and that variable contains the value of the element for the given iteration.
  • You cannot use number (old C-Style language construct) or anything that does not evaluate to a Boolean value as a condition of an if statement or looping construct. You can’t for example say if(x) unless if the is Boolean value.
  • The do loop will enter body of the loop at least once, even if the test condition is not met.

Using break and Continue:

  • An unlabeled break statement will cause the current iteration of the increment looping construct to stop and the line of code following the loop to run.
  • An unlabeled continues statement will cause: the current iteration of the inner most loop to stop, the condition of that loop to be checked, and if the condition is met, the loop to run again.
  • If the break statement or the continue statement is labeled it will cause similar action to occur on the labeled loop not eh increment loop.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s