If statement in c programming

IF statement is one of the powerful decision making statement in c programming. IF statement in c program is used when we need to execute our program code as per some condition.

If statement takes the following form in c programming 
if( test expression )

Above form has two way branching. In which first it check or evaluate the expression first and than after it will transfer the control of the particular statement as depending on whether it is true or false.

After the evaluate the expression it has two path to follow one is true condition and another is false condition.
ifstatementflowchart


The if statement has many different form depending on complexity of conditions. The following are the different form of if statement.
  • 1. Simple if statement.
  • 2. If... else Statement
  • 3. Nested if... else statements
  • 4. else if ladder.

Simple if statement in c programming

The general syntax of simple if statement is :
if( expression )
{
     Staement-block;
}
statement-x;

In this syntax first it statement check the expression if the expression is true than the control flow transfer to the statement block and it execute the statement(s) of statement block and then after it execute the statement-x.

And if the expression is false than it directly jump on statement-x and it will skip the statement blocks statement.

To represent the flow of control simple if statement as follow.
simpleifstatementflowchart

Example for simple if statement in c programming: 
Suppose a customer purchase two items and if there cost is greater than 2000 than he/she eligible to get 200 discount other wise not.

#include<stdio.h>

void main()
{
int item1,item2,total=0;
clrscr();
printf("Amount of item1:: ");
scanf("%d",&item1);
printf("Amount of item2:: ");
scanf("%d", &item2);
total=item1+item2;

if(total>=2000)
{
total=total-200;
printf("\nYou are eligible to get 200 discount.");

}
printf("\nNet payble amount:: %d ", total);
getch();

}



ifelsestatementflowchart



If... else Statement in c programming

The if... else statement is extension of simple if statement. It provide additional facility with simple if statement.

The general syntax of if... else statement.
if( test expression )
{
     True statement block;
}
else
{
     false statement block;
}
statement-x;



In if... else statement it  provide two blocks, first one is true statement block and second one is statement block.

if the test expression in if is true than it execute the true statement block other wise it execute the false statement block. But in which it execute only one block it either true-statement block or false statement block not both.It execute true statement block only if test expression is true.

Flow chart of if... else statement is as follow.



Example of if... else statement in c programming:
Suppose there are 5 boys and 5 girls and user insert new entry of student and count total number of boys and girls.

#include<stdio.h>
void main()
{
int boys=5,girls=5,gen=0;
char name[20];
clrscr();
printf("Enter name of student:: ");
scanf("%s",name);
printf("\nEnter Gender (1-Boy, 2-Girl )::");
scanf("%d", &gen);
if(gen==1)
{
boys=boys+1;
}
else
{
girls=girls+1;
}

printf("\nTotal No. of \nBoys: %d and Girls: %d ",boys,girls);
getch();
}

ifelsestatement


Nested if... else statement in c programming
Nested if... else statement means to use if... else statement within if... else statement.

Basic form of nested if... else statement as follows.
if( test expression )
{
     if( test expression )
    {
          statement-1;
    }
    else
    {
         statement-2;
    }
}
else
{
     statement-3;
}
statement-x;



In above form if outter if test expression true than control transfer to it, inner block and it further check the test condition of if test condition and if it is true than it execute statement-1 else it will execute statement-2.

If the outter if test expression false than flow of control transfer to its else block and it execute the statement-3.

Flowchart of nested if... else statement.
flowchartnestedifelse


Example of nested if... else in c programming.
Suppose a bank introduce there incentive policy of giving bonus to its deposit holders.And its policy as follows.

  • If deposit holder balance amount is greater than 5000 than it get 5% bonus
  • And if deposit holder is female than it get additional 2% bonus.


#include<stdio.h>

void main()
{
int gen=0;
float bal=0,total=0;
char name[20];
clrscr();

printf("Enter the name:: ");
scanf("%s",&name);
printf("\nEnter gen(1-male, 2-female)::");
scanf("%d",&gen);
printf("\nEnter the balance amount::");
scanf("%f",&bal);

if(bal>=2000)
{
if(gen==2)
{
total=((bal*5)/100);
}
else
{
total=((bal*2)/100);
}
}
else
{
printf("\nSorry you need more than 2000 balance to get bonus.");
}
total=total+bal;

printf("Total balance after balance:: %.2f",total);
getch();
}





else if ladder in c programming

There is another way to putting multiple if conditions together in c programming when multipath decisions are involve.

It takes following basics form in c programming.

if( condition-1 )
{
     statement-1;
}
else if( condition-2)
{
     statement-2;
}
else if( condition-3 )
{
     statement-3;
}
else
{
     default-satatement;
}
statement-x;


Above construction is called else if ladder. The conditions are evaluated from top to down words. In which as soon as true condition found it execute its corresponding statement and control transfer to statement-x. Otherwise if all n conditions are false than it execute the default statement from the else part and than control transfer to statement-x.

The flowchart for else if ladder in c programming.
elseladderflowchart



C programming example for else if ladder:
For example writing a c program to check the number is zero or positive or negative.

C programming code without else if ladder or by simple if statement.


#include<stdio.h>

void main()
{
int no;
clrscr();
printf("Enter number::");
scanf("%d",&no);

if(no==0)
{
printf("Number is ZERO ");
}
if(no>0)
{
printf("Number is Positive");
}
if(no<0)
{
printf("Number is Negative");
}

getch();
}

cprogramtofindpositivenegativezeronumber

In above code we need use more than one if statement or we putting multiple if statement in single program. In this type of situation we use the if else ladder, Now let see one c programming example of if else ladder.

#include<stdio.h>

void main()
{

int no;
clrscr();
printf("Enter Number:: ");
scanf("%d",&no);

if(no==0)
{
printf("Number is ZERO");
}
else if(no>0)
{
printf("Number is Positive");
}
else
{
printf("Number is Negative");
}
getch();
}

elseifladderexampleinc




0 comments:

Post a Comment