Conditional Statements PDF

Summary

This document provides an overview of conditional statements in programming. It explains the concepts using flowcharts and pseudocode. The examples focus on determining if a number is even or odd.

Full Transcript

Programmatic thinking Conditional statements Please do not copy without permission. © ExploreAI 2023. Programmatic thinking Programmatic thinking tools Recap: Algorithms Operators Specific procedures to solve problems in terms of the Compariso...

Programmatic thinking Conditional statements Please do not copy without permission. © ExploreAI 2023. Programmatic thinking Programmatic thinking tools Recap: Algorithms Operators Specific procedures to solve problems in terms of the Comparison operators are used to compare numbers required actions and the order in which these actions or strings to perform the evaluation within a boolean are executed. expression. Boolean operators are used as Operators are an important tool that algorithms use to conjunctions to combine (or exclude) statements in a make decisions. boolean expression. Flowcharts Pseudocode Used to visually represent the flow of control of A sequence of steps and actions in plain natural logic, algorithms, pseudocode, and conditional language – these are step-by-step descriptions for an statements. algorithm using short but descriptive phrases. Next up: Conditional statements 2 Programmatic thinking Conditional statements | Conditional statements allow us to represent decision-making by setting specific conditions. A statement of “If x, then yˮ is a _conditional statement_, If x, x where “xˮ is the hypothesis (or condition) then y and “yˮ the conclusion (or consequent). An important part of formal logic is understanding how to use and interpret conditional statements. Furthermore, conditional statements play a critical role in solving problems and writing good code. 3 Programmatic thinking Conditional statement example Letʼs consider the following example: Using programmatic tools Determine whether a number x is an even or odd number. together allows us to represent Weʼve already seen that we can visually represent this as a flowchart and a problem and its solution more write down the steps in pseudocode. However, we may not have effectively. recognised that we were already using a conditional statement. Flowchart Pseudocode Start If Start x % 2 == 0 then - y = “Even” False If x % 2 == 0 then Else x % 2 Odd == 0 - y - y = = “Odd” “Even” Elseif End True - y = “Odd” End if Even 4 Programmatic thinking Conditional statement example Letʼs again consider the following example: Using programmatic tools Determine whether a number x is an even or odd number. together allows us to represent Weʼve already seen that we can visually represent this as a flowchart and a problem and its solution more write down the steps in pseudocode. However, we may not have effectively. recognised that we were already using a conditional statement. Flowchart Pseudocode This is an example of the same if-else statement represented in Start two different ways. If Start x % 2 == 0 then - y = “Even” False If Elsex % 2 == 0 then x % 2 Odd == 0 - - yy = = “Even” “Odd” Else End if True - y = “Odd” End if Even 5 Programmatic thinking If statement | The if statement is the fundamental decision-making statement. Specific code is executed when the condition is met; if not, nothing is executed. Pseudocode Flowchart We start the process and set the decision point, which is the Start Start condition x % 2 == 0. If x % 2 == 0 then - what needs to happen when the False condition is true x % 2 End End if == 0 End 6 Programmatic thinking If statement | The if statement is the fundamental decision-making statement. Specific code is executed when the condition is met; if not, nothing is executed. Pseudocode Flowchart We start the process and set the decision point which is the Start Start condition x % 2 == 0. If x % 2 == 0 then When the condition is True, then - what needs to happen if the False y is equal to “Evenˮ. - y = “Even” condition is true x % 2 End End if == 0 End True y = “Even” 7 Programmatic thinking If statement | We can also set the output to a default value up front so that the output still has a value even when the condition is False. Pseudocode Flowchart We start the process and set y to being equal to “Oddˮ by default. Start Start If x % 2 == 0 then The condition is still x % 2 == 0. - y = “Even” If x % 2 == 0 then y = “Odd” Else - what - yneeds = “Odd” to happen when the condition is true End if End if x % 2 == 0 End 8 Programmatic thinking If statement | We can also set the output to a default value up front so that the output still has a value even when the condition is False. Pseudocode Flowchart We start the process and set y to being equal to “Oddˮ by default. Start Start If x % 2 == 0 then The condition is still x % 2 == 0. - y = “Even” If x % 2 == 0 then y = “Odd” When the condition is True, then Else - what y is equal to “Evenˮ. - yneeds = “Odd” to happen if the - ycondition = “Even”is true End if End if x % 2 End == 0 End True y = “Even” 9 Programmatic thinking If statement | We can also set the output to a default value up front so that the output still has a value even when the condition is False. Pseudocode Flowchart We start the process and set y to being equal to “Oddˮ by default. Start Start If x % 2 == 0 then The condition is still x % 2 == 0. y = “Odd” - y = “Even” If x % 2 == 0 then y = “Odd” When the condition is True, then Else - what y is equal to “Evenˮ. - yneeds = “Odd” to happen if the - ycondition = “Even”is true End if False End if x % 2 Now, if the condition is False, End then y is equal to the default == 0 End value of y, which is “Oddˮ. True y = “Even” 10 Programmatic thinking If-else statement | An if-else statement can be used in order to specify a block of code to be executed if the condition in the if statement is false, without setting a default value up front. Pseudocode Flowchart The condition is still x % 2 == 0. Start Start If x % 2 == 0 then If x - % y 2= == 0 then “Even” - what Else needs to happen when the x % 2 condition is true == 0 - y = “Odd” Else End if - what needs to happen when the condition is false End if End End 11 Programmatic thinking If-else statement | An if-else statement can be used in order to specify a block of code to be executed if the condition in the if statement is false, without setting a default value up front. Pseudocode Flowchart The condition is still x % 2 == 0. Start Start When the condition is True, then If x % 2 == 0 then y is equal to “Evenˮ. If - x y % =2 “Even” == 0 then True - what - yElse x % 2 needs to happen if the = “Even” condition is true == 0 - y = “Odd” Else End if - what needs to happen when the condition is false y = “Even” End if End End 12 Programmatic thinking If-else statement | An if-else statement can be used in order to specify a block of code to be executed if the condition in the if statement is false, without setting a default value up front. Pseudocode Flowchart The condition is still x % 2 == 0. Start Start When the condition is True, then If x % 2 == 0 then y is equal to “Evenˮ. If -x y% =2 “Even” == 0 then True False - what - yElse x % 2 needs to happen if the = “Even” When the condition is False, then condition is true == 0 - y = “Odd” y is equal to “Oddˮ. ElseEnd if - what needs to happen if the - ycondition = “Odd” is false y = “Even” y = “Odd” End if End End 13 Programmatic thinking Nested if statement | We use nested if statements when we must decide on a combination of conditions before deciding the next action or final output. Example: Pseudocode Since we can only calculate the Start modulus of numbers, letʼs add 01. If x is a number then another condition: - what needs to happen when the condition is true Determine whether a variable x is an even or odd number. Set the condition to see if the End if 01. input variable x is a number. End 14 Programmatic thinking Nested if statement | We use nested if statements when we must decide on a combination of conditions before deciding the next action or final output. Example: Pseudocode When the condition is True, Since we can only calculate the Start 02. then we determine whether it modulus of numbers, letʼs add is even or odd. 01. If x is a number then another condition: - what needs to happen when the condition is true_ 02. Determine whether a variable x is an even or odd number. Set the condition to see if the End if 01. input variable x is a number. End 15 Programmatic thinking Nested if statement | We use nested if statements when we must decide on a combination of conditions before deciding the next action or final output. Example: Pseudocode When the condition is True, Since we can only calculate the Start 02. then we determine whether it modulus of numbers, letʼs add is even or odd. 01. If x is a number then another condition: - what needs to happen if the - If x % 2 == 0 then condition is true - - y = “Even” 03. 02. We use our previous if-else Determine whether a variable x is 03. statement when the condition an even or odd number. - Else is True. - - y = “Odd” - End if Note the indent levels. Set the condition to see if the End if 01. input variable x is a number. End 16 Programmatic thinking Nested if statement | We use nested if statements when we must decide on a combination of conditions before deciding the next action or final output. Example: Pseudocode When the condition is True, Since we can only calculate the Start 02. then we determine whether it modulus of numbers, letʼs add is even or odd. 01. If x is a number then another condition: - what needs to happen if the - If x % 2 == 0 then condition is true - - y = “Even” 03. 02. We use the if-else statement Determine whether a variable x is 03. from the previous example an even or odd number. - Else when condition 1 is True. - - y = “Odd” - End if Set the condition to see if the End if When condition 1 is False, we 01. 04. input variable x is a number. 04. exit the if statement without End doing anything else. 17 Programmatic thinking Nested if statement 01. The flowchart to determine if variable x is a number: Pseudocode Flowchart Start Start If If x isx %a 2number then == 0 then - y = “Even” True - what Elseneeds to happen when the x is a Do condition is true number something - y = “Odd” End if False End End if End 18 Programmatic thinking Nested if statement 02. The flowchart of the nested if statement that checks both if x is a number and whether itʼs even or odd: Pseudocode Flowchart Start Start If If x is x %a 2number then == 0 then - y - If x =% “Even” 2 == 0 then True True - what needs to happen if the x is a x % 2 - -Else y = “Even” y = “Even” condition is true number == 0 - - Else y = “Odd” - -End y =if “Odd” False False - End if End End if y = “Odd” End End 19 Programmatic thinking Nested if statement Example: Pseudocode Determine whether a variable x is Start a number greater than 10 and If x is a number AND x > 10 then even or odd. - If x % 2 == 0 then - - y = “Even and greater than 10” We can solve this by amending the - Else first condition with a boolean - - y = “Odd and greater than 10” operator. - End if Else However, now we can only say - y = “Not a number or smaller than 10” that the variable x is either not a End if number or smaller than 10, i.e. we End donʼt know the specific reason for not giving y as “Evenˮ or “Oddˮ. 20 Programmatic thinking Nested if statement Example: Flowchart Determine whether a variable x is a number greater than 10 and Start even or odd. We can amend the first condition with a boolean operator to x is a True True y = “Even and x % 2 consider the second condition (x > number AND == 0 greater than 10” x > 10 10) in conjunction with the first. False False However, now we can only say that the variable x is either not a y = “Not a y = “Odd and number or smaller than 10, i.e. we number OR greater than End donʼt know the specific reason for smaller than 10” 10” not giving y as “Evenˮ or “Oddˮ. 21 Programmatic thinking Nested if statement Example: Pseudocode Determine whether a variable x is a number greater than 10 and Start even or odd. If x is a number then - what needs to happen when the condition is true We can also nest an additional if statement to test the two conditions separately, i.e. (x is a number) and (x > 10. 01. Is variable x a number? Else - y = “Not a number” End if Note the indent levels. End 22 Programmatic thinking Nested if statement Example: Pseudocode Determine whether a variable x is a number greater than 10 and Start even or odd. If x is a number then - If x > 10 then We can also nest an additional if - - what needs to happen when the condition is true statement to test the two conditions separately, i.e. (x is a number) and (x > 10. 01. Is variable x a number? - Else - - y = “Smaller than 10” 02. Is variable x greater than 10? - End if Else - y = “Not a number” End if Note the indent levels. End 23 Programmatic thinking Nested if statement Example: Pseudocode Determine whether a variable x is a number greater than 10 and Start even or odd. If x is a number then - If x > 10 then We can also nest an additional if - - If x % 2 == 0 then statement to test the two - - - y = “Even and greater than 10” conditions separately, i.e. (x is a - - Else number) and (x > 10. - - - y = “Odd and greater than 10” - - End if 01. Is variable x a number? - Else - - y = “Smaller than or equal to 10” 02. Is variable x greater than 10? - End if Else 03. Is variable x even or odd? - y = “Not a number” End if Note the indent levels. End 24 Programmatic thinking Nested if statement Example: Determine whether a variable x is a number greater than 10 and even or odd. Flowchart Note: The succeeding Start conditions are only considered when the previous True True True x is a x % 2 y = “Even and conditions are x > 10 number == 0 greater than 10” True. False False False y = “Not a y = “Smaller than y = “Odd and number” End or equal to 10” greater than 10” 25 Programmatic thinking If-else-if ladder | We use if-else-if ladders when we need to test multiple conditions and execute different code based on which of the conditions are met. Itʼs a way to chain multiple if statements together. If (condition) then Flowchart - Do something Start... True Condition Do something We expect the if statement to False terminate when the condition is false. End 26 Programmatic thinking If-else-if ladder | We use if-else-if ladders when we need to test multiple conditions and execute different code based on which of the conditions are met. Itʼs a way to chain multiple if statements together. If (condition) then Flowchart - Do something Start Else if (another condition) then True - Do something different Condition Do something... False Now, when the first if statement is True Condition Do something End false, the second condition is considered. False When the second condition is also false, then the if-else-if ladder terminates. 27 Programmatic thinking If-else-if ladder | We use if-else-if ladders when we need to test multiple conditions and execute different code based on which of the conditions are met. Itʼs a way to chain multiple if statements together. If (condition) then Flowchart - Do something Start Else if (another condition) then True - Do something different Condition Do something Else if (another condition) then - Do something different False True If all three conditions are false, Condition Do something End then the if-else-if ladder terminates*. False Here we only have three different True conditions and consequences, but Condition Do something we could include many more. We donʼt always have to show terminations. If neither a True nor False flow arrow is shown, it is assumed that it leads to the end terminator. 28 Programmatic thinking If-else-if ladder If (condition) then Flowchart - Do something Else if (another condition) then Start - Do something different True Else if (another condition) then Condition Do something - Do something different Else - Do something different_ False True Condition Do something End If all three conditions are false, we can also use an else statement to False execute another process before the ladder terminates. True Condition Do something Note: False The succeeding conditions are only considered when the Do something previous conditions are False. 29 Programmatic thinking If-else-if ladder Letʼs see if we can rewrite the Pseudocode example used in the nested if statements by using an if-else-if Start ladder. If x is not a number then - y = “Not a number” Example: Else if x

Use Quizgecko on...
Browser
Browser