If Statements

Cards (14)

  • What is Selection?
    Selection is one of three constructs of programming, along with Sequence (logical order) and Iteration (loops)
  • What is an if statement?
    • An if statement is a conditional statement that performs a specific action based on conditional values 
    • Essentially if ‘thing A’ is true, ‘thing B’ will happen
  • How does the if statement below impact the code?
    • If the user answers “yes” to the window question, then an appropriate statement is printed 
    • The double equals (==) stands for ‘is equal to’ 
    • The colon stands for THEN , and the line after an if statement must be indented (by pressing the tab key once)  
    • If the window is not open, nothing will happen in this code if you type “no”
  • What is an elif command?
    • The elif command stands for else if  
    • It can be used in conditional statements to check for multiple conditions
    • Essentially : If ‘thing A’ is true then do ‘thing B’. else if ‘thing C’ is true then do ‘thing D’
  • How does the elif statement below impact the code?
    • An answer of “yes” prints an appropriate statement 
    • But if “yes” is false, the next condition, which is “no” is checked 
    • If true. an alternative appropriate statement is printed
  • What is an else command?
    The else command submits a response if the value is anything else
  • How does the else command below impact the code?
    • If the value is not “yes” or “no”, the else command submits a response  
    • The if and elif commands have a colon at the end, but the else command has it at the start 
    • The else command does not need to be on a new line
  • What are nested if statements?
    • Complex programs may require you to have nested if statements, which are if statements within if statements 
    • In programming, one thing inside another is known as nesting 
    • You must make sure that the related if, elif and else statements line up with each other  
    • You can do this by using the tab key to indent a line
  • How do the nested if statements below affect this code?
    • The program asks the user what the weather is like today 
    • If the weather is sunny, the program asks the user how hot is, and prints an appropriate statement depending on which conditions are true 
    • If the weather is rainy, an alternative appropriate statement is printed 
    • If the answer is neither “rainy” or “sunny”, the else command submits a response
  • Comparison operators
    • Comparison operators can be used can be used within if statements 
    • > means greater than 
    • >= means greater than or equal to 
    • < means less than 
    • <= means less than or equal to 
    • == means equal to (is used to compare a variable's value to a specific number)
  • Logical operators
    • Logical operators can be used can be used within if statements 
    • These include and and or
  • How do the logical operators and comparison operators below affect this code?
    • The program asks the user to input their maths test score 
    • If their score is equal to 50 , the program tells the user they scored top marks 
    • If their score is greater than or equal to 40 and less than 50, the program tells the user they scored a great grade 
    • If their score is greater than or equal to 20 and less than 40, the program tells the user they did okay in the test 
    • Else, any other input is met with a response from the CPU that tells the user they have to try harder next time
  • What is not equal to?
    • The opposite of equal to is not equal to , and it is represented by !=  
    • It is often used with while loops to repeat code when an input is not what is expected (e.g. repeatedly asking for a password while the input is not equal to fluffycat123)
  • How does != affect this code?

    • The code below uses != for an incorrect answer 
    • It could however be re-written to use == for a correct answer