Advanced Decision Statements And Loops
Note, this is part 4 of the programming guide. If you have not read the previous parts, you will not understand this one. laptop lcd replacement
This article is going to deal with more advanced decisions as well as loops, a loop is when the program is going to keep running the same code over and over again until it finally receives the key to stop and continue on to the rest of the code.
The program we are going to be writing is for a high school; we will continue working on this project for the next few articles, so save your work when your done. This article will simply ask the user what grade they are in and decide if they entered a correct number.
So first off fire up QBASIC and start by creating the variables that we are going to use.
We are going to need a variable to store what grade the user entered that they are in as well as a temporary variable for our first loop.
DIM GRADE as STRING
DIM TEMP as STRING
Now we make the TEMP variable store the number "0".
TEMP = "0"
After we have all of our Variables created we are going to begin this program with a LOOP statement.
DO WHILE TEMP = "0"
This will make it so the same code is run over and over until the TEMP variable is made into a "1".
Now add an INPUT statement that asks what grade the user is in.
INPUT "What grade are you in? (9-12)"; GRADE
Then we are going to have to check if the user even entered a number, many times users can enter things accidentally this following decision will decide if the user accidentally entered a letter or the correct numbers. This is where our first SELECT CASE statement comes in, SELECT CASE is just like an IF THEN ELSE statement except it has multiple choices. Now when a correct answer is given we will change the TEMP variable to 1 so the loop will end, otherwise the loop will keep going until a correct answer is given.
SELECT CASE GRADE
CASE IS = "9"
PRINT "You are a freshman."
TEMP = "1"
CASE IS = "10"
PRINT "You are a sophomore."
TEMP = "1"
CASE IS = "11"
PRINT "You are a junior."
TEMP = "1"
CASE IS = "12"
PRINT "You are a senior."
TEMP = "1"
CASE ELSE
PRINT "You did not enter a number between 9 - 12!, Please enter again."
END SELECT
Now we simply have to end the loop with a LOOP statement.
LOOP
Now the program will end until the next article so lets end with.
PRINT "You entered a correct grade!"
The next article will continue to build onto this program so dont forget to save your work.
Need more help? Post in the forums
Back to the top
Back
