QBasic Tutorial 8 - Select Case Statements - QB64


Description:


Select-Case statements work like If statements. The difference is that the Select-Case statement can make the code simpler to read and work with than If statements. In some computer languages and computer systems, the Select-Case statements run faster than the If statements.  


Creating a Select-Case statement is simple to do. The next program will prompt the user to select the key A-D and the program will respond by telling the user what key was entered. We will create a Select-Case statement for the A-D keys entered.



DIM KeyPressed AS STRING


CLS


PRINT

PRINT


INPUT "Please Enter A Key (A,B,C,D): ", KeyPressed


KeyPressed = UCASE$(KeyPressed)


PRINT


SELECT CASE KeyPressed

  CASE "A"

     PRINT "A Was Entered"

  CASE "B"

     PRINT "B Was Entered"

  CASE "C"

     PRINT "C Was Entered"

  CASE "D"

     PRINT "D Was Entered"

  CASE ELSE

     PRINT "Some Other Key Was Entered"

END SELECT




Code Download:  QBT8_1.BAS


The next program asks for a score then prints the grade assigned to the score.


DIM Score AS INTEGER


CLS


PRINT

PRINT

INPUT "Enter The Test Score: ", Score


PRINT


SELECT CASE Score

   CASE IS >= 97

     PRINT "Grade A+"

   CASE 93 TO 96

     PRINT "Grade A"

   CASE 90 TO 92

     PRINT "Grade A-"

   CASE 87 TO 89

     PRINT "Grade B+"

   CASE 83 TO 86

     PRINT "Grade B"

   CASE 80 TO 82

     PRINT "Grade B-"

   CASE 77 TO 79

     PRINT "Grade C+"

   CASE 73 TO 76

     PRINT "Grade C"

   CASE 70 TO 72

     PRINT "Grade C-"

   CASE 67 TO 69

     PRINT "Grade D+"

   CASE 63 TO 66

     PRINT "Grade D"

   CASE 60 TO 62

     PRINT "Grade D-"

   CASE ELSE

     PRINT "Fail"

END SELECT



Code Download: QBT8_2.BAS





Video Tutorial: QBasic Tutorial 8 - Select Case Statements - QB64

Previous Tutorial                 Next Tutorial