2.4.2 If…Else Statements
In this section, we’ll explore how to make decisions in your Flutter apps using if…else statements. These statements are like the brain of your app, helping it decide what to do based on certain conditions. Let’s dive into the world of decision-making in code!
Understanding If…Else Statements
If…else statements are a fundamental part of programming. They allow your app to make choices by checking conditions. Think of them as a way to ask questions and decide what to do based on the answers.
Basic Syntax
The basic structure of an if…else statement in Dart looks like this:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
- Condition: This is what your app checks. If it’s true, the code inside the first block runs. If it’s false, the code inside the else block runs.
Multiple Conditions with Else If
Sometimes, you need more than two choices. That’s where else if comes in handy. It allows you to check multiple conditions:
int score = 85;
if (score >= 90) {
print('Grade: A');
} else if (score >= 80) {
print('Grade: B');
} else {
print('Grade: C');
}
In this example, the app checks if the score is 90 or higher. If true, it prints “Grade: A”. If not, it checks if the score is 80 or higher, printing “Grade: B” if true. Otherwise, it prints “Grade: C”.
Visualizing If…Else Decisions
To better understand how if…else statements work, let’s visualize the decision-making process using a flowchart:
graph TD;
A[Start] --> B{Is score >= 90?}
B -- Yes --> C[Print "Grade: A"]
B -- No --> D{Is score >= 80?}
D -- Yes --> E[Print "Grade: B"]
D -- No --> F[Print "Grade: C"]
C --> G[End]
E --> G
F --> G
This flowchart shows the path your app takes based on the score. It checks each condition in order and follows the path that matches the score.
Activity: Create Your Own Grading System
Now it’s your turn! Let’s create a simple grading system using if…else statements. Here’s what you’ll do:
- Think of a Subject: Choose a subject you like, such as math or science.
- Set Your Criteria: Decide on the score ranges for different grades (e.g., A, B, C).
- Write Your Code: Use if…else statements to print the grade based on a score.
Here’s a template to get you started:
int score = 75; // Change this score to test different outcomes
if (score >= 90) {
print('Grade: A');
} else if (score >= 80) {
print('Grade: B');
} else if (score >= 70) {
print('Grade: C');
} else {
print('Grade: D');
}
Experiment and Explore
Try changing the score variable to see how the output changes. You can also modify the grading criteria to create your own unique grading system. What happens if you add more else if conditions? Experiment and have fun!
Best Practices and Tips
- Keep Conditions Simple: Start with simple conditions and gradually add complexity as you become more comfortable.
- Test Different Scenarios: Change the values to see how your code behaves under different conditions.
- Use Comments: Add comments to your code to explain what each part does. This helps you and others understand your logic.
Common Pitfalls
- Forgetting the Else: If you don’t include an else statement, your app won’t do anything if none of the conditions are true.
- Misplacing Braces: Ensure that each if, else if, and else block has its own set of braces to avoid errors.
By mastering if…else statements, you’re equipping your app with the ability to make smart decisions. Keep experimenting, and soon you’ll be creating even more complex logic in your Flutter apps!
Quiz Time!
### What is the purpose of an if...else statement in programming?
- [x] To make decisions based on conditions
- [ ] To repeat a block of code
- [ ] To store data
- [ ] To define a function
> **Explanation:** If...else statements are used to make decisions in code by evaluating conditions and executing different blocks of code based on whether the conditions are true or false.
### Which part of an if...else statement checks the condition?
- [x] The if part
- [ ] The else part
- [ ] The print statement
- [ ] The variable declaration
> **Explanation:** The if part of the statement checks the condition to determine which block of code to execute.
### What will the following code print if the score is 85?
```dart
int score = 85;
if (score >= 90) {
print('Grade: A');
} else if (score >= 80) {
print('Grade: B');
} else {
print('Grade: C');
}
```
- [ ] Grade: A
- [x] Grade: B
- [ ] Grade: C
- [ ] Grade: D
> **Explanation:** The score is 85, which satisfies the condition `score >= 80`, so the code prints "Grade: B".
### How can you add more conditions to an if...else statement?
- [ ] By using a loop
- [x] By using else if
- [ ] By using a function
- [ ] By using a variable
> **Explanation:** You can add more conditions to an if...else statement by using else if to check additional conditions.
### What happens if none of the conditions in an if...else statement are true?
- [ ] The program crashes
- [ ] The first block of code runs
- [x] The else block runs
- [ ] Nothing happens
> **Explanation:** If none of the conditions are true, the else block runs, executing its code.
### What is the output of this code if the score is 95?
```dart
int score = 95;
if (score >= 90) {
print('Grade: A');
} else if (score >= 80) {
print('Grade: B');
} else {
print('Grade: C');
}
```
- [x] Grade: A
- [ ] Grade: B
- [ ] Grade: C
- [ ] Grade: D
> **Explanation:** The score is 95, which satisfies the condition `score >= 90`, so the code prints "Grade: A".
### Which of the following is a common mistake when using if...else statements?
- [ ] Using too many conditions
- [ ] Using comments
- [x] Forgetting to use braces
- [ ] Using variables
> **Explanation:** A common mistake is forgetting to use braces to define the blocks of code for each condition, which can lead to errors.
### What is the purpose of the else block in an if...else statement?
- [ ] To check another condition
- [x] To execute code if no conditions are true
- [ ] To store data
- [ ] To define a function
> **Explanation:** The else block executes its code if none of the preceding conditions are true.
### True or False: You can have multiple else blocks in an if...else statement.
- [ ] True
- [x] False
> **Explanation:** You can only have one else block in an if...else statement. However, you can have multiple else if blocks.
### True or False: If...else statements can only be used with numbers.
- [ ] True
- [x] False
> **Explanation:** If...else statements can be used with any data type that can be evaluated in a condition, not just numbers.