Friday 10 October 2014

Control Statement If....else















Control Statement

In C program Statement are executed sequentially in order in which they appear in the program.But sometime we may want to use a condition for executing only a part of program. Also many situations arise where we may want to execute some statement several times. Control statements enable us to specify the order in which the various instructions in the program are to be executed.This determines the flow of control. Control statements define how the control is transferred to other part of program.
C language supports four types of control statement,


  1. if......else
  2. goto
  3. switch
  4. loop

If......else

This is bi-directional conditional control statement. This statement is used to test a condition and take one two possible actions. if the condition is true then a single statement or block of statements is executed (one part of the program), otherwise another single statement or block of statement is executed (other part of program).

Syntax of if-

if(condition)
{
statement;
.............
.............
}



Flow chart if if. statement..





here if the condition is true then statement is executed and if it false then the next statement which is immediately after the control statement is executed.


Syntax of if...... else 


if(condition)
{
statement;
............
}
else
{
statement;
.............
}
in this syntax , if condition is true then first block is execute, otherwise second block executed.

Flow Chart of if...... else Control Statement 




Here if the condition is true then statement 1 is executed and if it is false then statement 2 is executed. After this the control transfers to the next statement which is immediately after the if...else control statement.

// Program to print a message if negative number is entered.

#include<stdio.h>
main()
{
int num;
printf("Enter a number");
scanf("%d",&num);
if(num<0)
printf("number entered is negative");
printf("Value of num is%d,num);
}

Output

Enter a number :-6
numaber entered is negative
value of num is = -6

// program to print the larger and smaller of the two number

#include<stdio.h>
main()
{
int a,b;
printf("Enter the first number");
scan("%d",&a);
printf("Enter the Second number");
scan("%d",&b);
if(a>b)
printf("larger number =%d and smaller number=%d,a,b);
else
printf("larger number =%d and smaller number=%d",b,a);
}

Output

Enter the first number: 9
Enter the Second number :11
larger number=11 and smaller number =9


Nesting of if ..... else

When we include another if.... else in the portion of if block or else block(as we need). this is called nesting of if.... else statements. we can include only if portion or else portion separately as we need in the program, not need to write if... else both portion simultaneously.  

Syntax:-  

                                     if (Condition 1)
                        {
                               if (Condition 2)
                                     Statement;
                               else 
                                     Statement;
                         }

                         else
                         {
                                 if (Condition 3)
                                      Statement;
                                                   else
                                       Statement;
                          }


// Program to find largest number from given number.

#include<stdio.h>
main()
{
     int a,b,c,large;
     printf("Enter three number:");
     scanf("%d%d%d",&a, &b, &c);
     
     if( a>b)
     {
          if(a>c)
               large=a;
           else
               large=c;
     }

    else
    {
          if (b>c)
                 large=b;
          else
                 large=c;
    }
}                                   // end of main




While nesting if....else statements, sometime confusion may arise in associating else part with appropriate in if part. example-

if (grade=='A')
{
     if(marks>95)
      print("excellent");
}

else
      printf("work hard for result");

if we write the above code without braces

if (grade=='A')

     if(marks>95)
      print("excellent");


else
      printf("work hard for result");


here the else part is matched with the second if. but we wanted to match it with the first if. The compiler does not associate if and else parts according to the indentations, it matches the the else part with closest unmatched if part. so whenever there is double regarding matching of if and else parts can use braces to enclose each if and else blocks.


// Program to find whether a year is leap or not.

#include<stdio.h>
main( )
{
int year;
printf("Enter Year:");
scanf("%d",&year)
if(year%100==0)
{
    if(year%400==0)
          printf("Leap Year\n");
    else
          printf("Not leap");
}

else
{
    if(year%4==0)
         printf("Leap year\n");
    else
         printf("Not leap");
}


else if ladder

This is a type of nesting in which there is an if....else statement in every else part except the last else part.

Syntax:-
                  if( condition 1)
                           statement;
                        else     
                                  
                                 if (condition)
                                     statement;
                                 else
                                            
                                           if(condition)
                                               statement;
                                            else
                                                statement;

we can write this syntax in another form, this is a compact form.

                          if( condition 1)
                                    statement;
                          else  if (condition)
                                     statement;
                          else if(condition)
                                      statement;
                                            
                            else
                                      statement;


Flow chart of else if ladder



Here each condition is checked, and when condition is found to be true, the statement corresponding to that are executed, and the control comes out of the nested structure  without checking remaining conditions. if none of the condition is true then last else part is executed.

//program to find out the grade of a student when the marks of 4 subjects are given. The method of assigning grade is as-
per>=85                                      grade A
per<85 and per>=70                    grade B
per<70 and per>=55                    grade C
per<55 and per>=40                    grade D
per<40                                        grade E

#include<stdio.h>
main()
{
float m1,m2, m3, m4, totle, per;

char grade;

printf("Enter marks of 4 subjects");

scanf("%f%f%f%f",&m1, &m2,&m3,&m4);

totle=m1+m2+m3+m4;

per=totle/4;

if(per>=85)
 grade='A';
          
           elseif(per>=70)
           grade='B';

elseif(per>=55)
grade='C';
          
           elseif(per>=40)
           grade='D';
else
   grade='E';

printf("Percentage is %f\n grade",per,grade);
}



No comments:

Post a Comment