if-statement

View


Sometimes we require executing one or more statements based on some condition. In such situation we can use if- statement.

Syntax- of if-block

                                    if ( condition )

                                    {

                                                Statements (s);                       // one or more statements

                                    }

Here, a condition is specified with if. If the condition is TRUE then statement(s) inside if-block will be executed. If the condition is FALSE, then statement(s) inside if-block will be skipped.

 Flowchart-

Example-

A shopkeeper gives discount of 10% if customer makes purchase of rupees 1000 or more. If purchase amount is input through the keyboard, write a program to print discount and Net amount.

 /* Program to display discount and net amount [discount.c] */
#include < stdio.h >
#include < conio.h >

void main ( )
{
            // Variable Declaration
            float pa, dis, na;
            clrscr ( );
                                                                                                         
            // Input
            printf ( “ \n Enter the Purchase Amount \n “ );
            scanf ( “ %f “, &pa );
 
            // Processing
            dis = 0;
            if ( pa > = 1000 )
            {
                        dis = pa * 0.1;
            }
            na = pa – dis ;
   
            // Output
            printf ( “ \n Discount = %f “, dis );
            printf ( “ \n Net Amount = %f “, na );
            getch ( );
}


Last modified: Tuesday, 21 September 2021, 9:43 AM