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
  1. Set a constant for the perfect day's temperature (24 degrees Celsius).

  2. Ask the user for the day's temperature and store the response.

  3. Determine if the day is perfect by comparing the user's input to the perfect day's temperature.

  4. 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 int using ConvertToInt32 or int32.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 if braces.

  • Otherwise, the code inside the braces is ignored.

  • Code after the if statement is executed regardless of the result of the if condition.

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 if statement and use it within the if statement.

  • 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 if statement.

    • true literal: code within the braces will always be executed.

    • false literal: 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 if statement.

  • 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 if statement 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 if statement checks if a condition is false rather than true.

Next Steps

  • Testing with an if statement to evaluate conditions more effectively.