1. Decision Making
Decision-making structures have one or more conditions to be evaluated or tested by the program. Statements are executed when a condition is true and, optionally, other statements are executed when the condition is false.
The general idea is:
Execute Conditional Code
Java provides several decision-making statements:
ifif...elseif...else if...else- Nested
if...else switch
2. if Statement
An if statement consists of a Boolean expression followed by one or more statements.
Syntax
if(Boolean_expression) {
// Statements will execute if the Boolean expression is true
}
If the Boolean expression evaluates to true, the block of code inside the if statement is executed. If it evaluates to false, execution continues with the first set of code after the if statement.
Example
public class Test {
public static void main(String args[]) {
int x = 10;
if( x < 20 ) {
System.out.print("This is if statement");
}
}
}
Output
This is if statement.
if statement executes its block only when its Boolean condition is true.
3. if...else Statement
An if statement can be followed by an optional else statement. The else block executes when the Boolean expression is false.
Syntax
if(Boolean_expression) {
// Executes when the Boolean expression is true
} else {
// Executes when the Boolean expression is false
}
If the Boolean expression evaluates to true, the if block is executed; otherwise, the else block is executed.
Example
public class Test {
public static void main(String args[]) {
int x = 30;
if( x < 20 ) {
System.out.print("This is if statement");
} else {
System.out.print("This is else statement");
}
}
}
Output
This is else statement
4. The if...else if...else Statement
An if statement can be followed by optional else if and else statements. This is useful for testing multiple conditions.
Important Rules
- An
ifcan have zero or oneelse, and theelsemust come after anyelse ifblocks. - An
ifcan have zero to manyelse ifblocks, and they must come before theelse. - Once an
else ifcondition succeeds, the remainingelse ifandelseblocks are not tested.
Syntax
if(Boolean_expression 1) {
// Executes when Boolean_expression 1 is true
} else if(Boolean_expression 2) {
// Executes when Boolean_expression 2 is true
} else if(Boolean_expression 3) {
// Executes when Boolean_expression 3 is true
} else {
// Executes when none of the above conditions is true
}
Example
public class Test {
public static void main(String args[]) {
int x = 30;
if( x == 10 ) {
System.out.print("Value of X is 10");
} else if( x == 20 ) {
System.out.print("Value of X is 20");
} else if( x == 30 ) {
System.out.print("Value of X is 30");
} else {
System.out.print("This is else statement");
}
}
}
Output
Value of X is 30
5. Nested if...else
It is legal to nest if and else if statements. This means one conditional statement can appear inside another conditional statement.
Syntax
if(Boolean_expression 1) {
// Executes when Boolean_expression 1 is true
if(Boolean_expression 2) {
// Executes when Boolean_expression 2 is true
}
}
else if...else can also be nested in a similar way.
Example
public class Test {
public static void main(String args[]) {
int x = 30;
int y = 10;
if( x == 30 ) {
if( y == 10 ) {
System.out.print("X = 30 and Y = 10");
}
}
}
}
Output
X = 30 and Y = 10
6. switch Statement
A switch statement allows a variable/expression to be tested for equality against a list of values. Each value is called a case.
Syntax
switch(expression) {
case value:
// Statements
break; // optional
case value:
// Statements
break; // optional
// You can have any number of case statements.
default: // Optional
// Statements
}
How switch Works
The switch expression is evaluated. Java compares its value with the available case labels. When a matching case is found, its statements execute. A break can terminate the switch; without a break, execution can fall through into subsequent cases.
7. Rules of a switch Statement
| Rule | Description |
|---|---|
| Allowed switch types | The source lists integers, convertible integer types such as byte, short, char, strings and enums. |
| Number of cases | Any number of case statements can be used. |
| Case value | A case is followed by the value to compare and a colon. |
| Matching type | The case value must correspond to the switch expression's type and be a constant/literal. |
| Matching case | When the switch value equals a case, statements following that case execute. |
| break | When reached, break terminates the switch and control moves to the next statement after it. |
| Fall-through | If no break appears, execution continues into subsequent cases until a break is reached. |
| default | Optional case used when none of the other cases matches. It normally appears at the end. |
break is optional in an individual case, but omitting it intentionally or accidentally can cause fall-through behavior.
8. switch Example
public class Test {
public static void main(String args[]) {
// char grade = args[0].charAt(0);
char grade = 'C';
switch(grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
case 'C':
System.out.println("Well done");
break;
case 'D':
System.out.println("You passed");
case 'F':
System.out.println("Better try again");
break;
default:
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}
Output
Well done Your grade is C
'B' and 'C' share the same statement block. Because grade is 'C', the program prints Well done and then exits the switch at the following break.
9. Quick Revision
| Statement | Purpose |
|---|---|
if | Executes a block when a Boolean condition is true. |
if...else | Chooses between two blocks based on a condition. |
if...else if...else | Tests multiple conditions from top to bottom. |
Nested if | Places one conditional statement inside another. |
switch | Tests an expression against multiple case values. |
case | Defines a possible matching value inside switch. |
break | Terminates switch execution and prevents further fall-through. |
default | Runs when none of the cases matches. |
Decision-Making Flow
Condition
|
+---- true ----> Execute matching block
|
+---- false ---> Check next condition / else
|
+---- switch default if no case matches