Java: Selection Statments
More Examples: MathBits - Java - Conditionals
If Statement
If statement Syntax
if (boolean expression) {
// the TRUE condition code goes here
}
Examples: 1.5.1 If Statement
If ... else ... Statement
if else statement Syntax
if (boolean expression) {
// the TRUE condition code goes here
}
else {
the FALSE condition code goes here
}
Examples: 1.5.2 If else
If ... else if ... Statement
if else if ... statement Syntax
if (condition #1) {
// the TRUE condition #1 code goes here
} else if (condition #2) {
// the TRUE condition #2 code goes here
} else if (condition #3) {
// the TRUE condition #3 code goes here
} else {
// FALSE for All the above conditions code goes here
}
- You can have as many else if statements as needed.
- only one block of code is executed, once one condition is found to be true the balance of the conditions are not checked and the control of the program is passed to the line after the final } in this block of code.
Example: 1.5.3 if else if Statement
Switch
Example: 1.5.4 Switch Statement