Input/Output
Every language has an input and an output command, or commands. You take input from the keyboard or from a file and you can output it to a screen, or a printer, or a file. INPUT and PRINT are two commonly used algorithms. INPUT takes user input from the keyboard, and PRINT is used not to describe printing to a function, but printing out to the screen.
INPUT Name
PRINT "Hello" Name
There are some things you need to take note on here. First off the Hello in quotation marks (“) is called a string. That print statement will print out whatever is in the quotation marks exactly as it is. The second thing you need to notice is Name. Name is a variable.
Variables
You might remember from algebra that variables are put in place of numbers. The use of variables in programming are the same basic concept, but a computer cannot have unsolved variables in a program. What I mean for example you can’t say that x = y + 4 cannot happen if you do not define what y is. Different languages do different things with uninitialized variables, for example Javascript and Visual Basic will initialize it to 0, while other languages will put garbage numbers in there that are left over from other variables, so before using variables it is good practice to initialize them such as:
y = 0
x = 0
x = y + 5
Unless you are talking about the X and Y coordinates, the variables x and y don’t really tell us much. When choosing variables, it is important to use names that mean something to you, and whoever else uses your code, like student_name. Notice the underscore. I don’t know all programming languages, but I don’t know any that allow variables to contain spaces, so the underscore normally comes in place of a space.
Unlike algebra, programming allows you to enter strings of data, or words into variables.
Pass_phrase="Kitty"
Conditionals
Conditional statements are just as it sounds. If a condition is true then do this else do something else. Normally what happens in conditionals, an inequality symbol is used to compare the two properties. Below is a list of symbols commonly used for algorithms.
< | less than |
> | greater than |
= | equal to |
<= | less than or equal to |
>= | greater than or equal to |
<> or ! | not equal to |
A simple conditional or IF statement looks like this:
IF GPA >= 4.0 THEN
honor_roll_status = On honor roll
ELSE
honor_roll_status = Not on honor roll
ENDIF
Notice where the indentation is, also take note of where the action is. We put it on the next line and indent it to make it easier to read. You will want to do this with your code so as to make it much easier to read for others as well as yourself. You may also combine IF statements using AND and OR.
IF student_grades >= 80 AND student_grades < 90 THEN
grade = B
ELSE
honor_roll_status = Not on honor roll
ENDIF
Case Structure
If you have a lot of IF statements, it can get hard to read, there is a simple solution: CASE STRUCTURE. Case Structure can easily be translated using high level languages, and it usually only involves one variable. Here is a good algorithm of part of a program for a student’s grade letter:
INPUT fall_semester_grade
CASE OF fall_semester_grade
Case 'A': PRINT "Congradulations on your A" Case 'B': PRINT "Congradulations on your B"Case 'C': PRINT "Really good effort"
Case 'D': PRINT "You need to study more"
Case 'F': PRINT "I'll see you next semester"
Notice after the word Case we had letters in single quotes. Rule of thumb is for single non-numerical characters, use single quotes. For a STRING of characters (2 or more characters) use double quotes.