Difference between revisions of "IF THEN ELSE"

From Ninerpedia
Jump to navigation Jump to search
m (→‎As used in Extended Basic: give source as manual)
m (→‎As used in TI BASIC: add link to TI Basic)
Line 3: Line 3:
==IF-THEN-ELSE==
==IF-THEN-ELSE==


===As used in TI BASIC===
===As used in [[TI BASIC]]===


The IF-THEN-ELSE statement allows you to change the normal sequence of your program execution by using a conditional branch.
The IF-THEN-ELSE statement allows you to change the normal sequence of your program execution by using a conditional branch.

Revision as of 13:13, 5 January 2015

This is taken from the final issue of the Users Reference Guide:

IF-THEN-ELSE

As used in TI BASIC

The IF-THEN-ELSE statement allows you to change the normal sequence of your program execution by using a conditional branch.

The computer evaluates the expression you have included in the statement, such as A>50. If the expression is true, the computer will jump to line-1, which follows the word THEN. If the condition is false, the computer will jump to line-2 following the word ELSE. If ELSE is omitted, the computer continues with the next program line.

The allowable relational operators

in TI Basic are:

  • Equal to (=)
  • Not equal to (<>)
  • Less than (<)
  • Less than or equal to (<=)
  • Greater than ( >)
  • Greater than or equal to (>=)

Some valid relationship tests:

  • A>7
  • CHR$(L)="A"
  • A$<"YES"
  • (A$&C$)>=D$
  • (A +B)/2<>AVG

Example

100 REM FIND THE LARGEST OF
  A SET OF NUMBERS
110 INPUT "HOW MANY VALUES?"
  :N 
120 INPUT "VALUE?":A
130 L=A
140 N=N-1
150 IF N<=0 THEN 180
160 INPUT "VALUE?":A
170 IF L>A THEN 140 ELSE 130
180 PRINT L;"IS THE LARGEST"
190 END
>RUN
 
  HOW MANY VALUES?3
  VALUE?456
  VALUE?321
  VALUE?292
  456 IS THE LARGEST
  ** DONE **

Comparisons

A numeric-expression must be compared to another numeric-expression and a string-expression to another string-expression.

Numeric-expressions are compared algebraically.

String-expressions are compared left-to-right, character by character, using the ASCII character codes. A character with a lower ASCII code will he considered less than one with a higher ASCII code. Thus, you can sort strings into numeric or alphabetic order. If one string is longer than the other, the comparison is made for each character in the shorter string. If there is no difference, the computer considers the longer string to be greater.

Example

>NEW
100 INPUT "AS IS ":AS
110 INPUT "B$ IS ":B$
120 IF A$=B$ THEN 160
130 IF A$<B$ THEN 180
140 PRINT "B$ IS LESS"
150 GOTO 190
160 PRINT "A$=B$"
170 GOTO 190
180 PRINT "B$ IS GREATER"
190 END

>RUN
 A$ IS TEXAS
 B$ IS TEX
 B$ IS LESS
** DONE **
>RUN
 A$ IS TAXES
 B$ IS TEX
 B$ IS GREATER
** DONE **

Using a direct value (0, not 0)

An alternative format of the IF-THEN-ELSE statement is to use a numeric-expression with no relationship expressed. In the example below, the computer will evaluate the expression A+B. If the result is zero, the expression is treated as false.

A non-zero result is treated as true.

This is the same as: IF expression <> 0 THEN line-1

Example

>NEW
100 INPUT "A IS ":A
110 INPUT "B IS ":B
120 IF A+B THEN 150
130 PRINT "RESULT IS ZERO,EX
 PRESSION FALSE"
140 GOTO 100
150 PRINT "RESULT IS NON-ZER
 O, EXPRESSION TRUE"
160 GO TO 100
>RUN

A IS 2
B IS 3
RESULT IS NON-ZERO,EXPRESSIO
N TRUE
A IS 2
B IS -2
RESULT IS ZERO,EXPRESSION FA
LSE

Press CLEAR to end loop.

As used in Extended Basic

(Taken from the Extended Basic Manual)

The above paragraphs on TI Basic use also apply to TI Extended Basic, however Extended Basic increases the functionlity as shown below:

The IF-THEN-ELSE statement allows you to transfer control to line-number1 or to perform statement1 if relational-expression is true or if numeric-expression is not equal to zero. Otherwise control passes to the next statement, or optionally to line-number2 or statement2.

Statement1 and statement2 may each be several statements long, separated by the statement separator symbol. They are only executed if the clause immediately before them is executed.

The IF-THEN-ELSE statement cannot contain DATA, DEF, DIM, FOR, NEXT, OPTION BASE, SUB, or SUBEND.

Examples

IF X>5 THEN GOSUB 300 ELSE X=X+5  

operates as follows: If X is greater than 5, then GOSUB 300 is executed. When the subroutine is ended, control returns to the line following this line.

If X is 5 or less, X is set equal to X+5 and control passes to the next line.

IF Q THEN C=C+1::GOTO 500 ELSE L=L/C::GOTO 300 

operates as follows: If Q is not zero, then C is set equal to C+1 and control is transferred to line 500. If Q is zero, then L is set equal to L/C and control is transferred to line 300.

IF A>3 THEN 300 ELSE A=0 :: GOTO 10 

operates as follows: If A is greater than 3, then control is transferred to line 300. Otherwise, A is reset to zero and control is transferred to line 10.

                                                                                                      109
100 IF A$="Y" THEN COUNT=COU 
NT+1::DISPLAY AT(24,1):"HERE 
WE GO AGAIN!"::GOTO 300   
IF A$="Y" THEN COUNT=COUNT+1:: DISPLAYAT(24,1):"HERE WE GO  AGAIN!"::GOTO 300 

operates as follows:

If A$ is not equal to "Y", then control passes to the next line. If A$ is equal to "Y", then COUNT is incremented by 1, a message is displayed, and control is transferred to line 300.

100 IF HOURS<=40 THEN PAY=HO 
URS*WAGE ELSE PAY=HOURS*WAGE 
+.5*WAGE*(HOURS-40) :: OT=1 
IF HOURS<=40 THEN PAY=HOURS*WAGE ELSE PAY=HOURS*WAGE+.5*WAGE*(HOURS-40) :: OT=1 

operates as follows:

If HOURS is less than or equal to 40, then PAY is set equal to HOURS*WAGE and control passes to the next line. If HOURS is greater than 40 then PAY is set equal to HOURS*WAGE+.5*WAGE* (HOURS-40), OT is set equal to 1, and control passes to the next line.

IF A=1 THEN IF B=2 THEN C=3 ELSE D=4 ELSE E=5 

operates as follows:

If A is not equal to 1, then E is set equal to 5 and control passes to the next line. If A is equal to 1 and B is not

equal to 2, then D is set equal to 4 and control passes to the next line. If A is equal to 1 and B is  equal to 2, then C is set equal to 3 and control passes to the next line.
                                                                 

The following program illustrates a use of IF-THEN-ELSE. It accepts up to 1000 numbers and then prints them in order from smallest to largest.

100 CALL CLEAR
110 DIM VALUE(1000)
120 PRINT "ENTER VALUES TO B
 E SORTED.": "ENTER '9999' TO
 END ENTRY."
130 FOR COUNT=1 TO 1000
140 INPUT VALUE(COUNT)
150 IF VALUE(COUNT)=9999 THE
 N 170
160 NEXT COUNT
170 COUNT=COUNT-1
180 PRINT "SORTING."
190 FOR SORT1=1 TO COUNT-1
200 FOR SORT2=SORT1+1 TO COU
 NT
210 IF VALUE(SORT1)>VALUE(SO
 RT2)THEN TEMP=VALUE(SORT1)::
  VALUE(SORT1)=VALUE(SORT2)::
  VALUE(SORT2)=TEMP
220 NEXT SORT2
230 NEXT SORT1
240 FOR SORTED=1 TO COUNT
250 PRINT VALUE(SORTED)
260 NEXT SORTED