Custom Search
 
  

 
IF-THEN Statement (Conditional)

Perhaps one of the most powerful statements in BASICs set of instructions is the IF-THEN statement. It is a statement of conditional branching; that is, it conditionally changes the consecutive order of execution of statements depending on the outcome of some test or relationship. Relational symbols and expressions are used in constructing IF-THEN statements. If the relationship proves true, control is transferred to a specified line number. If the relationship proves false, the program proceeds to the next sequential state-ment. The following example shows the fundamental concept of how an IF-THEN statement works and another use of the GOTO statement.

Example:

In this example, the test of the relationship between A and B is made at line 20 with a conditional branch to line 50 if A is equal to B. If A is not equal to B, the next statement executed is at line 30. In the latter case we do not want the program to execute the statement at line 50. To avoid this, we write a GOTO statement at line 40, which unconditionally transfers control to line 60.

As you can see, the IF-THEN statement causes a change in execution sequence only on meeting a particular condition of data. In contrast, the GOTO statement, line 40 causes a change in sequence every time it is executed.

Loop Control with IF-THEN

We have seen the IF-THEN statement used as a tool for branching to a different area of a program; now lets examine its use in controlling a loop. Remember, a loop is a sequence of statements that is to be repeated some specified number of times, or until a particular condition is met. The number of times through the sequence can be a constant number fixed at the time a program is written, or it can be a variable number depending upon some condition existing at the time of program execution. You, as the programmer, are responsible for initiating the loop and controlling how many times the program will execute the loop.

Consider the task of calculating all powers of 2 from 2** 1 through 2** 10. (2**10is the notation BASIC uses for "2 raised to the tenth power," exponentiation.) The following procedure shows how to do this, using the loop technique:

(1) Set K = 1

(2) Compute 2**K

(3) Print 2**K

(4) If K is equal to 10, Stop

(5) Add 1 to K

(6) Go back to step (2)

The flowchart for this procedure would look like this:

Weve used the variable K as the exponent so we can vary its value from 1 to 10 during program execution. K will initially be set to 1. Then 2**K will be computed and printed. Since K is not equal to 10 the program will take the "NO" path, add 1 to K and go back to repeat step 2. Now 2**K (which is 2**2) is computed and printed, then the values for 2**3, 2**4 . . . , 2**10 are all computed and printed. However, after 2**10 has been printed, K will equal 10 and the program will take the "YES" path, causing the program to halt.







Western Governors University
 


Privacy Statement - Copyright Information. - Contact Us

Integrated Publishing, Inc. - A (SDVOSB) Service Disabled Veteran Owned Small Business