Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.

Similar presentations


Presentation on theme: "Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements."— Presentation transcript:

1 Selection Statements

2 Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements

3 Review – if statements var number : int get number % Convert a negative number into a positive if ( number < 0 ) then number := number * -1 end if Boolean expression Code to execute if the Boolean expression is true

4 Review – if-else statements var number : int get number if ( number < 0 ) then % Convert the number into a positive number number := number * -1 elsif ( number = 0 ) then % Exit the program quit end if Boolean expression Code to execute if the Boolean expression is true Code to execute if the Boolean expression is false

5 Nesting Nesting means putting one conditional statement inside of another statement. We can use nesting to make our code shorter and easier to read by ▫reducing the number of conditions checked, or ▫removing repeated lines of code.

6 Nesting There is no limit to the amount of nesting you can do, although after 4 or more nesting levels it may become difficult to understand the code. Occasionally it is better to do nesting in order to group certain computations together. In the next example, there is the case where we want to exit the program, and the other case is where we actually do processing.

7 ` var number : int get number if ( number < 0 ) then % Convert to a positive number number := number * -1 put number elsif ( number = 0 ) then % Exit program if number is 0 quit else % Double the number number := number *2 put number end if var number : int get number if ( number = 0 ) then % Exit the program if number is 0 quit else % Process number if ( number < 0 ) then % Convert to a positive number number := number * -1 else % Double the number number := number *2 end if put number end if

8 Nesting Nesting can also be used to make code easier to read by splitting up the logical AND into several simpler Boolean expressions:

9 Unnested Boolean expression Original if ( age > 14 and grade > 9 and isInterested = true and isNiceOutside = false and isFriday = false) then % Student is old enough to understand, % interested, and not distracted put "You might be listening" end if

10 Nested Boolean Expression Nested if ( age > 14 and grade > 9 ) then % Student is old enough to understand if ( isInterested = true) then % Student is interested if ( isNiceOutside = false and isFriday = false) then % Student is not distracted put "You might be listening" end if

11 Question Why is nesting conditional statements inside of other conditional statements like using logical AND? Each nested conditional statement is entered only if the previous level of nesting is true. In order for the inner statements to be executed, every level of nesting needs to be true. This is like using AND to put them all together.

12 Discussion – What Happens in Each? % Process number if ( number < 0 ) then % Convert to a positive number number := number * -1 elsif ( number > 0 ) then % Double the number number := number *2 end if % Process number if ( number < 0 ) then % Convert to a positive number number := number * -1 end if if ( number > 0 ) then % Double the number number := number *2 end if

13 What Happens in Each In the left code segment, a negative number is converted into a positive number only. A number that starts off as positive is the only number that will get doubled. In the right code segment, both negative and positive numbers will get doubled.

14 Discussion When do you use separate conditional statements and when do you use if-elsif structures Use separate if statements only when you want every condition to be checked every time.

15 Discussion Use elsif statements for: 1) Mutually exclusive Boolean expressions. This means that if only one of the Boolean expressions can be true at one time. For example, a number cannot be both positive and negative at the same time. In this case, only one branch can be true anyway, so there is no point in using several if statements.

16 Discussion 2)Code that has differing levels of importance. Remember that the top-most Boolean expressions get evaluated first and the ones below it are skipped if any expression evaluates to true. Therefore, you can make certain some code gets executed with higher priority than those below it.

17 case statements These are less often used than conditional statements because they are less flexible. Instead of using Boolean expressions, you compare the switch against various cases. You can only use the data types int, string, and boolean in case statements. Can have many different cases

18 case statements Syntax case of label : % Code executed if constant value = testVariable value label : % Code executed if constant value 2 = testVariable value label : % like an else block, code that executes when all previous cases failed end case

19 case statements You can only compare equality against variables for case statements, and the data types that can be used are limited, that means no, etc. ▫Less flexible than if statements

20 var input : string put “Please enter a command” get input case input of label “x”: quit label “c”: put “ You have chosen to continue” label “d”: put“ You have chosen to self-destruct. Good-bye” label : % used like an else block put “Unknown selection was made” end case

21 if-elsif-else vs. Case Why would we use Case instead of if-elsif ▫Don’t they do the same thing? Well, yes and no... ▫They end up with the same results, however they get there at different speeds ▫And they look entirely different  Ex. Demo Code on Moodle

22 Choices as Programmers Programmers are faced with choices everyday as there is never only one way of doing something. ▫In all cases there is only two reasons every programmer considers when making this decision:  Readability  How clean and easy is code to read and understand at a glance  Efficiency (Memory Space and Time)  How fast and little memory does a certain technique take?

23 Case Both Case and if-elsif statements will only go into one of the possible cases. ▫The difference is that for a Case statement it does not check each case, it automatically goes to the correct one (Efficiency) ▫if-elsif statements must look at each one until the first correct one is found.


Download ppt "Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements."

Similar presentations


Ads by Google