1. WHAT CONTROL STATEMENTS DO
In Lesson 1, you learned that a program is just a set of instructions that tell a computer what to do in a language it can understand. In this lesson, you'll learn about the different logical structures in a computer program that you use to create those instructions.
Every scripting or programming language has a different set of rules -- called its syntax -- for using these different structures in a program written in that language. Although the syntax may be different across languages, the basic use of each structure is the same. For a general discussion, we like to avoid using only the syntax of a single language, so we use pseudocode . Pseudocode is simply a way of writing instructions in something close to natural English but that's related to typical programming languages. Don't worry, it will all become clear.
Scripting languages and full programming languages use structures called control statements to hold instructions that help computers make decisions about what to do ( conditional statements ), or perform tasks multiple times ( loops ).
For example, say you want to have the computer read your e-mail for you and then delete all the messages you've received that look like spam advertisements. In this case, you'd want the program to do the following:
- Open your e-mail inbox and find your new e-mail messages.
- Look at the first message.
- See if this message looks like annoying spam.
- Delete the message if it looks like annoying spam.
- Leave the message untouched if it may be a real message.
- Go on to the next message and repeat the process until all messages have been checked.
In this case, you're having the computer do work for you. To do that, it has to be able to make decisions. You have to tell it exactly what to decide, and when. This is one of the fundamental rules of programming: Computers only do what their instructions tell them.
In the case of this e-mail routine, the computer has to make a decision with each message whether or not it should delete it. You have to tell it to check the e-mail subject, and then what text to look for. What the computer does from there depends on the message passing this subject-matching test. This example includes both a conditional statement (if the message subject has certain words, delete the message) and a loop (keep checking each message until all messages are tested).
2. LOOPS AND CONDITIONAL STATEMENTS
To write loops and conditional statements, you use statements that make a logical decision. The most common conditional statement is what's called an if-then statement. This is exactly what it sounds like. You might tell the computer:
IF (the message subject starts with ADV) THEN (delete the message)
Different programming languages have slightly different ways of phrasing and punctuating that statement, but they all mean roughly the same thing. One common extension of that is what is called an if-then-else statement. This says:
IF (some logical condition is met) THEN (do something) ELSE (do something else)
We call the (do something) part the true branch of the if-then-else and the (do something else) the false branch.
The other control statement used in the e-mail testing routine is called a loop . A loop is a set of instructions that can be executed one or many times, depending on a specific logical condition. In the example, the program repeats while there are still messages.
There are several different kinds of loops: loops that are executed according to a counter, and that execute until a specific condition is met, among others. In some languages, there are actually even more possibilities. The ones I mentioned, however, are the most common.
In this case, you want your message processing to happen for each message in the inbox, so you use a while loop that looks similar to the following:
WHILE (there are messages we haven't looked at) process the message END WHILE
The END WHILE statement is there so that the computer knows
which instructions are part of the loop, and where the loop ends. The concept
here is simple. While the test statement is true (there are messages you
haven't looked at), keep doing whatever is in the loop.
There are several types of loop statements, but usually you can figure out which one to use simply by phrasing it as if you were giving instructions to a human. For example, you can phrase your loop as, "Do this until you're out of messages," or, "Do this 100 times."
In pseudocode, the following is what we call a DO-UNTIL loop:
DO process the message UNTIL (there are no more messages to process)
Note that because this loop always executes the process at least once, you must be sure there's at least one message to process before entering the DO.
Different programming languages have different syntaxes and terms for loop control statements. The concepts, however, are universal.
Loops with Counters
A for loop uses a counter to perform a task a specified number of times. If your e-mail program knew in advance how many message were waiting, you could write the processing loop this way:
FOR (number of messages) process the next message END FOR
Now, let's go back to your e-mail checking routine and fill out some details. In terms of the control statements just discussed, it now looks something like this:
1. Open the mailbox and get any new messages.
2. WHILE (there are more messages to look at) do the following:
1. Look at the subject of the message.
2. IF (the subject starts with ADV)
3. THEN delete it.
3. Get the next message.
4. END WHILE.
5. Close the mailbox. The program is done.
Notice that it was important to tell the program to get the next message in Step 3; otherwise, your loop would keep testing the same message over and over again. Not advancing to the next item in a loop is a very common error. Remember, computers tend to be literal and don't usually read programmers' minds very well.
Conditional Statements
In conditional statements you use Boolean expressions to tell the computer to make a decision. A Boolean expression is one that can be evaluated as either true or false. Remember back in school when you learned how to compare two numbers? This is the same idea. A Boolean expression asks the computer to do one of the following:
- Tell you if one value is greater than another
- Tell you if one value is less than another
- Tell you if one value is equal to another
More advanced Boolean operations use greater than or equal to, less than or equal to, and the idea of not, but it's essentially grade-school arithmetic in action.
In the example e-mail program, the message is deleted if the subject contains a certain string. In an actual program you would use a Boolean expression to specify this condition in terms a computer can understand. Before you can work with control statements, you need to have a good handle on Boolean expression.
Programming languages provide a syntax for creating Boolean expressions about data of different types. For example, when comparing numbers, such as the number of letters in a name compared with the maximum number allowed, you can write an expression that is true when one number is equal to another. When comparing text, you can write an expression that is true if one word comes before another alphabetically.
You can also combine Boolean results with the familiar AND, OR, and NOT logic. For example, you only want to discard long e-mails from Julie, you might write something similar to this:
IF (the message is from Julie
AND the message is longer than 30 lines)
THEN delete it
3. PROGRAM WITH VARIABLES AND CONSTANTS
In programming terms, variables are where items of
data live. Programs deal with all types of data, such as numbers, dates,
names, and letters. Variables provide the convention for naming, storing, and retrieving these different kinds of
information. A variable is simply a name used to store and refer to a value.
For example, you can define a variable called count and set it
to store the number 5 by doing the following:
count = 5
You can then treat this variable the same way you'd treat a number. If you
add count to a number, it's just as if you added 5. Another
variable, called total , is used in the following:
total = count + 2
where, if count is still storing the value 5, you just set
total=7 . You can also do operations such as:
count = count +10
where you just added 10 to the value held in count .
One important point about variables is that, in most languages, you need to define what type of information you're going to use them to store. It could be a type of number, letters, or something else, such as a collection of variables as discussed in the following section. For the previous examples, you need to define count and total before you use them, as shown in the following:
integer: count, total
An integer is a number such as 1, 2, 3, 4, and so on, with no decimal. For other numbers you can use a type called real or float (short for floating point). Different languages store text in different ways; for these pseudocode examples you use a type called string that stores text as a string of letters. The analogy is a string of beads, one after the other.
There are other special variables called constants
. These are variables whose values you set once and
never change. They can be thought of as special variables. For example, in a
mathematical program, you might declare a constant called pi ,
as shown in the following:
real constant : pi pi = 3.14
The value of pi is not going to change as you use it, so you
store this value in a constant. Different languages use different methods of
declaring variables and constants; these are covered later in the course. For
now, just use this pseudocode version.
Collections of Variables
Most programming languages also allow you to put your data values into a list or array. A list or array is like a grocery list in real life: It wouldn't make much sense to write each thing you need from the grocery store on separate sheets of paper. Instead, you put all these items into a list.
In most programming languages, you can step through a list of data one item
at a time using a control structure such as FOR -- END FOR .
Many programming languages also allow you to pick an individual item from a
list using an index. For example, if you want to
grab the third item in a list, you'd use that list item's index number to
access it.
To continue with the earlier e-mail checking example, you might provide the program with an array of names of people who are sending you annoying spam. The inner part of the WHILE control structure would then look something similar to this:
1. Look at the sender of the email
2. FOR (every N in the SPAMMER array)
1. IF (the sender equals the Nth SPAMMER)
2. THEN delete the email and stop the loop
3. END FOR
By using an array, you now have a program that can handle multiple spammer
names with only one named variable. Arrays combined with the FOR - END
FOR control structure make it possible to manipulate large amounts of
data with simple commands. In addition, many languages provide built-in
functions that work arrays. For example, you can sort an array of strings
alphabetically with a single command.
Some programming languages have specialized structures to add additional logic to variables they hold onto. For example, a stack structure holds onto values in the order they are added, and enables you to get at them in terms of their position relative to the top of the stack.
