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