The IF Statement

The IF Statement

    This statement helps in transferring the control of program execution from one part of the program to another depending on some test-condition.
    There are two types
    • IF-THEN statement
    • IF-THEN-ELSE statement
    IF-THEN statement
    Its format is
    if -test
    where
    n – is the statement number
    test-condition – may be relational or logical expression.

    When the IF-THEN statement is executed in a program, the test-condition is tested. If the test-condition is true, control of program execution is transferred to statement labeled n. If the test-condition is False, the statement immediately following the IF-THEN statement is executed.
    Eg :
    5 IF A>10 THEN 125

    Program
    Given a set of 6 values as -5, 2, 6, -1.1, 0, 4
    Develop a program that computes the sum of the values greater than zero.
    10 DATA -5, 2, 6, -1.1, 0, 4
    20 LET N=0
    25 LET S=0
    30 READ A
    40 LET N = N+1
    50 IF A<=0 THEN 70
    60 LET S=S+A
    70 IF N<6 THEN 30
    80 PRINT “SUM=”;S
    90 STOP
    100 END
    Output
    SUM=12
    2) IF-THEN-ELSE statement
    This is more general form of IF statement.
    Its syntax is
    if
    where
    n1 and n2 are labels of other statements.
    When the test-condition is true, statement labeled n1is executed. However when the test-condition is false, statement n2 is executed.
    Eg:
    60 IF A<> B THEN 100 ELSE 200

Last modified: Wednesday, 18 January 2012, 10:48 PM