Java   View all facts   Glossary   Help
syntactic unit > statement > control flow statement > decision making statement
Next control flow statement: exception statement    Up: control flow statement    Previous control flow statement: continue statement   

decision making statement comparison table
Subject avoid by omit have example have syntax have purpose is a subtopic of is a synonym of is a kind of have part
if statement braces if there is only one embedded statement 
if  (boolean expression) {
embedded statement(s)
}
to make a decision on which branch of code to follow in a programLoops and Decision Making decision making statementboolean expression
if-else statement braces if there is only one embedded statement
int testscore; 
char grade;
if (testscore <= 90) {
grade = 'A';
} else if (testscore <= 80) {
grade = 'B';
} else {
grade = 'C';
}
if (boolean expression) {
if-true statement
else
if-false statement
}
to make a decision on which branch of code to follow in a programLoops and Decision Making decision making statementelse block
switch-case statementusing polymorphismthe keyword default and the default statements if they are not needed
int month;
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
default: System.out.println("Not January, February or March"
}
switch(integer producing expression)
{
case integer constant 1:
// statements to execute for integer 1
break;
case integer constant 2:
// statements to execute for integer 2
break;
...
default:
// statements to execute if none of the above is true
break;
}
to execute one of a choice of statements depending on the value of a variableLoops and Decision Makingcase statementdecision making statement 
try-catch-finally statement  
//Any division by zero that occurs when executing the try block will result in execution of the catch block. 
//Once either block completes, execution continues at the statement after the catch block.
try
{
result = numerator / denominator;
validResult = true;
}
catch (ArithmeticException e)
{
validResult = false;
}
try{
statements
} catch (exception_type1 identifier1) {
statements
} catch (exception_type2 identifier2) {
statements
...
} finally {
statements
}
to handle exceptionsException Handling exception statementtry block, catch block(s), finally block

Next control flow statement: exception statement    Up: control flow statement    Previous control flow statement: continue statement