The IF... THEN statement allows us to make a decision regarding program flow based on the result returned by an expression.
Syntax
LN IF expression [,] THEN statement[,]
Points to remember
- If the result of expression is true (or non zero), THEN then statement is executed.
- If the result of the expression is false (or zero), the THEN statement is ignored and execution continues with the next executable statement.
- A comma is allowed before THEN
CS 7.5 Display on the screen for IF..THEN statement
Use of IF..THEN statement in the program.
1. To check the number for given data and print error message.
Minimum and maximum value data are 10 and 100 respectively.
Ok
LIST
10 REM Learning the use of IF...THEN statement
30 INPUT A
40 IF A<10 THEN PRINT "DATA BELOW THE RANGE"
45 IF A>=10 AND A<=100 THEN PRINT "DATA WITHIN THE RANGE"
50 IF A>100 THEN PRINT "DATA ABOVE THE RANGE"
60 IF A<>0 THEN 30
70 END
Ok
RUN
? 5
DATA BELOW THE RANGE
? 10
DATA WITHIN THE RANGE
? 35
DATA WITHIN THE RANGE
? 150
DATA ABOVE THE RANGE
? 0
DATA BELOW THE RANGE
Ok
2.CS 7.6 Display on the screen for IF...THEN statement
Ok
LIST
20 REM To find the largest number among X.Y and Z
30 INPUT " ENTER THE FIRST NUMBER = "; X
40 INPUT " ENTER THE SECOND NUMBER = ";Y
50 INPUT " ENTER THE THIRD NUMBER = ";Z
60 IF X>Y THEN GOTO 100
70 IF Y>Z THEN GOTO 110
80 PRINT " THE LARGEST NUMBER IS "; Z
90 GOTO 140
100 IF X>Z THEN GOTO 130
105 GOTO 80
110 PRINT " THE LARGEST NUMBER IS " ; Y
120 GOTO 140
130 PRINT " THE LARGEST NUMBER IS " ; X
140 END
Ok
RUN
ENTER THE FIRST NUMBER = ? 68
ENTER THE SECOND NUMBER = ? 139
ENTER THE THIRD NUMBER = ? 23
THE LARGEST NUMBER IS 139
No comments:
Post a Comment