23
Selection Statements

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

Embed Size (px)

Citation preview

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

Selection Statements

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

Introduction

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

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

Review – if statements

var number : intget number % Convert a negative number into a

positiveif ( number < 0 ) then number := number * -1end if

Boolean expression

Code to execute if the Boolean expression is true

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

Review – if-else statementsvar number : intget number

if ( number < 0 ) then % Convert the number into a positive number number := number * -1elsif ( 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

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

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.

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

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.

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

`

var number : intget numberif ( number < 0 ) then % Convert to a positive number number := number * -1 put numberelsif ( number = 0 ) then % Exit program if number is 0 quit else % Double the number number := number *2 put numberend if

var number : intget number

if ( number = 0 ) then % Exit the program if number is 0 quitelse % Process number if ( number < 0 ) then % Convert to a positive number number := number * -1 else % Double the number number := number *2 end if put numberend if

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

Nesting

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

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

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

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

Nested Boolean Expression•Nestedif ( 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 end ifend if

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

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.

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

Discussion – What Happens in Each?% Process number

if ( number < 0 ) then % Convert to a positive number number := number * -1elsif ( number > 0 ) then % Double the number number := number *2end 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

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

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.

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

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.

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

DiscussionUse 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.

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

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.

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

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

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

case statementsSyntaxcase <testVariable> of label <constant value>: % Code executed if constant value = testVariable value label <constant value 2>: % Code executed if constant value 2 = testVariable value label : % like an else block, code that executes when all previous cases failedend case

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

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

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

var input : stringput “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

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

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

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

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?

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

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.