Saturday, 23 August 2014

Conditional statements

                C is the language where program is executed sequentially from the first line to last line. But sometime we need to choose some statements and to ignore some statement based upon the condition.
Therefore C provides us the solution for this problem: Decision Control Statement

Decision control statement:
11)      Conditional statements:  if, if-else, if-else-if, switch statement
22)      Unconditional statements:  goto, break, continue

Conditional statements: Whenever you want to execute a statement for a particular condition.
                                                Example: 1) You can travel in the airplane only if you have Passport.
                                                                   2) You are eligible to drive car if you are 18+  (in India).

if statement:   if(test expression)  
                                {
                                  Statements to be executed when condition is true;   /* this block will execute only when the condition in the if statement returns 1 */

                                }
                                Another statement;

Example: int age;
                printf(“ \n enter your age :”);
                scanf(“ %d “, &age);
                if(age >=18)
                {
                printf(“ you are eligible to drive “);
                }
getch();
return 0;
}


if-else statement:  find smallest of two numbers.
                                #include<conio.h>
                                #include<stdio.h>
                                int main()
                                {
                                int a,b;
                                printf(“enter the value of a and b “);
                                scanf(“ %d %d “, &a,&b);
                                if(a<b)
                                                { printf(“ \n the smallest number is %d”,a); }
                                else
                                                { printf(“\n the smallest number is %d”,b); }
                                getch();
                                return 0;
                                }

if-else-if  statement: #include<conio.h>
                                  #include<stdio.h>
                                int main()
{
                                int x,y;
                                printf(“ \n Enter two numbers “);
                                scanf(“%d %d”,&x,&y);
                                if( x==y)
                                                { printf(“ The two numbers are equal”); }
                                else if(x>y)
                                                { printf(“%d is greater than %d”, x,y); }
                                else
                                                { printf(“ %d is less than %d”, x,y); }

                                return 0;
                                }

Switch case Statement:
A switch case statement is a multi-way decision statement that is a simplified version of if-else block that evaluate only one variable.

Generalized Switch statement
Generalized if-else statement
Switch(a)
{
case 1: // do this
case 2: // do this
case 3: // do this
default: // do this

}

If(exp1){
   // do this
}
else if (exp2){
   // do this
}
else if(exp3){
   // do this
}


Switch statements are mostly used in two situations:
1)       When there is only one variable to evaluate in the expression.
2)       When many conditions are being tested for.
Note: After each case we must have to use “ break; “ statement which tells the compiler to jump out from the switch block.
Now let us have an example: program to check vowels.
                #include<conio.h>
                #include<stdio.h>
                int main()
                {
                char c;
                printf(“\nEnter any character “);
                scanf(“%c”,&c);
                switch(c)
                {
                case ‘a’ :
                                printf(“ \n %c is vowel”, a);
                                break;
                case ‘e’ :
                                printf(“ \n %c is vowel”, a);
                                break;
                case ‘i’ :
                                printf(“ \n %c is vowel”, a);
                                break;
                case ‘o’ :
                                printf(“ \n %c is vowel”, a);
                                break;
                case ‘u’ :
                                printf(“ \n %c is vowel”, a);
                                break;
                default :
                printf(“ \n %c is not a vowel “,a);
}
                getch();
                return 0;
                }

Note: what if more than one case has the same expression to be evaluated.
                In the above example   ‘e’ and ‘E’ both are vowel. So,
                case ‘e’ : case ‘E’ :
                                printf(“ \n %c is a vowel” , a);


Advantages of using Switch Case statement
11)       Easy to debug.
22)       Easy to read and understand.
33)       Easy of maintenance as compared with its if-else statements.
44)       Like if-else switch statement can also be nested.
55)       Executes faster than its equivalent if-else statement.




like us on facebook and get updated Programming Infinitum

No comments:

Post a Comment