Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multi-Selection If-elsif Statement.  In fact, it’s everything in between  Yesterday we learned how to add a control statement that gave us two path.

Similar presentations


Presentation on theme: "Multi-Selection If-elsif Statement.  In fact, it’s everything in between  Yesterday we learned how to add a control statement that gave us two path."— Presentation transcript:

1 Multi-Selection If-elsif Statement

2  In fact, it’s everything in between  Yesterday we learned how to add a control statement that gave us two path choices in our program, if-else  This is great, but rarely in life or programs is there only two choices to choose between.  Introducing multi-selection, allowing infinite choices  The if-elsif statement is exactly the same as an if-else statement except it allows us to add more choices in between the if and the else. Life is not Black and White

3 if ( ) then % Code to execute if the in brackets is true. elsif ( ) then % Code to execute if the in brackets is true AND ALL % expressions above it is false elsif ( ) then % Code to execute if the in brackets is true AND ALL % expressions above it are false else % Code to execute if all ’s above are false. end if The if-elsif Statement

4 var denominator : int get denominator if ( denominator = 0 ) then % if the number is 0 put “The number is not divisible.” elsif (denominator > 0 ) then % if the number is positive and divisible put “The number is divisible and positive.” else % if the number is negative and divisible put “The number is divisible and negative.” end if Example

5  Only the FIRST block that evaluates to true will be executed, all others, even if true, will be skipped  Order of test expressions is important since only the first true expression is entered  Each if-elsif statement only has ONE if  The else statement is optional  The number of elsif’s can be from 0 to infinite Special Notes

6  Create a program that asks and stores the user for their current mark.  The program should then use that mark and output a specific message stating what level they are at in a complete sentence. Below is a chart with the message that corresponds with the grades. For this question you will have to use a compound if statement, that means using either AND or OR. Practice MarkOutput Text Between 80 and 100Level 4 Between 70 and 79Level 3 Between 60 and 69Level 2 Between 50 and 59Level 1 Between 1 and 49Level R 0Level I

7  Question 2: The Guessing Game Part 2  Recreate the Guessing game, but this time your program will be between 1 and 100 and do more than just tell them to guess higher or lower  It must now calculate how far away the user’s guess is from the random number and display the appropriate statement depending on the distance away.  Right on: “You win!”  Within 2: “Red hot”  Within 5: “Steaming hot”  Within 10: “Warm”  Within 20: “Cool”  More than 20: “Ice cold”  HINT: You will need to use the mathematical concept of an absolute value, what this is, is that it takes any number positive or negative and gives you the positive version as a result.  This is useful when calculating the difference between two numbers and you do not know which number is larger.  To use absolute value in Turing we have a new function to use, abs(value : int)  E.g. var myValue : int := abs(-5.4) Practice


Download ppt "Multi-Selection If-elsif Statement.  In fact, it’s everything in between  Yesterday we learned how to add a control statement that gave us two path."

Similar presentations


Ads by Google