Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Statements.

Similar presentations


Presentation on theme: "Selection Statements."— Presentation transcript:

1 Selection Statements

2 Boolean Expressions Are statements that evaluate to either a true or false value; Are useful in computing and programming because computers think in terms of 1’s and 0’s, which can represent true and false.

3 Boolean Expressions Can be a simple expression if it is a single statement involving a relation, equality, or not statement; var isTeacherAGamer : boolean := true var isWeatherNice : boolean := isSkyBlue var isLazy : boolean := not(isHardWorker) Can be a compound expression if there are several simple expressions connected using the and and or operators willBallBounce := isBallRubber or isGroundRubber

4 Our Programs So Far... procedure GetSomeData() var input : string put “Please enter the data” get input put “You inputted the data: “ + input end GetSomeData Variable value and output change with the user input

5 Our Programs So Far... Our programs so far cannot make decisions
E.g., the user can’t decide to just exit instead of printing their input Our programs follow the typical Top-to-bottom flow, we need to give the programmer control

6 Selection Statements We usually want our programs to run different lines of code depending on different variable values. This is where we use selection statements. Selection statements are used to control the flow of the program, so are also called control structures.

7 Comparison Operators Before we can use this control structure we need to understand how they give us control. They do so by comparing values. The following is a list of all comparisons in Turing Operator Meaning Example Evaluates to = Equal to teacherName = “Mr.Jackson” False Greater than 6 > 3 True Less than 5 < 11 >= Greater than or equal to 23 >= 23 <= Less than or equal to 4 <= 21 not= NOT equal to 3 not= 3

8 If-statements (the detour)
Syntax if (<test-expression>) then %Insert code between the if and end if lines %This code is executed if and only if the Boolean %expression in the brackets evaluates to true. end if

9 What is a test-expression
A test-expression is any expression that evaluates to true or false, usually a comparison of values E.g. %In this example, if the user entered a value of 15 or less the put %command would be executed, otherwise it would be skipped completely if (userAge < 16) then put “Sorry, no license for you” end if

10 Examples var understood : boolean put “Is the topic understood?” get understood if ( understood = false) then %User did not understand put "We need more time" end if

11 if-else statements (The fork in the road)
Syntax if (<test-expression>) then % Code between the if and else lines that is to be executed if the Boolean % expression in the brackets is true else % Code inside the else and end if lines that is to be executed if the Boolean % expression in the brackets is false end if Only ONE of the branches (code blocks) will run depending on whether the test-expression evaluates to true or false.


Download ppt "Selection Statements."

Similar presentations


Ads by Google