Introduction to If Else Statement

Decision Construct: - An Introduction
They provide the facility to specify the certain condition in a program and associate a set of statements with that condition. These conditions are evaluated during the execution of program.

These conditions must return a Boolean value true or false. The associated group of statements is executed if the given condition is true.

If the given condition is false it skips the associated group of statements and executes the remaining statements of a program.

C language provides three Decision constructs. They are:
i].If Statement                                    
ii].Switch…case Statement                             
iii].Conditional Statement

1].if Statement
If statement is very simple decision making statement in ‘C’ language. It allows the user to control the flow of execution with the help of a condition and transfer the control to particular direction.

This statement divide the program into two section one for true condition and other for false condition. Thus it is a basically two way decision statement.

Generally if statement has the following forms: -
i). Simple IF statement                       
ii).If …Else statement             
iii). If Else if statement                      
iv). Nested if




i). Simple IF statement
If statement is a very simple decision making statement in ‘C’ language. The general form of the if statement is
if(condition)
{
           statement 1      
           . . . . . . .
           . . . . . . .
          statement N
}

The if statement consists of two parts:-
a).Condition:-The condition is a Boolean expression that must returns true (nonzero) or false (zero) value. We can include any relational operators in condition such as <, >, <=, >=, = = (for equality).

b).Body:-The Body consists of one or more statements enclosed with in curly braces ({…..}).If you want to execute only one statement, opening and closing braces are not required.

It executes the condition first. If the condition return true then the statements following the if are executed. If the given condition is false then it skips the statements following the if and executes the statements which are written after the if
  
WAP to input a number and convert it into absolute value.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num;
      clrscr();
      printf("enter a Number");
      scanf("%i",&Num);

      if(Num<0)
      {
            Num=Num * -1;
      }
     printf("The absolute value is%i", Num);
     getch();
}

ii).If …Else statement
It is an advance form of simple if statement. This statement will be used when we want to perform one operation when the given condition is true and another operation when the given condition is false. The general form of the if else statement is

if (condition)
{
    Statement 








            
    . . . . . .
   Statement N
}
else
{
   Statement 1           
   . . . . . .
   Statement N
}

When the condition return boolean true it execute the statements following the if statement. These set of statements are called true-block.

When the condition return boolean false it execute the statements following the if else statement. These set of statements are called false-block.

After the execution of true of false block control is transferred out of if. . .else statement. Note that the ‘C’ compiler executes either true-block or false-block, but not both at the same time.

WAP to input a number from the user and check it is negative or positive.
#include<stdio.h>
#include<conio.h>
void main()
{     int Num;
      clrscr();
      printf("Enter a Number :");
      scanf("%i",&Num);


      if(Num>0)
      {
printf("It is positive Number ");
}
      else
      {
            printf("It is Negative Number");
      }
      getch();
}



WAP to input a number from the user and check it is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num1;
clrscr();
printf("enter a number");
scanf("%i",&Num1);
if(Num1%2==0)
{
printf("%i is even",Num1)'
}
else
{
printf("%i is odd",Num1);
}
getch();
}

Write a program to input two numbers and display largest number.
#include<stdio.h>
#include<conio.h>
void main()
{
      int N1,N2;
      clrscr();
      printf("Enter two Number :");
      scanf("%i%i",&N1,&N2);

      if(N1>N2)
      {
            printf("%i is largest then %i",N1,N2);
      }
      else
      {
            printf("%i is largest then %i",N2,N1);
      }
      getch();
}



Write a program to input a year from the user and check it is leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
{
      int Y;
      clrscr();

      printf("Enter a Year :");
      scanf ("%i",&Y);

      if(Y%4==0)
      {
            printf("It's a leap year");
      }
      else
      {
            printf("It's not aleap year");
      }
      getch();
}




Write a program to input a character from the user and change its case.
#include<stdio.h>
#include<conio.h>
void main()
{
      char ch;
      clrscr();

      printf("Enter a character :");
      scanf("%c",&ch);

      if(ch>=97 && ch<=122)
      {
            ch=ch-32;
      }
      else
      {
            ch=ch+32;
      }
      printf("After case change :%c",ch);
      getch();
}



WAP to input a value and check it whether it is alphabet or not
#include<stdio.h>
#include<conio.h>
void main()
{
      char v;
      clrscr();
     
      printf("enter a value");
      scanf("%c",&v);

      if(v>='a'&&v<='z')
            printf("alphabed");
      else
            printf("not alphabed");
     
      getch();
}

WAP to input a value and check it whether it is decimal number or not
#include<stdio.h>
#include<conio.h>
void main()
{
      char v;
      clrscr();
      printf("Enter a Value");
      scanf("%c",&v);
      if(v>=48&&v<=57)
      {
            printf("Decimal Number");
      }

      else
      {
            printf("Not a decimal number");
      }
getch();
   }




Write a program to enter quantity and price and offer 15 % discount if quantity is more then 1000.
#include<stdio.h>
#include<conio.h>
void main()
{
      float p, amt,dis,pay;
      int qty;
   
      clrscr();
      printf("enter qty and price");
      scanf("%i%f",&qty,&p);
     
      amt=qty*p;
      if(amt>100)
      {
            dis=amt*.15;
      }
      else
      {    
            dis=0;
      }

      pay=amt-dis;
      printf("Total payable amount is %f",pay);
      getch();
}



Write a program to calculate gross salary according to the following conditions
Case 1: - basic < Rs. 1500, then HRA=10% of basic salary and DA=90% of basic salary.
Case 2:- basic >= Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary.
#include<stdio.h>
#include<conio.h>
void main()
{
      float bs, gs, da, hra;
      clrscr();

      printf ("Enter basic salary:");

      scanf ("%f", &bs);

      if (bs<1500)
      {
            hra=bs*10/100;
            da=bs*90/100;
      }
      else
      {
            hra=500;
            da=bs*98/100;
      }

      gs=bs + hra + da;

      printf ("gross salary = Rs. %f", gs);
      getch();
}






iii). If Else if statement
In if-else statement when the given condition is false, the compiler automatically execute the else part. If we want to execute else part based on a condition then we must use the if. . .else if statement. The general form of if …else if  statement is

if (condition)
{
        Statement 1              
        . . . . .
        Statement N
}
else if(condition)
{
       Statement 1           
       . . . . . .
       Statement N
}

. . . . . . . . .
. . . . . . . . .
else
{
         Statement 1           
         . . . . . .
         Statement N
}

If the given condition if true it execute the set of statements following the if statement but if the given condition is false, the control is transferred to the else if statement. Compiler again checks the condition here.

If the given condition is true, compiler execute the statements following the else if statement but if the condition is false compiler transfer the control next else if statement. This process is continue until compiler encounter else if statement.

When all the condition is false, compiler execute the set of statements following the else statement.
















WAP to input a number and check it whether it positive, negative or zero.
#include<stdio.h>
#include<conio.h>
void main()
{
      int Num;
      clrscr();
      printf("Enter a number");
      scanf("%i",&Num);
     
if(num>0)
      {
            printf("Positive Number");
      }
     else if(Num<0)
      {
            printf("Negative Number");
      }
      else
      {
             printf("Zero");
      }
getch();
}




iv). Nested if
When a if statement contain another if statement. Such form of if statement is called nested if. The if statement which contains another if statement is called parent/outer if. The if statement which is inside another if statement is called child/Inner if statement.

If the parent if condition is true, the control is transfer to the child if. After execution of child if/child else part, the control is transferred to the outside of parent if.

If the parent If condition is false, the control is transferred to the parent else. After execution of parent else statements, control is transferred outside the parent if-else.

The general form of nested if
if (condition)
{
      if (condition)
     {
                  Statement 1  
      }
    else
     {
                 Statement 1
     }
}
else
{
             Statement 1 

             Statement N
}
Statement X



Comments