BWD Chapter 05: Let your programming make a statement

2004-11-01 22:17 - Beginners Web Development

Chapter 05
Let your programming make a statement

In chapter three we learned our first statement, the print statement. There are of course many other important statements, each solving a particular problem. In this chapter we will learn about conditionals. There are a few types, and they form the bread and butter of programming. The strong power of a computer is to perform many small tasks quickly. This power is leveraged to solve large problems by breaking them down into smaller pieces which the machine can rapidly complete.

But of course, there are special cases. We used making a peanut butter and jelly sandiwch as an example earlier. What if, in our programmer's view of things, we tried to open the peanut butter jar and found it to be empty? We could not continue making the sandwich, we need to deal with the situation. We need to consider the possibility for the jar being empty and conditionally act on the possibilities. If the jar proves to be empty, find another. If there is no other available, we would give up and skip the rest if the steps that we woulld have followed.

This is the realm of the if statement. The if statement lets us check conditions, and then perform steps based on the information. We will use a piece of pseudocode here to explain the idea. Pseudocode is derived from the root forms pseudo and code, code being the programming code that we are designing, and pseudo being "not real but close". Pseudocode is a good planning tool, it can be used to write a whole program, simply skim over the easy concepts, work them in with descriptive phrasing. The overall structure of the program is written without worrying about the small specific details.

get two slices of bread
get jar of peanut butter
if jar is empty then
  get a new jar
  if no new jar is available then
    give up
place peanut butter on one slice of bread
...

That pseudocode above describes the first few steps for writing an imaginary program to make a PBJ sandwich. It is written in no specific language, as it is for humans to interperet. There is though a truly critical and real programming concept introduced. You probably understood it without my even saying what it was, but it is vital now to name it and know it through to the core. It is indentation. Programming languages allow blank space at the beginning of a line, and you should well make use of it. Without proper indentation most programs become unreadable

After each sentence that said "if something" I put things to do if the something was true. I grouped the actions together, and indicated which they were, by putting space in front of them. This let me see that if the jar is empty, I get another. On the other hand, if it is not empty I skip the indented lines and continue to place the peanut butter on the bread. Inside my if actions, I have another if, whose actions are also indented, another space over from where this if started.

We can see at a glance which statements are grouped with which. If we ever need to come back and change the program, this is extremely important. Without it we would need to read and carefully ponder each line to find where the if starts and stops. In real programming, there is usually an explicit marker for where the if actions end, but it can still be a lot of extra work to find the marker, or especially to match multiple ifs with the markers they belong to, without indentation.

Let's learn how the if statement really works. It has three basic parts: the if statement itself including a condition to check, the statement(s) to execute if the condition is true, and the marker ending those statements and the whole if block. It looks like this:

IF (a = 5) THEN
  PRINT "a equals five"
END IF

This is a basic if statement. It begins, of course, with the keyword IF. Remember that keywords are special words that the programming language assigns meaning to, and these names cannot be used for variables. We then see our conditional to be tested, in this case we are comparing the variable a to the constant 5, with the equality comparison operator. If that operator returns a true value, we print a message to the screen saying that a is five. Otherwise, the statement inside is skipped until the END IF statement.

Hopefully you already see how useful the if statement can be. But wait there's more, call now and we'll throw in the ELSE and ELSEIF statements for free! The ELSE statement is used to have the IF statement do two different things, one when the condition is true, one when the condition is false.

IF (a = 5) THEN
  PRINT "a equals five"
ELSE
  PRINT "a does not equal five"
END IF

The code block above clearly checks the value of a, and lets us know when a is five, and when it is not. We still have the ELSEIF statement too though!

IF (a = 5) THEN
  PRINT "a equals five"
ELSEIF (a < 5) THEN
  PRINT "a is less than five"
ELSE
  PRINT "a must be greater than five"
END IF

Now we see that one if statement can have many conditions to check and many statements to execute depending on those conditions. First note that within the realm of one IF ... END IF though there can be many statement branches, only one will be executed. Once one condition is true, there is no more "else" to worry about. Also, one IF block may have as many ELSEIFs as you desire. I have put parenthesis around my conditions above. BASIC does not require this, but some languages do. Parenthesis often help you read the code later just like indentation, and are thus often a good idea anyway.

The IF statement truly begins to harness the power of variables. Remember variables are just values that can vary while the program is running. Therefore at any point in time the program will not know what value is in the statement. One way to deal with this is to use the IF statement to test the variable, and take appropriate actions based on the value. Think a bit about this. In the next chapter we will introduce another statement that will make variables and IFs much more fun!

Comments:

No comments!

Post a comment:

Username
Password
  If you do not have an account to log in to yet, register your own account. You will not enter any personal info and need not supply an email address.
Subject:
Comment:

You may use Markdown syntax in the comment, but no HTML. Hints:

If you are attempting to contact me, ask me a question, etc, please send me a message through the contact form rather than posting a comment here. Thank you. (If you post a comment anyway when it should be a message to me, I'll probably just delete your comment. I don't like clutter.)