Files
RCBASIC4/doc/doc_files/hello_world.html
2025-01-25 23:53:43 -05:00

576 lines
43 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>RCBasic First Program [RCBasic Doc] </title>
</head>
<body>
<p><h2>YOUR FIRST PROGRAM </h2></p>
<p>
This tutorial is designed to introduce complete newbies to the world of computer programming and specifically programming using RC Basic. This tutorial will not assume any previous programming knowledge and really won't even assume you know much about how a computer works. A basic understanding of 6th Grade Algebra will come in handy but is not necessary. Now that I got that out of the way, lets take our first steps in our journey to becoming computer programmers.
</p>
<p>
First lets go over the requirements you will need to follow this tutorial. You will need a computer (obviously), RC Basic (yet again obvious, but I had to say it), and the will to learn (not essential but it will definitely help). Ok lets begin.
</p>
<p>
<h2><b>What Is A Computer Program?</b></h2> It seems like a stupid question but it is important to understand what a program is before we go any further. A computer program is simply a list of detailed instructions telling a computer how to perform a task. And when I say detailed I mean DETAILED. A computer can perform several complex equations a second but still does not know more than you do. If you don't know any complex equations neither does the computer. To give the computer our list of instructions on what we want it to do we use a computer language. There are probably as many computer languages as there are spoken languages and maybe even more. A computer programmer chooses a language based on the task they need to perform, how complex the task is, the time it takes to write the program, and personal choice. For this tutorial we will be using RC Basic. RC Basic is based on the BASIC programming language, which was designed to be easy to learn and use. RC Basic adds the advantage of modern multimedia features and the ability to write our program once and run it on multiple operating systems.
</p>
<p>
<h2><b>Our First Program</b></h2> Lets start off with a simple computer program. We are going to make a program that writes the words "Hello World" to the screen. To tell the computer to write some text to the screen we will use the Print command. Type the following into the RC Basic Editor and click the Run button (the triangle button or you could go into the build menu and click Run).
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Hello World"</span>&nbsp;<br>
</code></p>
<p>
You should see "Hello World" in your console window. Congratulations, you just wrote your first computer program. So lets break this program down into its individual parts. First is the Print command. This is telling the computer to write something to the console (I will be referring to the DOS screen as the console for the rest of this tutorial). And then we have "Hello World". First of all, why is it in quotation marks? The short answer is because it is not a number. We will go over this more in depth later but for now that is all we need to know for right now? Before we move on, use the Print command to write some other stuff to the screen.
</p>
<p>
<h2><b>Data Types</b></h2> We are finally going to learn why "Hello World" has quotations around it. So lets talk about data types. A data type is simply the type of data you are working with. RC Basic has 2 data types: Numbers and Strings. Numbers are exactly what you think they are. So what are strings? Well to answer this we have to understand what a character is. A character is a number which is generally represented by some kind of symbol. I know that doesn't make since right now so just think of a character as any key on your keyboard. The A Key, B Key, and even the SPACE Key are only numbers to the computer. When the computer reads a character it is actually reading a number and then it outputs a symbol (like letters, digits, parenthesis, space, etc.). So what then is a string? Well a string is a group of characters strung together (which is why it is called a string). RC Basic uses quotation marks to group characters together into a string. So in the string "Hello World", H is a character, e is a character, and so on. If you want to use a single character you would still use quotation marks but you would only have one character between the quotation marks.
</p>
<p>
Look at the following examples.
</p>
<p id="rc_code"><code>
<span class="rc_string">"42"</span>&nbsp;<br>
<span class="rc_number">42</span>&nbsp;<br>
</code></p>
<p>
The first line is a string and the second line is a number. Are you confused yet? The first line has the character "4" and the character "2" in a string and the second line is the number 42. Yes they look the same but the way you perform operations on a string is different than the way you perform operations on numbers. It will make more since with the examples below.
</p>
<p>
This will add the string "53" to the end of the string "42" and output 4253 to the console. Try it.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"42"</span>&nbsp;+&nbsp;<span class="rc_string">"53"</span>&nbsp;<br>
</code></p>
<p>
This will will add the number 42 to the number 53 and output 95 to the console. Try it.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_number">42</span>&nbsp;+&nbsp;<span class="rc_number">53</span>&nbsp;<br>
</code></p>
<p>
The differences between numbers and strings should be starting to clear up a little by now. If you are still a little confused at this point don't worry. This is only an Introduction to programming. If you are able to write some text on the screen and do some simple math by the end of this tutorial you will be ready to continue into the next lesson.
</p>
<p>
Now we are going to get into operators. I know I said at the beginning of the tutorial I did not expect you to know much but I am assuming you know how to add, subtract, multiply, and divide. We got a brief introduction to the "+" operator in the last example. It is important to note that the "+" operator is the only operator that works on both numbers and strings.
</p>
<p>
Here is a list of the main string operators. "+": Adds one string to the end of another. This example will output "batman" to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"bat"</span>&nbsp;+&nbsp;<span class="rc_string">"man"</span>&nbsp;<br>
</code></p>
<p>
Before we continue, lets break down the <b>PRINT</b> statement. It is the simplest way to get text on the screen. <b>PRINT</b> also allows us to output multiple items of different types by separating them with a “;”.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;“Text”;&nbsp;<span class="rc_number">5</span>+<span class="rc_number">4</span>;&nbsp;&nbsp;More&nbsp;Text”;&nbsp;<br>
&nbsp;&nbsp;<br>
Ending&nbsp;<span class="rc_keyword">Print</span>&nbsp;with&nbsp;a&nbsp;“;”&nbsp;keeps&nbsp;the&nbsp;cursor&nbsp;on&nbsp;the&nbsp;same&nbsp;line&nbsp;in&nbsp;the&nbsp;console.&nbsp;<br>
</code></p>
<p>
Here is a list of the main number operators. "+": Adds two numbers together. This example will output 8 to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_number">6</span>&nbsp;+&nbsp;<span class="rc_number">2</span>&nbsp;<br>
</code></p>
<p>
"-": Subtracts one number from another. This example will output 4 to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_number">6</span>&nbsp;-&nbsp;<span class="rc_number">2</span>&nbsp;<br>
</code></p>
<p>
"*": Multiplies two numbers (Note: Multiplication uses an * instead of an x). This example will output 12 to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_number">6</span>&nbsp;*&nbsp;<span class="rc_number">2</span>&nbsp;<br>
</code></p>
<p>
"/": Divides one number by another. This example will output 3 to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_number">6</span>&nbsp;/&nbsp;<span class="rc_number">2</span>&nbsp;<br>
</code></p>
<p>
"^": Raises a number to a power. This example will output 36 (6 to the second power) to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_number">6</span>&nbsp;^&nbsp;<span class="rc_number">2</span>&nbsp;<br>
</code></p>
<p>
Both string operators perform the same operation. They will place the characters in the second string at the end of the first string. The number operators perform the same operations they do on a calculator. One more important thing to mention is that the number operators follow the Order of Operations. Take a look at the example below.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_number">4</span>+<span class="rc_number">3</span>*<span class="rc_number">2</span>&nbsp;<br>
</code></p>
<p>
In the example above the, following the order of operations PEMDAS (Parenthesis, Exponents, Multiplication, Division, Addition, and Subtraction); 3 is multiplied by 2 which is 6, and then 4 is added to it which is 10. So math is not necessarily done from left to right but it is determined by the order of operations. There is more to the order of operations but we are not going to cover that right now. We covered all of the main operators so before we move on lets look at one more example that uses parenthesis.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<b>(</b><span class="rc_number">4</span>+<span class="rc_number">3</span><b>)</b>*<span class="rc_number">2</span>&nbsp;<br>
</code></p>
<p>
This is almost the same problem as the example before it, but now it groups 4 plus 3 together using parenthesis which means 4 and 3 are added first which will be 7. Then that 7 is multiplied by 2 which is 14. If you do the same problems on a calculator you will get the same answers. Try out a few math problems on your own.
</p>
<p>
Interacting With The User Up to this point, we have been writing output to the screen. We have been the user as well as the programmer so there was no reason to need to accept input from the screen. But when you write programs that are meant to be used by people other than yourself you will want to get input from the user and process that input to perform some task. So lets dive into some code. For this example we are using the function Input to ask the user "What is your name? " and read what the user types into the console (which should be there name but they can type whatever they want).
</p>
<p id="rc_code"><code>
Input$<b>(</b><span class="rc_string">"What is your name? "</span><b>)</b>&nbsp;<br>
</code></p>
<p>
You should have seen "What is your name?" and then been able to type something in. You have just received input from the user for the first time. So lets break this line down. First we have the function name Input$. Input$ is a built-in function that prompts the user with something and it returns what the user typed. This is the first time I used the word returns so let me clarify. In the example above lets say you type in Bob for your name. The whole Input$() function call is replaced with "Bob". Take a look at this example.
</p>
<p>
This line does not prompt anything and just waits for you to type something. We are going to type Bob.
</p>
<p id="rc_code"><code>
Input$<b>(</b><span class="rc_string">""</span><b>)</b>&nbsp;<br>
</code></p>
<p>
After you enter "Bob" the line above will be replaced with "Bob" and the line will finish processing your commands.
</p>
<p id="rc_code"><code>
<span class="rc_string">"Bob"</span>&nbsp;<br>
</code></p>
<p>
You are probably thinking that is useless. What can we do with a line that just has a string on it. And the answer is you really can't do anything with a line that just has Bob on it. But if we store the string "Bob" we can use it to do other stuff with.
</p>
<p>
To store "Bob" (or whatever you call yourself) we use something called a variable. You might have seen variables used in math class in school. A variable is just a symbol that holds data. By symbol I don't mean a logo or anything. Just take a look at this example.
</p>
<p id="rc_code"><code>
a&nbsp;=&nbsp;<span class="rc_number">2</span>&nbsp;<br>
b$&nbsp;=&nbsp;<span class="rc_string">"Bob"</span>&nbsp;<br>
batman&nbsp;=&nbsp;<span class="rc_number">4.5</span>&nbsp;<br>
superman$&nbsp;=&nbsp;<span class="rc_string">"this is just random text"</span>&nbsp;<br>
</code></p>
<p>
This example makes four variables: a, b$, batman, superman$ and stores some data in them. Lets do a break down.
</p>
<p>
We will start with the variables a and batman. The variables are just storing numbers. We can use variables that store numbers when we have programs that are going to be handling values that change over time. For now lets just do some simple math. Type the variables from the previous example and add the following code and run it.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;a&nbsp;+&nbsp;batman&nbsp;<br>
</code></p>
<p>
This example just adds the values in the variables together and outputs the values to the screen. You can change the value of a variable at any time. This is better explained with another example.
</p>
<p id="rc_code"><code>
a&nbsp;=&nbsp;<span class="rc_number">5</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;a&nbsp;<br>
a&nbsp;=&nbsp;<span class="rc_number">6</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;a&nbsp;<br>
a&nbsp;=&nbsp;<span class="rc_number">5</span>&nbsp;+&nbsp;<span class="rc_number">6</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;a&nbsp;<br>
</code></p>
<p>
What we are doing in the example above is changing the value of a and doing math and storing the value in a. We can also store the value of one variable inside another. Lets Look at another example.
</p>
<p id="rc_code"><code>
a&nbsp;=&nbsp;batman&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;a&nbsp;<br>
a&nbsp;=&nbsp;a&nbsp;+&nbsp;batman&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;a&nbsp;<br>
</code></p>
<p>
The example above will first store the value of batman in the variable a. It will write a to the screen. Then it will add a and batman and store the result in a and write that value to the screen.
</p>
<p>
Now we are going to take a look at the other two variables we created: the variable b$ and the variable superman$. Did you notice that both of the variables end in a $. There is a reason for that. The $ at the end of the variable name lets the compiler know that these variables are strings. Since these two variables are strings the rules about strings apply to these variables. Look at the following examples.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;b$&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;superman$&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;b$&nbsp;+&nbsp;superman$&nbsp;<br>
</code></p>
<p>
The above example writes the value stored in b$ to the screen. Then it writes the value stored in superman$ to the screen. Finally it adds the contents of the superman$ variable to the end of the contents for the b$ variable and writes that to the screen.
</p>
<p>
Now that we have covered variables, how do we use them with the users input. Well lets look at the Input$() function again. Notice that the function ends in a $. input$() returns a string so it can be used anywhere a string can. Look at this example.
</p>
<p>
This line does the same thing as the last time we used the Input$() function. The main Difference is now we are storing the value the user enters into the variable name.
</p>
<p id="rc_code"><code>
name$&nbsp;=&nbsp;Input$<b>(</b><span class="rc_string">"What is your name?"</span><b>)</b>&nbsp;<br>
</code></p>
<p>
This code will store "Bob" or what ever you typed in into the variable name$. Now we can use the name$ as many times as we want anywhere in our program. So now lets make a program that Uses Input$() along with Print. We are going to ask the User for there name and then welcome the User to RC Basic.
</p>
<p id="rc_code"><code>
name$&nbsp;=&nbsp;Input$<b>(</b><span class="rc_string">"What is your name? "</span><b>)</b>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Hello "</span>&nbsp;+&nbsp;name$&nbsp;+&nbsp;<span class="rc_string">". Welcome to RC Basic."</span>&nbsp;<br>
</code></p>
<p>
Lets do our break down again. In the first$ line we create a variable called name$ and call Input$() in it. This will write "What is your name? " to the screen. When the User Enters something, whatever the User types in will replace Input$(). So name$ will be whatever the User types in. On the next line we use Print to write a message to the screen. The only thing different this time is we are adding a variable to our string in Input$(). But remember that our variable is a string and can do anything a string can, so all you are doing is saying "Hello " + the User's name + ". Welcome to RC Basic.".
</p>
<p>
We have covered a lot in these first few sections so I am going to take this time to challenge you. Write a program that ask the User to type there favorite color in and then tell them what there favorite color is on the next line.
</p>
<p>
Flow Control Flow control sounds like some really complicated topic. But it will probably make sense almost immediately. So what is flow control? Simply put, flow control is controlling what your program does based on what conditions are met. To control the flow in our programs we will use the If statement. The If statement will allow us to execute a block of code depending on what conditions are met. Let look at the last example from the last segment.
</p>
<p id="rc_code"><code>
name$&nbsp;=&nbsp;Input$<b>(</b><span class="rc_string">"What is your name? "</span><b>)</b>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Hello "</span>&nbsp;+&nbsp;name$&nbsp;+&nbsp;<span class="rc_string">". Welcome to RC Basic."</span>&nbsp;<br>
</code></p>
<p>
To give an idea of how to use the <b>IF</b> statement we are going to extend this program. So lets make the program give a special greeting if Bob is typed in for the name. Look at the following example.
</p>
<p id="rc_code"><code>
name$&nbsp;=&nbsp;Input$<b>(</b><span class="rc_string">"What is your name? "</span><b>)</b>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Hello "</span>&nbsp;+&nbsp;name$&nbsp;+&nbsp;<span class="rc_string">". Welcome to RC Basic."</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">If</span>&nbsp;name$&nbsp;=&nbsp;<span class="rc_string">"Bob"</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"We have been expecting you"</span>&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
This program will do the same thing it did in the last segment but it adds the use of the <b>IF</b> statement. So lets do a quick break down.
</p>
<p>
This line is saying if name$ has "Bob" stored in it, then run the following code. Each <b>IF</b> statement must end with the <b>THEN</b> statement.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">If</span>&nbsp;name$&nbsp;=&nbsp;<span class="rc_string">"Bob"</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
</code></p>
<p>
This line is writing "We have been expecting you" to the screen.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"We have been expecting you"</span>&nbsp;<br>
</code></p>
<p>
This line is ending the block of code that the <b>IF</b> statement wanted the program to execute and continues the rest of your program.
</p>
<p>
The block of code started by the <b>IF</b> statement can also be given different conditions to check for and execute a different block of code for each different condition. We are going to take a break from strings and use numbers for our next few examples. We are also going to introduce a few new operators. So lets start by making some number variables.
</p>
<p id="rc_code"><code>
a&nbsp;=&nbsp;<span class="rc_number">1</span>&nbsp;<br>
b&nbsp;=&nbsp;<span class="rc_number">3</span>&nbsp;<br>
c&nbsp;=&nbsp;<span class="rc_number">5</span>&nbsp;<br>
</code></p>
<p>
Now that we have some variables we are going to make a program that ask the User to enter a number and check if the number is equal to each variable and outputs something different for each one. First I need to cover one very important topic. The Input$() function only takes in a string. So even number that are typed in by the User are treated as strings. So we need to convert the Users input to a number to be able to compare it to another number or do any kind of math with it. To do this we will use the Val() function. The Val() function converts a string to a number. Look at the following example real quick.
</p>
<p>
This example will convert the string "12" to the number 12.
</p>
<p id="rc_code"><code>
x&nbsp;=&nbsp;Val<b>(</b><span class="rc_string">"12"</span><b>)</b>&nbsp;<br>
</code></p>
<p>
The more programs you write, the more you will get use to converting numbers to strings and strings to numbers. So how do we use <b>Val()</b> with <b>Input$()</b>. Well remember that Input$() can be used anywhere a string can. So instead of putting "12" in Val() like we did in the last example we will simply put Input$() inside Val(). Make sure your program has the variables a, b, and c we created earlier and add this.
</p>
<p id="rc_code"><code>
d&nbsp;=&nbsp;Val<b>(</b>&nbsp;Input$<b>(</b><span class="rc_string">"Enter a number: "</span><b>)</b>&nbsp;<b>)</b>&nbsp;<br>
</code></p>
<p>
The code above is creating a variable called d. Then it uses the <b>Val()</b> function to convert the string returned by <b>Input$()</b> into a number. The string returned by Input$() is what ever the User types in. Now that we have gotten a number from the User lets compare that number to the variables we made earlier.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">If</span>&nbsp;d&nbsp;=&nbsp;a&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"You typed the A value"</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">ElseIf</span>&nbsp;d&nbsp;=&nbsp;b&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"That is B"</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">ElseIf</span>&nbsp;d&nbsp;=&nbsp;c&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"That would be C"</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">Else</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"You did not type any of the numbers"</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
Lets do a break down. This is starting a multi-layered If statement block. It should be fairly simple to understand. We are going to go through each line and examine what is happening step by step.
</p>
<p>
This line is starting your If block by comparing d(which is what the User typed in) to the variable a. If the two variables are equal then it will output "You typed the A value" to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">If</span>&nbsp;d&nbsp;=&nbsp;a&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"You typed the A value"</span>&nbsp;<br>
</code></p>
<p>
This line will compare d and b if d wasn't equal to a. If the value stored in d is equal to the value stored in b then it will output "That is B" to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">ElseIf</span>&nbsp;d&nbsp;=&nbsp;b&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"That is B"</span>&nbsp;<br>
</code></p>
<p>
This line will compare d and c if d wasn't equal to a or b. If d is equal to c then "That would be C" is output to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">ElseIf</span>&nbsp;d&nbsp;=&nbsp;c&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"That would be C"</span>&nbsp;<br>
</code></p>
<p>
This line will output "You did not type any of the numbers" if none of the previous conditions were met.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Else</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"You did not type any of the numbers"</span>&nbsp;<br>
</code></p>
<p>
And finally this line will end the If block and finish running the rest of the program.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
Now we are going to play with a few other operators for numbers only. In addition to being able to compare if two numbers are equal we can also compare if one number is greater than another, less than another, not equal to another, greater than or equal to another, etc. Here is a list of comparisons we can do with numbers.
</p>
<p>
Given we have two number stored in variables a and b.
</p>
<p>
•.a = b : Test if a is equal to b
</p>
<p>
•.a < b : Test if a is less than b
</p>
<p>
•.a > b : Test if a is greater than b
</p>
<p>
•.a <= b : Test if a is less than or equal to b
</p>
<p>
•.a >= b : Test if a is greater than or equal to b
</p>
<p>
•.a <> b : Test if a is not equal to b
</p>
<p>
Here is a few examples of doing some of these comparisons with a <b>IF</b> statement.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">If</span>&nbsp;a&nbsp;=&nbsp;b&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_comment">'Code to execute </span><br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">If</span>&nbsp;a&nbsp;&lt;&nbsp;b&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_comment">'Code to execute </span><br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">If</span>&nbsp;a&nbsp;&lt;&gt;&nbsp;b&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_comment">'Code to execute </span><br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
These other comparisons are pretty straight forward. To try these out we are going to make a program that ask the User to enter a number. If the number is less than or equal to 10 then "Low" will be output to the console. If the number is greater than 10 then we will check if the number is greater than 20. If it is then then "High" will be output to the console. If neither of these conditions are true then "Mid" will be output to the console. First we need to get a number from the User. Remember we need to use the <b>Val()</b> function to convert the User's input from a string to a number.
</p>
<p>
Here is where we take the User's input and convert it to a number. The <b>Input$()</b> function will get replaced with what the User types in so we simply use the <b>Val()</b> function to convert <b>Input$()</b> to a number.
</p>
<p id="rc_code"><code>
user_num&nbsp;=&nbsp;Val<b>(</b>&nbsp;Input$<b>(</b><span class="rc_string">"Enter a number: "</span><b>)</b>&nbsp;<b>)</b>&nbsp;<br>
</code></p>
<p>
We start our If block by comparing user_num (Our input from the User in the previous line) to the number 10. If the user_num variable is less than or equal to 10 then we use the Print statement to write "Low" to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">If</span>&nbsp;user_num&nbsp;&lt;=&nbsp;<span class="rc_number">10</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Low"</span>&nbsp;<br>
</code></p>
<p>
If the previous comparison is false then we compare the user_num variable to 20. If user_num is greater than 20 then we will write "High" to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">ElseIf</span>&nbsp;user_num&nbsp;&gt;&nbsp;<span class="rc_number">20</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"High"</span>&nbsp;<br>
</code></p>
<p>
Finally, if none of the previous conditions are true we will write "Mid" to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Else</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Mid"</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
Our complete program looks like this.
</p>
<p id="rc_code"><code>
user_num&nbsp;=&nbsp;Val<b>(</b>&nbsp;Input$<b>(</b><span class="rc_string">"Enter a number: "</span><b>)</b>&nbsp;<b>)</b>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">If</span>&nbsp;user_num&nbsp;&lt;=&nbsp;<span class="rc_number">10</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Low"</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">ElseIf</span>&nbsp;user_num&nbsp;&gt;&nbsp;<span class="rc_number">20</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"High"</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">Else</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Mid"</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
The last thing we will cover on flow control is the keywords <b>AND</b>, <b>OR</b>, <b>XOR</b>, and <b>NOT</b>. These 4 keywords are actually operators that can be used for more specific comparisons. We will start with the AND operator. The AND operator will compare the conditions to its left and its right. If both conditions are true the AND operator sets the whole expression as true. I already know you didn't understand that explanation so it will probably be easier to show you. Just look at this example.
</p>
<p>
This code will write "This is true" to the console because 1 is equal to 1 and 4 is greater than 2. Yes it is that simple.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">If</span>&nbsp;<span class="rc_number">1</span>=1&nbsp;<span class="rc_keyword">AND</span>&nbsp;<span class="rc_number">4</span>&gt;<span class="rc_number">2</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"This is true"</span>&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
The <b>OR</b> statement will set the whole expression it is being used in if at least one of the conditions is true. Here is an example of <b>OR</b>.
</p>
<p>
This code will write "This is true" to the console because although 1 is not equal to 2, 5 is less than or equal to 6.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">If</span>&nbsp;<span class="rc_number">1</span>=2&nbsp;<span class="rc_keyword">AND</span>&nbsp;<span class="rc_number">5</span>&lt;<span class="rc_number">6</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"This is true"</span>&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
The <b>XOR</b> statement is like the <b>OR</b> statement with one major difference. With <b>XOR</b>, one of the conditions in the expression has to be true and the other has to be false. Here is an example of <b>XOR</b>.
</p>
<p>
This code will write "This is true" to the console because 1 is equal to 1 and 2 is equal to 2 ( 2 <> 2 is false because 2=2. Remember <> is the NOT EQUAL operator). Basically one condition in the <b>XOR</b> expression has to be true and the other has to be false otherwise the whole expression is false. If this is slightly confusing don't worry. In my 15+ years of programming experience I have never once used this operator.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">If</span>&nbsp;<span class="rc_number">1</span>=1&nbsp;<span class="rc_keyword">XOR</span>&nbsp;<span class="rc_number">2</span>&nbsp;&lt;&gt;&nbsp;<span class="rc_number">2</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"This is true"</span>&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
The last of these special operators we will cover is <b>NOT</b>. This one will be extremely simple to understand. Basically if an expression is true it becomes false and if an expression is false it becomes true.
</p>
<p>
This code will write "This is true" to the screen because the expression 1=3 is false. The <b>NOT</b> statement reverses the false and makes it true.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">If</span>&nbsp;<span class="rc_keyword">NOT</span>&nbsp;<b>(</b><span class="rc_number">1</span>=3<b>)</b>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"This is true"</span>&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
Lets go over one more program before we move on to the next segment. We will ask the User for a number from 1 to 10. If the User enters 3 or 4 then we will write "Good Answer" to the console. If the User enters a number greater than 5 and less than 9 (Basically 6, 7, or 8) then we will write "Choice numbers" to the console. If the User types in any other number then we will write "Maybe next time" to the console.
</p>
<p>
Here we are asking the User to enter a number from 1 to 10 using the <b>Input$()</b> function. <b>Input$()</b> returns a string so we have to store it in a string variable.
</p>
<p id="rc_code"><code>
user_in$&nbsp;=&nbsp;Input$<b>(</b><span class="rc_string">"Enter a number from 1 to 10: "</span><b>)</b>&nbsp;<br>
</code></p>
<p>
Then we convert the string the User entered into a string using the <b>Val()</b> function. Note: We are using a string variable inside the <b>Val()</b> function instead of using <b>Input$()</b> inside it this time. I am showing it this way to get you used to the idea that <b>Val()</b> takes a string. It can be a string variable, string function, or just a regular string between quotation marks.
</p>
<p id="rc_code"><code>
num&nbsp;=&nbsp;Val<b>(</b>user_in$<b>)</b>&nbsp;<br>
</code></p>
<p>
In the first line of this piece of code we are comparing the variable num (which is the number the User entered) to the number 3 and we compare the variable num to the number 4. If the variable num is equal to the number 3 or 4 then we write "Good Answer" to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">If</span>&nbsp;num&nbsp;=&nbsp;<span class="rc_number">3</span>&nbsp;<span class="rc_keyword">OR</span>&nbsp;num&nbsp;=&nbsp;<span class="rc_number">4</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Good Answer"</span>&nbsp;<br>
</code></p>
<p>
This block of code compares the variable num to the number 5 and the number 9 if the previous If condition was false. If num is greater than 5 and num is less than 9 then we write "Choice numbers" to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">ElseIf</span>&nbsp;num&nbsp;&gt;&nbsp;<span class="rc_number">5</span>&nbsp;<span class="rc_keyword">AND</span>&nbsp;num&nbsp;&lt;&nbsp;<span class="rc_number">9</span>&nbsp;<span class="rc_keyword">Then</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Choice numbers"</span>&nbsp;<br>
</code></p>
<p>
Finally this last bit of code will write "Maybe next time" to the console if none of the previous <b>IF</b> conditions were true. It then ends the <b>IF</b> block.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">Else</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;<span class="rc_string">"Maybe next time"</span>&nbsp;<br>
&nbsp;&nbsp;<br>
<span class="rc_keyword">End</span>&nbsp;<span class="rc_keyword">If</span>&nbsp;<br>
</code></p>
<p>
<h2><b>FUNCTION</b></h2> This is going to be fairly short. So what is a function? A function is a block of code that is labelled by some kind of name so it can be called at any point in a program. I know you are confused again. But you have been using two functions several times throughout this tutorial. The Input$() function and the Val() function. A function has a return value that is either a number or a string. The Input$() function returns a string so it can be used anywhere in your program a normal string can be used. The Val() function returns a number so it can be used anywhere in your program a number can be used. Yes even inside a math problem. Are these the only two functions available in RC Basic. Not even close. There are over 200 functions available in RC Basic which allows you to do various task. Here is how a function is structured.
</p>
<p>
<b><i>function_name</i></b> ( argument1, argument2, ..etc ) - A function can have no arguments or several arguments. Arguments in a function can be numbers or strings, it just depends on what function you are using. So lets look at a few different functions to see how other functions are used.
</p>
<p>
Abs() returns the Absolute value of a number.
</p>
<p id="rc_code"><code>
Abs<b>(</b>&nbsp;-<span class="rc_number">4</span>&nbsp;<b>)</b>&nbsp;<br>
</code></p>
<p>
Date$() returns the current date as a string.
</p>
<p id="rc_code"><code>
Date$<b>(</b><b>)</b>&nbsp;<br>
</code></p>
<p>
UCase$() returns the argument you give it as an all uppercase string.
</p>
<p id="rc_code"><code>
UCase$<b>(</b><span class="rc_string">"Hello World"</span><b>)</b>&nbsp;<br>
</code></p>
<p>
As you can see there are different functions to accomplish different task. You can learn more functions by looking in the RC Basic reference manual located in the Docs folder in the rcbasic directory.
</p>
<p>
<h2><b>LOOPS</b></h2> We have finally made it to the last part of this tutorial. We are already fairly familiar with most of the core concepts of programming. We are finishing this lesson with a brief overview of loops. Loops are exactly what you think they are. They are ways of repeating blocks of code in your program as much as you need them to. Why would we need to repeat parts of our program? Well you will often have a program that you will want to continue running until the user is done using it or you will want to make a counter of some kind.
</p>
<p>
We are going to cover two types of loops. The first one we are going over is the <b>FOR</b> loop. For loops are used when you want to cycle through a block of code a set number of times. Lets just dive into the code.
</p>
<p>
Type this code into your editor and run it.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">For</span>&nbsp;i&nbsp;=&nbsp;<span class="rc_number">1</span>&nbsp;<span class="rc_keyword">to</span>&nbsp;<span class="rc_number">10</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;i&nbsp;<br>
<span class="rc_keyword">Next</span>&nbsp;<br>
</code></p>
<p>
The code in the example above writes the numbers 1 to 10 in the console. The <b>FOR</b> loop starts with a variable, which in this case is i. We set it with an initial value of 1 and tell the computer to loop the code between the <b>FOR</b> line and the <b>NEXT</b> line until i is equal to 10. When the <b>NEXT</b> statement is reached, i is increased by 1 and the program goes back to the start of the loop. That is how the <b>FOR</b> loop works in a nutshell. There is some extra control we can take over the For loop with the <b>STEP</b> statement. With the <b>STEP</b> statement we can change the amount the variable in the For line increases by. Look at the following example.
</p>
<p>
This code will write the even numbers from 2 to 10 to the console.
</p>
<p id="rc_code"><code>
<span class="rc_keyword">For</span>&nbsp;i&nbsp;=&nbsp;<span class="rc_number">2</span>&nbsp;<span class="rc_keyword">to</span>&nbsp;<span class="rc_number">10</span>&nbsp;Step&nbsp;<span class="rc_number">2</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;i&nbsp;<br>
<span class="rc_keyword">Next</span>&nbsp;<br>
</code></p>
<p>
The last loop we are covering in this tutorial is the <b>WHILE</b> loop. The <b>WHILE</b> loop will loop through a block of code while a certain condition is true. Look at the following example.
</p>
<p id="rc_code"><code>
i&nbsp;=&nbsp;<span class="rc_number">1</span>&nbsp;<br>
<span class="rc_keyword">While</span>&nbsp;i&nbsp;&lt;=&nbsp;<span class="rc_number">10</span>&nbsp;<br>
<span class="rc_keyword">Print</span>&nbsp;i&nbsp;<br>
i&nbsp;=&nbsp;i&nbsp;+&nbsp;<span class="rc_number">1</span>&nbsp;<br>
<span class="rc_keyword">Wend</span>&nbsp;<br>
</code></p>
<p>
In the example above we are doing a little bit more than we have been doing up to this point. First we are creating a variable called i. We start a While loop that compares i to the number 10. If i is less than or equal to 10 then we write the value stored in i to the console. Then we get to the line i = i + 1. This line is making i equal to the value stored in i plus 1. Basically we are increasing i by 1. The <b>WEND</b> line goes back to the start of the <b>WHILE</b> block where it compares i to 10 and if it is less than or equal to 10 it repeats the block of code again.
</p>
<p>
Now for some challenges.
</p>
<p>
1. Write a program that ask the User to enter a number. Then create a loop that outputs every number between 1 and the number the user enters to the console. 2. Write a program that continues to ask the User for there name until they enter "Bob". Then tell the User "Good by Bob".
</p>
<p>
</body>
</html>