Selection Statements and IF Statements
Selection Statements
Allow a program to change its execution pathway.
Four types:
IF statements (and variations)
SWITCH statement
IF Statement
Foundation for other selection statements.
Can be used in three ways:
Changing the execution path
Directs a program to a different execution path.
Example: London Underground trains terminating at different stations.
Diverting and returning
Temporarily diverts execution to a different path before returning to the main sequence.
Example: Passengers taking a detour before rejoining the original route.
Acting as a guard
Validates input and handles invalid data.
Prevents issues later in the program.
Basic Syntax
Starts with the keyword
if.Followed by a condition in parentheses that evaluates to a boolean value (true or false).
A set of statements enclosed in braces
{}.If the condition is true, the statements inside the braces are executed.
If the condition is false, the statements within the braces are bypassed.
Braces are optional for a single statement, but recommended for minimizing errors.
Case Study: A Perfect Proposal
Scenario: Kelsey wants to propose to Kim only on a perfect day (24 degrees Celsius).
Program needed to enter the day's temperature and determine if it is a perfect day.
Identifiers and Values
Perfect day's temperature: 24 degrees Celsius (constant).
Today's temperature: Varies daily (variable, user input).
Behaviors
User enters today's temperature.
Program informs the user if today is a perfect day.
Selection
IF statement with the equality operator to compare the two values.
Algorithm
Set a constant for the perfect day's temperature (24 degrees Celsius).
Ask the user for the day's temperature and store the response.
Determine if the day is perfect by comparing the user's input to the perfect day's temperature.
Continue with the rest of the program based on the comparison outcome.
Code Implementation
Initialize a constant to store the value of 24 (perfect day).
Stored as a numeric type (e.g.,
int).Identifier in uppercase (common practice for constants).
Prompt the user to enter today's temperature and read their input.
Convert user input (string) into an
intusingConvertToInt32orint32.Parse.
Test if today's temperature matches the perfect day's temperature.
Use
==to test equivalence.
If the condition is true, execute the code within the
ifbraces.Otherwise, the code inside the braces is ignored.
Code after the
ifstatement is executed regardless of the result of theifcondition.
Examples
User enters 24: "What a perfect day" is displayed.
User enters 20: "What a perfect day" is not displayed.
In both cases: "I wonder what tomorrow's temperature will be" is displayed.
Boolean Variables in IF Statements
You can assign a Boolean variable outside of the
ifstatement and use it within theifstatement.Consistency is important for readability.
Avoid using an equivalence operator within the expression (tautology).
Just use the identifier.
Boolean Literals
Avoid using a sole Boolean literal in the
ifstatement.trueliteral: code within the braces will always be executed.falseliteral: code within the braces will never be executed.
These are compilable and executable, but often not useful.
Common Pitfalls
Adding a semicolon directly after the Boolean expression.
No further expressions are dependent on it, likely a bug.
NOT IF Statement
Inverse of the
ifstatement.Block is executed if the temperature does not equal the perfect day.
Two ways to implement:
Use the NOT equivalence operator within the IF statement.
Use the negation operator outside of the IF statement, enclosing it with a set of parentheses.
Generally better to use the first way (easier to read).
Summary
The
ifstatement allows a program's execution to take a different path based on a condition.Condition can be derived from user input, data, or other program states.
The
not ifstatement checks if a condition is false rather than true.
Next Steps
Testing with an
ifstatement to evaluate conditions more effectively.