Selection
- Selection statements are used to control the flow of a program by executing specific blocks of code based on certain conditions.
- They enable programs to make decisions, allowing for dynamic and flexible behaviour.
- Imagine an ATM.
- It needs to check if your card is valid and if your PIN is correct before allowing access to your account.
Types of Selection Statements
Simple if statements: Execute a block of code if a condition is true.
if (age < 18){
System.out.println("You are not eligible to vote.");
}- The condition age $< 18$ is evaluated.
- If true, the message "You are not eligible to vote." is printed.
- Always ensure that the condition inside the if statement is a boolean expression that evaluates to true or false.
- When programming, avoid using the assignment operator (=) instead of the equality operator (==) in conditions.
if...else statements: Provide an alternative block of code if the condition is false.
if (age < 18){
System.out.println("You are not eligible to vote.");
} else {
System.out.println("You are eligible to vote.");
}- The condition age $< 18$ is evaluated.
- If true, the message "You are not eligible to vote." is printed.
- If false, the else block is executed, printing "You are eligible to vote."
- The else block is optional.
- If omitted, the program simply skips the if block if the condition is false.
Compound if...else statements: Handle multiple conditions using else if clauses.
if (points > 90){
grade = "A";
} else if (points > 60){ \\ if points in (60, 90]
grade = "B";
} else{
grade = "C";
}- The program checks each condition in order.
- Once a true condition is found, the corresponding block is executed, and the rest are skipped.
The else block at the end is optional but useful for handling cases where none of the previous conditions are met.
Constructing Code with Selection Statements
Step-by-step approach:
- Identify the problem: Determine what decision needs to be made.
- Define conditions: Clearly specify the conditions that will guide the decision.
- Choose the appropriate structure: Decide between simple if, if...else, or compound if...else statements.
- Implement and test: Write the code and test it with different inputs to ensure it behaves as expected.
Imagine a temperature warning system:
- Problem: Display a warning if the temperature is too high or too low.
- Possible code:
public class Main{
public static void main(String[] args){
double temperature = 10.0;
if (temperature > 30){
System.out.println("Too hot!");
} else if (temperature <0){
System.out.println("Too cold!");
} else {
System.out.println("Temperature is normal.");
}
}
}
- The program checks if the temperature is greater than 30.
- If it is true, the it prints "Too hot!"
- If not, it checks if the temperature is less than 0.
- If it is less, then it prints "Too cold!"
- If neither condition is true, it prints "Temperature is normal."
- Corresponding flow chart:
- When writing compound if...else statements, order your conditions from most specific to least specific.
- This ensures that the correct block is executed first.
- Keep conditions simple: Complex conditions can be difficult to read and debug. Break them into smaller parts if necessary.
- Use else if wisely: Ensure that each else if condition is mutually exclusive to avoid unexpected behaviour.
- Test all scenarios: Always test your code with different inputs to cover all possible paths.
- Can you identify the logical operators used in the ATM access example?
- How would you modify the grading system example to include a new grade category for scores above 95?
- How do selection statements in programming mirror decision-making processes in real life?
- What are the limitations of relying solely on logical conditions to make decisions?