Selection Statements

Cards (15)

  • Selection Statements

    These statements enable developers to create dynamic and responsive code by allowing the program to make decisions (selection) and repeat tasks (iteration) based on specified conditions.
  • Iteration Statements

    These statements enable developers to create dynamic and responsive code by allowing the program to make decisions (selection) and repeat tasks (iteration) based on specified conditions.
  • Selection and Iteration Statements
    • Fundamental programming constructs that control the flow of a program's execution
    • Enable developers to create dynamic and responsive code
  • IF Statement

    1. Check condition
    2. If condition is true, execute statements inside "if" body
    3. If condition is false, skip statements inside "if" body
  • IF Statement

    • If age >= 18, print "You are eligible to vote."
  • If else Statement

    1. Check condition
    2. If condition is true, execute statements inside "if" body
    3. If condition is false, execute statements inside "else" body
  • If else Statement

    In this case, since age is 16, the message "You are not eligible to vote." will be printed.
  • else...if Statement


    1. Check multiple conditions
    2. Execute first matching condition's statements
  • else...if Statement
    Here, the message "Grade: B" will be printed because the score is 85.
  • Nested if else Statement

    • When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.
    • Nesting is the practice of enclosing several if-else statements within an if-else statement
  • Nested if else Statement

    This example will print "Positive even number" because number is 10, which is positive and even.
  • Switch Statements 

    • Used when there are multiple options and a different task needs to be performed for each option
    • Each value is called a case, and the code associated with the first matching case is executed
    • If no match is found, an optional default case can be executed
  • Switch Statements

    This code will print "Thursday" because the day is 4.
  • Break Statement

    • Used to terminate a switch statement or a loop immediately
    • When encountered within a case block, the control exits from the switch statement
  • Default Case


    Executed in a switch statement if none of the case values match the switch expression