Input, Processing, Output
All computer programs have three elements Input, Processing, and Output. Input is information (or data) fed into the computer. Input can be in the form of a textual string of characters from the keyboard, a mouse movement, or a data file. Output is information (or data) sent out of the computer. Output can be in the form of text displayed on the computer screen, mouse movement, graphics, or writing to a data file. Finally, Processing is any action performed on the input data to generate the output data.
Input
B2C has two commands to get data from the user and into the Cybiko computer. The major one is called Input and the other one is called 'key' (we'll discuss key later). Input takes data from the keypad and inserts it into a variable (well discuss variables more in the next chapter). The Input statement looks like this:
input a
The a in the input statement is the name of a variable, which receives the data.
In B2C the input statement accepts a prompt string. This string is displayed on the screen before the user enters their value:
dim name[32] as char
input "Enter your name", name
Output
The command for output to the Cybiko screen is Print. The Print statement takes one or more variables and character strings (separated by commas) and displays them on the screen. In its default mode, the screen holds 7 lines of about 23 characters each (The FONT command, as we'll later see, can change this).
print "The value of a is", a
The stuff between the quotes () is called a Literal String. This is a string of letters, digits, and other characters that you want displayed on the screen. Here, the variable a will be displayed. It is important to realize that a is not displayed, but rather, its value.
Also note that typing this statement into B2C alone will result in an error. You will need to Dimension (Dim) any variables before using them. Dim is covered in the next chapter.
Processing
Nearly everything else in the B2C set of commands can be considered processing. The simplest sort of processing is assignment. The assignment operation is the equal sign =. You can assign a value to a variable like this:
a = 1
Were giving the variable a a value of one. Consider the next examples
a=1 b=a+1 print b
Here, b is given the value a+1. In this case the variable a is replaced with the value of a (which is one). Hence, b=1+1 or b=2. So, the value of b is two.
Comments
All good computer languages have a way to document the code (programmer lingo for the statements comprising a program) in the program itself. Commenting a program is good practice in the event that you want to share your program with someone else. Commenting is also good as a way of reminding yourself what you intended when you wrote the code to begin with. It is good practice to have one line of commentary for each line of code on average. In B2C, comments are identified by a single-quote mark (also called the apostrophe mark).
' demo program for chapter 7 dim a as int dim b as int a=1 set a to one b=a+1 set b to one greater than a print b show the user the value of b
As well see in the upcoming example program, it is also useful to name your variables in a self-documenting way. If you are summing 4 grades, name the variables grade1, grade2, grade3, grade4, sum (the sum of the grades) and avg (the average of the grades). While variable names like a and b are short and easy to type, they are also cryptic and hard to remember.
Upper and Lower Case
B2C does not care about upper or lower case. A varaible name written in uppercase (A) in one place can be referred to later in lowercase (a). The names of statements in B2C are also not case sensitive. So Print and PRINT and print are all the same function.
Example Program
DO THIS |
Copy the file "c:\ \B2Cv5b\tutorial\ch7.b2c" to "C:\ \B2Cv5b\ch7.b2c". Then execute the command "build ch7.b2c". Download the ch7.app file to the cybiko. |
The example
program will take 4 values as input, sum them, and take the average.
The average is the sum of a set of numbers divided by the number of items.
' chapter 7 example program ' sum and average of 4 grades ' grades are from 0-100 dim grade0 as int ' well discuss the Dim command in the next chapter dim grade1 as int ' here are our 4 grades dim grade2 as int dim grade3 as int dim sum as int 'this variable will store the sum of the 4 grades dim avg as int 'this variable will store the average of the 4 grades ' ---INPUT --- print "Enter grade 0" 'well discuss numbering at 0 in the next chapter input grade0 'get the grades from the student print "Enter grade 1" input grade1 print "Enter grade 2" input grade2 print "Enter grade 3" input grade3 '--- PROCESS --- ' compute the sum and the average sum = grade0+grade1+grade2+grade3 avg = sum/4 '--- OUTPUT --- print "The average of your " print "4 grades is", avg print "Press <Enter> to continue" dim tmp ' a temporary variable input tmp 'wait for the user to press enter
|