Introduction To Programming 2
This
tutorial will assume you have read part 1 and atleast attempted the
challenges I presented at the end. If you have ever dealt with a
programming language before you were probably able to finish the
challenges without too much trouble. However since this is an
introduction to programming I am going to assume you were not able to
complete the challenges or had a very difficult time doing so. You
might be thinking to yourself, if simple programs like these are this
challenging to write then I will definitely never learn to program stuff
like games or any other kind of software that people could actually
use. Believe it or not, EVERY programmer
started off not knowing what a variable is or how to use a function. So
lets break these challenges down and understand how to get them to
work.
The first challenge was to have the user enter a number and
then output every number between 1 and the number the user enters. If
you remember from the last tutorial I introduced the Input$( ) function.
The Input$( ) function looks like this:
Input$ ( "Prompt Text" ) '----- This will write the Prompt Text to the console and then wait for the user to type something in.
So to get a number from the user we could just do this:
Input$ ( "Enter a number: " ) '----- This will ask the user to enter a number and then wait for the user to type something in.
However
there is three problems with this line the way it is. The first
problem is: how will we know what number the user entered later on in
our program? The solution to this problem is we have to store that
number in a variable. If you remember from part 1, a variable is a
symbol such as a letter, word, or group of letters and numbers that we
can use to represent a value. There are two types of variables: numbers
and strings. Number variables hold numbers and can be used anywhere in
your program to do math. String variables can hold any kind of text
including numbers, however, NUMBERS STORED IN STRING VARIABLES CANNOT BE USED TO DO MATH.
Now that we know we need to store the number in a variable we get to
the second problem. The Input$( ) function is a string function so we
can only store the value in a string variable. However, if we are going
to count from 1 to the number the user entered we will need to be able
to do math with the variable. Fortunately RC Basic has functions built
in to convert between numbers and strings. So we can solve this problem
by doing the following.
user_input$ = Input$("Enter a number: ")
user_number = Val(user_input$)
Lets break the code above down by each line.
First we ask the user to enter a number and store the number in a string variable we create called user_input$.
user_input$ = Input$("Enter a number: ")
Next
we use the Val( ) function to convert the text stored in the
user_input$ string variable to its number value and we store this number
in a number variable we create called user_number.
user_number = Val(user_input$)
The
third problem is not really a problem right now but it will be by the
time we finish the program. But for now we have a number from the user
that we can use to do math so lets continue. To be able to count from 1
to the number we need a loop. The simplest way to accomplish this is
with the FOR loop. I covered the FOR loop in the last tutorial but I
will give a brief overview of how the FOR loop works here. Look at the
following
For variable = initial_value To final_value Step increase_amount
...Code To Loop....
Next
The
start of the FOR loop has a variable which is set to the initial value
after "=". Then the keyword TO is entered following the initial value.
The STEP keyword is optional. Without STEP the variable is automatically
increased by 1 each time through the loop. If STEP is used, you must
enter an amount you want to increase the variable by each time through
the loop. For the purpose of our program we could just do the following
for now.
For n = 1 to user_number
Print n
Next
The
code above sets the variable n to 1 and will loop the PRINT command
between the FOR and NEXT lines increasing n by 1 each time through the
loop until n is equal to user_number. So the program we have looks like
this.
user_input$ = Input$("Enter a number: ")
user_number = Val(user_input$)
For n = 1 to user_number
Print n
Next
Run
this program and type a few numbers in. It should do what the
challenge wanted it to do. But remember the third problem I mentioned.
Well it is still in our program. To demonstrate what I mean, run the
program and type in a number less than 1. You should see the program
continuously counting. So what is the problem? Well if you remember
earlier when I was talking about the FOR loop I mentioned that without
STEP the variable will automatically increase by 1. So if user_number
is less than what n is initially set to, n will never pass the number
and will just keep counting too infinity. The solution to this final
problem is to use the STEP keyword to increase the variable n by 1 if
the user enters a number greater than or equal to 1 and decrease n by 1
if the user enters a number less than 1. To do this we will need to use
the IF block. Lets review the IF block again real quick. Look at the
following.
If condition Then --- If the condition is true then the following code will be executed
...code to execute...
ElseIf condition Then -- If the previous conditions were not true and this condition is then the following code is executed
...code to execute...
Else
--- If none of the other conditions were true then the following code
is executed
...code to execute...
End If
To
use the IF block with to determine whether to set STEP to 1 or -1 we
simply need to make one more variable. Lets call this variable
increase. So if user_number is greater than or equal to 1 then we will
set increase to 1 and if user_number is less than one then we will set
increase to -1. Here is how it looks.
increase = 1
If user_number >= 1 Then
increase = 1
Else
increase = -1
End If
In
the above code we create a variable called increase and set it to 1.
If the users number greater than or equal to 1 then increase will remain
1. If the users number is not greater than or equal to 1 then the
increase variable will be set to -1.
Now we just need to add the
STEP keyword to our FOR block to use the increase variable to count up
or down depending on what number the user types in. The new FOR line
will look like this.
For n = 1 To user_number Step increase
So the final program will look like the the program below.
user_input$ = Input$("Enter a number: ")
user_number = Val(user_input$)
increase = 1
If user_number >= 1 Then
increase = 1
Else
increase = -1
End If
For n = 1 To user_number Step increase
Print n
Next
Try the program out with different numbers and see if you get the right output.
The
second challenge was to write a program that continues to ask the User
for there name until they enter "Bob". Then tell the User "Good bye
Bob". This program will actually be much easier as we do not have to
convert the text we get from the Input$( ) function to a number. The
biggest difference between this challenge and the first challenge is
that we will be continuously getting input from the user until the user
enters "Bob". Since we need to keep getting input from the user we will
be getting the user input inside the loop this time. But we will
create the variable we will be storing the users input in before we
start the loop. Since we are not looping a certain number of times like
in the last example we will use the WHILE loop. Our basic setup for
this program is below.
user_name$ = ""
While user_name$ <> "Bob"
Wend
In
the above code we first create a string variable called user_name$ and
store an empty string in it. We will be using this variable to store
input from the user inside the loop but for right now we just want it to
be empty. Next we start a WHILE loop. The WHILE loop will loop while
the variable user_name$ is not equal to "Bob" (the "<>" operator test whether two values are NOT EQUAL).
The WEND keyword is simply telling the computer to jump back to the
start of the loop and test if user_name$ is not equal to "Bob". If they
are not equal it will repeat the code in the loop. So now we just need
to write the code we want to repeat. The only thing we need to repeat
is asking the user for there name and storing there name in the
user_name$ variable. Just like in the last challenge we will use the
Input$( ) function. So our code inside our loop will look like this.
user_name$ = Input$("What is your name? ")
Lastly we want to say "Good Bye Bob" if the user enters "Bob". For this all we need is a simple PRINT statement.
Print "Good Bye Bob"
So the final program will look like this.
user_name$ = ""
While user_name$ <> "Bob"
user_name$ = Input$("What is your name? ")
Wend
Print "Good Bye Bob"
And now for some challenges.
1. Write a program that ask the user for 5 numbers and gives the average of the numbers they enter.
2. Write a simple multiple choice trivia game. Give the user atleast 3 questions and 3 choices per question.
3. Improve the second challenge from the last lesson to not be case sensitive.