The IF..THEN..ELSE statement allows us to make the decision regarding program flow based on the result returned by an expression.
Syntax
LN IF expression (,) THEN statement(,) ELSE statement
- if the result of expression is true, the THEN statement is executed.
- if the result of expression is false, the THEN line number is ignored and the ELSE line number is executed.
- THEN and ELSE may be followed by either a line number for branching or one or more statements to be executed.
- IF..THEN..ELSE is a single statement. They must be written in one line.
Example
1. 10 IF A>B THEN PRINT "A is greater " ELSE PRINT "B is Greater"
It will print the message "A is Greater" if A is greater than B and will print "B is Greater" if B is greater than A
Use of IF..THEN...ELSE statement in the program.
1. Write a program to decide whether a student has passed or failed in the exam. ( use IF...THEN..ELSE) Pass marks=32
CS 7.7 Display on the screen for IF..THEN..ELSE statement
Ok10 REM Program to check whether a student is passed
15 REM or failed in the S.L..C. Exam
30 INPUT "Enter the marks obtained :" ;M
40 IF M>=32 THEN 45 ELSE 65
45 PRINT "MARKS OBTAINED :"; M
50 PRINT "CONGRATULATIONS :"; HE PASSED
60 GOTO 80
65 PRINT "MARKS OBTAINED :"; M
70 PRINT "SORRY ! HE FAILED"
80 END
Ok
RUN
Enter the marks obtained : ? 76
Marks OBTAINED : 76
CONGRATULATIONS ! HE PASSED
Ok
RUN
Enter the marks obtained : ? 23
MARKS OBTAINED : 23
SORRY ! HE FAILED
2. CS 7.8 Display on the screen for IF...THEN...ELSE statement
OkLIST
10 CLS
15 REM Learning the use of IF...THEN..ELSE statemet
20 INPUT "ENTER THE FIRST NUMBER" ; X
30 INPUT "ENTER THE SECOND NUMBER" ; Y
40 INPUT "THE THIRD NUMBER" ; Z
50 IF X.>Y THEN GOTO 90 ELSE GOTO 100
90 IF X>Z THEN GOTO 130
95 GOTO 150
100 IF Y>Z THEN GOTO 140
105 GOTO 90
130 PRINT "THE LARGEST NUMBER IS" ; X
135 GOTO 160
140 PRINT "THE LARGEST NUMBER IS "; Y
145 GOTO 160
150 PRINT "THE LARGEST NUMBER IS"; Z
160 END
OK
RUN
No comments:
Post a Comment