GURU NANAK DEV CO-ED POLYTECHNIC

GURU NANAK DEV CO-ED POLYTECHNIC
MAIN BULIDING

Friday, 30 September 2011

(ii) if....else statement



                        if........else statement is used to execute one group of statements if the test condition is true or other group if the test condition is false. The syntax is

if (test condition)
            {
statement block-1;
}
else
            {
statement block-2;
}
next statement;

            When this statement is executed, the computer first evaluates the value of the test condition. If the value is true, statement block-1 is executed and the control is transferred to next statement. If the value is false, statement block-2 is executed and the control is transferred to next statement.

Conditions
(i)         The opening and curl closing brackets {} are must if the statement blocks
contain more than one statement.
(ii)        The brackets around the test condition are must
(iii)       The test condition must be a relational or logical expression.
(iv)       No statement other than the statements in the statement blocks are
allowed between if.........else.
(v)        Statement blocks are called body of the if......else statement. If can
contain one or more statements.

#include<stdio.h>
            main ( )
            {
                        int mark;
                        scanf(“%d”, &mark);
                        if (mark>40)
                                    printf(“PASS”);
                        else
                                    printf(“FAIL”);
            }

This program tests the performance of a student. If the mark obtained is more than 40 it prints pass. If it is less than 40 it prints fail.