Difference between revisions of "Flow Control"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 17: Line 17:
  
 
* General form:
 
* General form:
<nowiki>Insert non-formatted text here
+
<syntaxhighlight lang="java5">
 
switch(expression) {
 
switch(expression) {
 
       case constant1: code block
 
       case constant1: code block
Line 23: Line 23:
 
       default: code block
 
       default: code block
 
}
 
}
</nowiki>
+
</syntaxhighlight>
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 00:24, 19 July 2011

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
}