Download presentation
Presentation is loading. Please wait.
1
CP1020 Week 5 Selection Continued
2
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else zWe can use if then else statements to make selection from a list of possible cases, as seen from the following programme
3
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else programme DIM iMark AS INTEGER INPUT “Please enter your exam mark (0 - 100)”; iMark If iMark >= 0 AND iMark < 40 THEN PRINT “You need to resit your exam again” ELSEIF iMark >= 40 AND iMark < 60 THEN PRINT “You have passed your exam” ELSEIF iMark >= 60 AND iMark <75 THEN PRINT “You have passed with a merit” ELSEIF iMark >= 75 AND iMark <= 100 THEN PRINT “You have passed with Distinction” ELSE PRINT “Your value lies outside the range” ENDIF
4
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Alternative - Select Case The syntax is as follows: SELECT CASE Variablename CASE value1 statements CASE value2 statements CASE ELSE statement END SELECT Do this If none of the cases are selected then do this Upon the first case being satisfied then program moves to END SELECT
5
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Select case programme REM A menu selection programme REM Ian Coulson REM 30/10/97 DIM sMenuChoice AS STRING INPUT “Choose menu a,b or c > ”, sMenuChoice SELECT CASE sMenuChoice CASE “a” PRINT “menu a chosen” CASE “b”, “c” PRINT “menu b or c chosen” CASE ELSE PRINT “invalid input!” END SELECT Suppose user enters “b”
6
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Select case continued INPUT “Please enter your exam mark (0 - 100)”; iMark SELECT CASE iMark CASE 0 TO 40 PRINT “You need to resit your exam” CASE 40 TO 60 PRINT “ you have passed your exam” CASE 60 TO 75 PRINT “You have passed with merit” CASE 75 TO 100 PRINT “You have passed with distinction” CASE ELSE PRINT “Your mark must be in the range 0 to 100 END SELECT
7
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Indenting control structures We can make it easier to follow the flow of a programme when reading it by using a formatting convention. The statements are indented after a control statement:- BEFORE IF iMark >= 0 AND iMark < 20 THEN PRINT “You need to resit your exam again” ELSEIF iMark >= 40 AND iMark < 60 THEN PRINT “You have passed your exam” ELSEIF iMark >= 60 AND iMark <75 THEN PRINT “You have passed with a merit”
8
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Indentation AFTER: Would be written as: If iMark >= 0 AND iMark < 20 THEN PRINT “You need to resit your exam again” ELSEIF iMark >= 40 AND iMark < 60 THEN PRINT “You have passed your exam” ELSEIF iMark >= 60 AND iMark <75 THEN PRINT “You have passed with a merit” END IF This is much easier to follow
9
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson Questions 1. Write a Case Select statement to Print the month name given the month number. i.e. 7 - July 2. Write a Case Select statement to Print the words Spring, Summer, Autumn or Winter given the Month name. i.e July - Summer Return to view another lecture
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.