Decision-Making Statements
Decision-making statements allow a program to choose different paths of execution based on certain conditions.
Purposes
Section titled “Purposes”- They are essential for controlling the flow of a program and making it respond to different situations.
- To execute different code blocks depending on whether a condition is true or false
- To check boolean expressions and control program logic
How They Work
Section titled “How They Work”- A condition or boolean expression is evaluated
- If the condition is true, a specific block of code is executed
- If the condition is false, another block of code may be executed or skipped
Common Decision-Making Statements
Section titled “Common Decision-Making Statements”If-Else vs. Switch
Section titled “If-Else vs. Switch”| Feature | If-Else | Switch |
|---|---|---|
| Readability | Better for different variables or complex conditions | Better for many conditions on a single variable |
| Flexibility | Can check any boolean condition | Limited to equality checks against constants |