-->

6. C-If-Else-Statement

C-Operators: Previous                                                             Next: C-Loop Constructs                      

(b)     C-If Else Statement: - In if statement we have only one clause i.e. ‘if’, now in if else statement we have two clauses i.e. ‘if’ and ‘else’.
It further divided into following types:
  1. Simple if else
  2. Nested if else
iii.      if else if ladder
  1. Simple if else: - statements which are written just after if clause called True part and statement written after else clause called False part.
 Let’s take real life example, So that  you would be able to understand  it better.
 Suppose a customer says to shopkeeper that bring 1 kg apple, if you have otherwise bring pomegranate. It means in  case of satisfaction of condition or dissatisfaction n of condition  some statements will execute.
If we have an ‘else’ part then ‘if’ part must be defined first.
Syntax:
if(condition)
{
True Part;
}
else
{
False Part;
}

E.g.:              Write a program to find out the Maximum number between two numbers.
Solution:
                                #include<stdio.h>
void main( )
                                                                {
                                int num1,num2;
                                printf(“Enter 2 numbers : ”);
                                scanf(“%d %d”, &num1, &num2);
                                if( num1 > num2 )
                                {
printf(“Maximum number is %d ”, num1);
                                }
else
                                {
printf(“Maximum number is %d ”, num2);
                                }
}             
  1. Nested if else: - when we have nesting of if else blocks that is one after the another if else block then it is called nested if else.  In previously described  nested if, we  had only if’s  condition  true part, No false part were there.

If the condition1 will get true then condition 2 will get checked, if both are true then true part2 will get executed. But in case of false of condition2 the false part2 executed and in case of false of condition1 the last else block false part1 is executed.



E.g.: Nested if-else example program in c programming:
#include<stdio.h>
void main( )
                                                                {
                                int num;
                                printf(“Enter Number : ”);
                                scanf(“%d”, &num);
                                if( num % 2 == 0)
                                {
                                                if(num == 20)
                                                {
printf(“20 is Even”);
                                                                                                }
else
{
printf(“Number  is Even but not 20”);
}
                                }
else
                                {
printf(“Number is Odd”);
                                }
            
iii.         if else if ladder: - it is used when we have 2 or more choices and have to execute only one based on given condition. Or we can say that when we have a logical OR between given conditions or every condition using a same variable to compare then this statement is used.
Let’s take real life example sup pose a customer says to shopkeeper that  bring  apple, if  you don’t have apple then bring banana, if you don’t   have then  bring  pineapple. If you don t have either of them then  bring  orange.  But please!! Don t bring  all of t hem
Syntax:
if(condition1)
{
True Part;
}
else if(condition2)
{
True Part;
}
else if(condition3)
{
True Part;
}
else
{
False Part;
}

E.g.:         Write a C program to display a division of student on basis of their total obtained marks in 3 subjects out of 300.
Solution:
#include<stdio.h>
void main( )
                                                                {
                                int maths, sci, eng;
                                float per;
                                printf(“Enter Marks of Maths : ”);
                                scanf(“%d”, &maths);
                                printf(“Enter Marks of Science : ”);
                                scanf(“%d”, &sci);
                                printf(“Enter Marks of English : ”);
                                scanf(“%d”, &eng);
                               
                                per = (maths + sci + eng) / 3;
                                if( per >= 60)
                                {
                                                printf(“First Division”);
                                                                                }
                                else if( per >= 48)
                                {
                                                printf(“Second Division”);
                                                                                }
                                else if( per >= 36)
                                {
                                                printf(“Third Division”);
                                                                                }
else
                                {
printf(“Fail”);
                                }
}

  1. Conditional operator: -

This is another type of operator which is used to replace if else block. It works same as if else statement works but only difference in their look.
Syntax:    (condition) ? True part : False part;
E.g.:         write a program to print largest number using conditional operator.
Solution:
                #include<stdio.h>
#include<conio.h>
void main( )
{
                                int num1,num2;
                                clrscr( );
                                printf(“Enter 2 Numbers : ”);
                                scanf(“%d %d”, &num1, &num2);
                                (num1 > num2) ? printf(“Number 1 is MAX”) : printf(“Number 2 is MAX”);
}

Advantages of using conditional operator instead of if else block are:
  • Makes program simple and short.
  • User does not have to be worry about if else braces.

  1. Assignment operator: -

As name implies assignment operators are used to assign a value in a variable. It is binary operator. It is single operator which has a Left hand side as well as Right hand side part as a part an operator.
Working as follows:
First, that expression at RHS will be evaluated and finally it get convert into single value and assign to LHS part. This operator is used in math term also but it works there in reverse order. Value which is evaluated is assigned to RHS instead of LHS.

E.g.:        10+20+30 = 60 (wrong)
Now the syntax LHS = RHS
E.g.:        sum = a+b+c;
Rules need to be follow:
  • We cannot write the expression at LHS.
  • We cannot write the Literals at LHS.
  • Only variables can be used at LHS of the assignment.
Two types of assignment operator :
  1. Simple assignment
  2. Compound assignment
In compound assignment operators we combine arithmetic operator or logical or relational operator  with assignment operator(=).

+=Compound addition
-=Compound sub
*=Compound multiply
&=Compound AND
E.g. int num =5;
num =num +7;
num+=7;
6.Increment/Decrement:-
                The symbol for this operator is ++ or -- . they both are unary operator and only works on numeric data type. This operator is used to increase or decrease value of variable by 1. It contains inbuilt assignment operator. They are either used pre increment/decrement or post increment/decrement.  There is no difference between pre or post until they are  not part of an expression.
E.g.
  #include<stdio.h>
                                 void main()
                                {
                                                int num=20;
                                                num++; printf(“the number is %d”, num );
                                                num-- ;
                                                printf(“the number is %d”, num);
}

7.Special operator:

                Following are some special operator supported by c:
  1. Sizeof operator
  2. &(address  of)
  3. *(value at)
  4. .(member of)
(A)    sizeof() operator :- it is unary operator and used to tell the  bytes occupied by a specified variable or literal.
Syntax :- size of(variable/literal);
E.g. suppose int num=25;
sizeof( num);
this will return 2 bytes, because num is of int type and since, ‘int’ data types store 2 bytes in memory.
More e.g. sizeof(float);
sizeof(25);
(B)     (&) address of :- this is unary operator and takes variable as value and returns address of specified variable. The format specifier for address is %u because address are always unassigned.
e.g. int num=28;  its address is 200-2002. Now write it as follows to get the address of variable named ‘num’.
printf(“the address is %u”, &num);
(C)     (*) valueat:- it is unary operator and takes a base address of memory location and returns value store d at that particular location.
e.g. int num=28;  and address is 2003-2005.
*(&num);
This will return 28.
Note :- the operators member of is defined in later chapter “structures”.

C-Operators: Previous                                                             Next: C-Loop Constructs