Make your own free website on Tripod.com
QBasic - Step By Step :: Section 3
Author: Mike Ware
Website: [warebiz] :: "The Programmer's Domain" - http://warebiz.tripod.com
Email: warebiz@yahoo.com
Copyright © 2002 Michael Shawn Ware, All Rights Reserved.


You are here --> [Section 3 :: Controlling Execution With Control Structures]

"Jump To Articles"
    --> IF Statements
    --> SELECT CASE Statements
    --> Operator Precedence


IF Statements

Decision Structures
Be definition, a control structure allows the programmer to determine whether or not specific statements are executed. Qbasic has two control types: decision structures and loops (loops are covered in later articles). Decision structures are used to make comparisons in order to decide if certain statements and actions are to be executed or taken. One form is the block IF statment which is a single-alternative decision. It either does something or it does nothing at all. The form of the block IF is as follows:


    IF (expression) THEN
        stmtT
    END IF

The stmtT is only executed if (expression) is true; otherwise, execution moves on to the next executable statement. Any number of statements may be included in the body of the IF, which is referred to as the statement block. For example, consider:


    IF age = 21 THEN
        PRINT "Congratulations, "; name$
        PRINT "You can now legally drink all the beer and liquor you want!"
        PRINT "Just remember, be responsible and be sure not to drink and drive!"
    END IF

As you can tell, for readability sake, you should indent the form of the control structure so the program logic can be read clearly and easily. The execution of the block IF statement is controlled by a boolean or logical expression, which is a statement that evaluates to true ( 1 ) or false ( 0 ). The block IF uses a relational operator to compare two expressions, which must be of the same type. In the above code, the two expressions were age and 21 ( = is the relational operator). The ASCII system is used to evaluate string values. In general, the following is where numbers and important letters begin in the ASCII table (for a larger table, seek info on the internet):

    0 -- ASCII 48
    A -- ASCII 65
    a -- ASCII 97

The relational operators which can be used are as follows:

    <     --> is less than
    >     --> is greater than
    <=     --> is less than or equal to
    >=     --> is greater than or equal to
    =     --> is equal to
    <>     --> is not equal to

Another form of a decision structure is the IF ELSE statement which is referred to as a double-alternative decision structure because one action is taken if the expression is true and another action is taken if the expression evaluates to false. The form of the IF ELSE statement is as follows:


    IF expression1 THEN
        stmtT
    ELSE
        stmtF
    END IF

For example, consider:


    IF number >= 100
        PRINT "This is a high value."
    ELSE
        PRINT "This is a low value."
    END IF

If you want to check for one of several conditions, you can add ELSEIF clauses into the body of the IF ELSE statement. This type of decision structure will always perform some action. The form is as follows:


    IF expression1 THEN
        stmtT1
    ELSEIF expression2 THEN
        stmtT2
    ELSEIF expression3 THEN
        stmtT3
    ELSE
        stmtF
    END IF

For example, consider:


    IF score > 90 THEN
        PRINT "Grade = A"
    ELSEIF score > 80 THEN
        PRINT "Grade = B"
    ELSEIF score > 70 THEN
        PRINT "Grade = C"
    ELSEIF score > 60 THEN
        PRINT "Grade = D"
    ELSE
        PRINT "Grade = F"
    END IF

You can also check for several conditions by using nested IF statements, which are IF statements used in the body of IF statements. The form for nested IFs is as follows:


    IF expression1 THEN
        IF expression1A THEN
            stmtT1A
        ELSE
            stmtF1A
        END IF
    ELSE
        IF expression1B THEN
            stmtT1B
        ELSE
            stmtF1B
        END IF
    END IF

SELECT CASE Statement

A SELECT CASE statement is another control structure which allows an action to be selected from a list of alternatives. The SELECT CASE statement uses various "cases", individually named CASE, which include one or more statements to be executed if the specified value of the expression equals the value of the "case". There is also a CASE ELSE clause which is optional but is useful for validating user input. The SELECT CASE statement is particularly efficient when menus are included in a program. A menu is a list of options that is displayed to the user with each option having an action to take place if it is selected. When the user makes a choice, the choice can be evaulated easily with a SELECT CASE statement. A menu is demonstrated in the program further down the page. The form of a SELECT CASE statement is as follows:


    SELECT CASE testExpression
        CASE expression1
            stmt1
        CASE expression2
            stmt2
        CASE expression3
            stmt3
        .
        .
        .
        CASE ELSE
            stmt(N)
    END SELECT

For example, consider the following SELECT CASE statements:


    INPUT "Enter the index number of your high school class year (1 - 4): ", classNum
    SELECT CASE classNum
        CASE 1
            PRINT "You are a freshman."
        CASE 2
            PRINT "You are a sophomore."
        CASE 3
            PRINT "You are a junior."
        CASE 4
            PRINT "You are a senior."
        CASE ELSE
            PRINT "You did not specify an index number from 1 - 4!"
    END SELECT


In the above statement, the expression being tested is classNum. If the value of classNum is equal to 1, then "You are a freshman." will be printed as output. If the value of classNum is equal to 2, then "You are a sophomore." will be printed as output, and so on. SELECT CASE statements can also evaulate character string values. Consider the following:


    INPUT "Enter the name of your favorite music category: ", musicCate$
    SELECT CASE musicCate$
        CASE "Alternative"
            PRINT "So, you must like Dave Matthews Band!"
        CASE "Rock"
            PRINT "So, you must like Creed!"
        CASE "Rap"
            PRINT "So, you must like 2Pac!"
        CASE "Metal"
            PRINT "So, you must like Stone Temple Pilots!"
        CASE ELSE
            PRINT "You did not enter a valid music category."
    END SELECT

Notice that in the above SELECT CASE statement, the individual "cases" are placed within quotes. We have to put each CASE within quotes because we are evaulating musicCate$, which is a character string value. When checking for a range of values while using a SELECT CASE statement, you must use the keyword IS when using a relational operator to make a comparison, and you must use the keyword TO to check for ranges. Consider the following:


    INPUT "Enter the test score for the current student: ", testScore
    SELECT CASE testScore
        CASE IS >= 90
            PRINT "Your grade is an A!"
            grade$ = "A"
        CASE 89 TO 80
            PRINT "Your grade is a B!"
            grade$ = "B"
        CASE 79 TO 70
            PRINT "Your grade is a C!"
            grade$ = "C"
        CASE 69 TO 60
            PRINT "Your grade is a D!"
            grade$ = "D"
        CASE IS <= 59
            PRINT "Your grade is a F!"
            grade$ = "F"
    END SELECT

Operator Precedence

Logical Operators
In addition to the arithmetic ops and relational ops, there is a third group of operators called logical or boolean operators. A logical boolean operator is an op that acts on one or more conditions and produces a value of true ( 1 ) or false ( 0 ). The three logical ops in their order of precedence are as follows:

    NOT
    AND
    OR

If you want to change the precedence of the operators in an expression, you will need to use parentheses so the compiler will know which conditions to evaluate first and so forth. When the logical op AND is used, the expression is true only if both conditions are true; otherwise, the expression is false. When the logical op OR is used, the expression is false only if both conditions are false; otherwise, the expression is true. The logical op NOT negates (or reverses) the value of the expression it precedes; if the expression is true, NOT will make it false; if the expression is false, NOT will make it true. For example, consider the following statements:


    IF testScore > 90 AND stuGPA > 3.5 THEN
        PRINT stuName$; " is an excellent student."
    END IF

    IF pet$ = "German Shepherd" OR pet$ = "Pit Bull" THEN
        PRINT "You have a dog as a pet."
    END IF

    IF NOT(stuName$ = "John Doe Smoe") THEN
        PRINT "You are not the top student in your class."
    END IF

The following is a complete program that demostrates most of the material covered in the tutorial up to this point. Study the code and write one of your own before moving on to the next section:


////////////////////////////////////////////////////////////////////////////////////////

REM This program serves as a simple register with limited functions
REM for a pizza place called Freddie's Pizza Palace
REM
REM PROGRAMMER: Mike Ware
REM DATE LAST UPDATE: 3-14-01

CLS

PRINT TAB(20); "Welcome to Freddie's Pizza Palace"
PRINT
PRINT TAB(23); "1 : Personal Pan Pizza - 6 inches"
PRINT TAB(23); "2 : Small Pizza - 10 inches"
PRINT TAB(23); "3 : Medium Pizza - 14 inches"
PRINT TAB(23); "4 : Large Pizza - 18 inches"
PRINT

INPUT "Enter the number (1 - 4) corresponding to the size of the pizza: ", orderNum
INPUT "Enter the number of toppings: ", numToppings
INPUT "Do you want extra cheese (Y/N): ", cheese$

SELECT CASE orderNum
   CASE 1
      totalCost = (numToppings * .5) + 4
   CASE 2
      totalCost = (numToppings * .6) + 7.5
   CASE 3
      totalCost = (numToppings * .75) + 9.25
   CASE 4
      totalCost = (numToppings * .9) + 12.9
   CASE ELSE
      PRINT "You did not specify a number in the correct range!"
END SELECT

IF cheese$ = "Y" OR cheese$ = "y" THEN
   IF orderNum = 1 OR orderNum = 2 THEN
      totalCost = totalCost + 1
   ELSEIF orderNum = 3 OR orderNum = 4 THEN
      totalCost = totalCost + 2
   END IF
END IF

REM figure tax on order
totalCost = totalCost + totalCost * .06

Format$ = "\                               \ $$##.##"
PRINT
PRINT USING Format$; "The total cost of your pizza is: "; totalCost
PRINT
PRINT "THANK YOU FOR COMING TO Freddie's Pizza Palace!"

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


In the next section, we will begin to talk about how to use modules in your programs. Read on for more...

Move on to next set of topics: Section 4 - Using Modules

Back to Top