2/07/2016

if else statement

if else statement

It is used to execute an instruction or sequence / block of instruction only if condition is fulfilled. if statement, expression is evaluated first and then, depending on whether the value of the expression (relation or condition) is trueor false, transfers the control to the particular statement or group of statements.
Different forms of implementation of if-statement are :
  • Simple if statement
  • if-else statement
  • Ladder if-else statement
  • Nested if-else statement
Simple if statement
This is used to executed the statements only if the condition is true.
Syntax :
if(condition)
    {
        block of statement;
    }


If..else statement is used when different block of statement is to be executed on condition true and false.
Syntax :
if(condition)
    {
        block 1 of statement;
    }
    else
    {
        block 2 of statement;
    }
if condition is true then block 1 statement will be executed and if condition is falseblock 2 statement will execute.





#include<stdio.h>               #include<conio.h> 
void main() 
{ 
int a;
printf("Enter a number");
scanf("%d",&a); 
  if(a%2==0) 
  {
   printf("Even number"); 
  } 
  else 
     { 
      printf("Odd number"); 
      } 
 getch();
}
Output:
Enter a number15
Odd number

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
 printf("Enter the value of year:");
 scanf("%d",&year);
  if(year%4==0)
  {
   printf("Entered year is leap year");
  }
else
  {
   printf("Entered year is not leap year");
   }
getch();
}
Output:
Enter the value of year:2004
Entered year is leap year



#include<stdio.h>
#include<conio.h>
void main();
{
int a,b;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
if(a>b)
{
printf("%d is greater than %d",a,b);
}
else
{
printf("%d is greater than %d"b,a);
}
getch();
}
Output:
Enter two numbers:12 6
12 is greater than 6
Other output:
Enter two numbers:12 16
16 is greater than 12


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("Enter the no.");
scanf("%d",&a);
if(a%8==0)
{
 printf("No. is divisible by 8); }
if(a%6==0)
{
printf("No. is divisible by 6"); }
else
{
printf("\nNo is not divisible by 6);
}
getch();
}
Output:
Enter the no.16
No. is divisible by 8 No. is not divisible by 6
Other output:
Enter the no.24
No is divisible by 8No. is divisible by 6
Other output:
Enter the no.15
No. is not divisible by 6
Note:-Here, after first if statement no else statement comes but after second if statement else statement comes.Carefully see outputs.



You can combine multiple if / if-else / if-else-if ladders when a series of decisions are involved. So you can make sure that your program executes certain instructions when a series of conditions are met.
Some use case :
1.  
    if(condition)
    {
        //block of statement

        if(condition)
        {
            //block of statement
        }
    }
    
2.

    if(condition)
    {
        //black of statement
    }
    else
    {

        if(condition)
        {
            //block of statement
        }
        else
        {
            //block of statement
        }

    }
    
3.  
    if(condition)
    {
        //black of statement
    }
    else if(condition)
    {

        if(condition)
        {
            //block of statement
        }
        else
        {
            //block of statement
        }

    }



Ladder if- else statement



#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
 printf("Enter marks:");
 scanf("%d",&marks);
  if(marks>360)   
  {    
    printf("FIRST DIVISION");     }
  else if(marks>240)
  {
   printf("SECOND DIVISION");
  }
  else if(marks>180)
  {
   printf("THIRD DIVISION");
  }
    else
    {
     printf("FAIL");
    }
getch();
}
Output:
Enter marks:300 SECOND DIVISION
Other output: Enter marks:400 FIRST DIVISION


Note:-
If condition  'if(a>360)' satisfied then no other codition will be tested.When codition 'if(a>369) is no stisfied then next condition will be tested and show on.
#include<stdio.h>
#include<conio.h>
void main();
{
int a;
printf("Enter the no.:");
scanf("%d",&a);
if(a%8==0)
{
printf("The number is divisible by 8 and 4");
}
else if(a%4==0)
{
printf("The number is not divisible by 8 but divisible by 4");
}
else
{
printf("The no. is not divisible by both 8 and 4");
}
getch();
}
Output:
Enter the no.:16
The number is divisible by 8 and 4





Nested If Else

#include<stdio.h>
#include<conio.h>
void main()
{
  int a,b,c;
  printf("Enter three integer values :\n");
  scanf("%d%d%d",&a,&b,&c);
  if(a>b)
{
   if(a>c)
  {
   printf("Largest value is %d",a);
  }
   else
  {
   printf("Largest value is %d",c);
  }
}
else
{
  if(b>c)
  {
   printf("Largest value is %d",b);
  }
  else
  {
   printf("Largest value is %d",c);
  }
}
getch();
}
Output:
Enter three integer values :
12 45 67
Largest value is 67




No comments:

Post a Comment