WRITING CODE FOR THE C64: 4- BEGINNING BASIC #3

Thinking through Problem Solving as a Programmer

Most of us have been exposed to the idea that computers can make decisions. This is true but before the computer can make a decision, the programmer must tell it how. One of the magic elements about computer science is that after you have written a program and made it work correctly, it will seem like the computer has a brain. When you run the program and type in some data, the computer appears to think. It makes decisions based on the data and takes the appropriate actions.

What is important about this process is that you have provided this brain. It is your thought process which you have placed into the program and into the computer. When it runs the program, it is “thinking” in exactly the same way you were thinking when you wrote the program. The true magic in programming isn’t the BASIC keywords but your mind that put them there.

So the key element in helping the computer make decisions is thinking through the problem to be solved using the compute program. Step by step.

After you have figured out how to solve a problem step by step you write a program telling the computer how to do it. The BASIC programming language serves you by providing a way to give the computer instructions. However, there is nothing in BASIC or the rules of programming that will tell you how to solve a problem. You do that yourself. BASIC is merely the way to write it down.

Decisions

Most computer programs can be broken down to a series of decisions. All decisions involve making a choice between two or more alternatives. If there are no alternatives, there is no choice and therefore no decision to be made.

Decisions in computer programs are based on comparisons. Here are some examples of this. In these examples, X represents one number and Y another number.

If X is larger than Y, then chose Z.

If X is not zero, then choose Z.

If X is zero, then choose Z.

If the value of X is negative then choose Y

When writing a computer program you can’t imply a decision. You must provide all the possible decisions explicitly in the program so that the computer knows what to do under any condition.

Decision Building Blocks – The IF-THEN Statement

IF and THEN are BASIC words which are used to make decisions. IF is used to state the comparison or condition that allows a decision. For example IF X is not Zero. The word THEN is used to state the decision that results. For example THEN choose Z.

IF and THEN are used together in a single statement on a program line.

When a program reaches an IF-THEN statement, it checks to see if the IF condition is true or false. If true, it executes the THEN part of that statement.

If the IF condition is not true, the program jumps immediately to the next numbered program line. No part of the IF-THEN statement is executed. If there is more than one statement on that program line, nothing following the IF-THEN statement will be executed.

Image result for if then programming BASIC diagram

Comparison Symbols

Decisions are based on comparisons. BASIC has a set of symbols used to describe the comparison to be made.

IF X=Y – Means IF X IS EQUAL TO Y

IF X<Y – Means IF X IS LESS THAN Y

IF X> Y– Means IF X IS GREATER THAN Y

IF X<=Y – Means IF X IS LESS THAN OR EQUAL TO Y

IF X >=Y – Means IF X IS GREATER THAN OR EQUAL TO Y

IF X<>Y – Means IF X IS NOT EQUAL TO Y

The name given to these comparison symbols is relational operators. The word operator means that some operation has been performed. A relation operator compares the value of X to the value of Y.

GOTO

In the history of BASIC no keyword has ever achieved the same infamy as the GOTO keyword. This enables you to skip some lines in a program, jumping either forward or backward. For example the statement GOTO 100 causes the computer to go immediately to line 100 and execute it.

If, for example, line 50 says GOTO 100, the computer will jump over all the lines between 50 and 100. They will not be executed. The it will continue to execute from line 100, executing program lines in sequence, such as 110,120, and so on.

GOTO is part of the building blocks of decision making as GOTO statements are often used after a choice to send the program to the correct path for one decision.

Program Example

Let’s examine the following program which uses the GOTO keyword in a decision making process. Imagine you are a cowboy in Red Dead Redemption, this program will dictate how you will greet a fellow pilgrim on the trail.

Look at Line 20 before you RUN this code. What do you think will happen when you run the program?

Notice that due to the GOTO statement the program skipped execution of line 30?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s