Saturday, 22 December 2018

Conditional control statement


Conditional control statement:
                                                 The conditional control statement is based on conditional operator, that which executes fast in a single line. It takes the combination of “ ? and : “ for using the conditional statement. The syntax is:
                                                 exp1?exp2:exp3;
                        This is a substitute of “if-else” statement that which uses a single condition. Similarly, the nesting conditional control statement syntax is:
                                        exp1?(exp2?exp3:exp4):exp5;
                  In the above nested conditional control statement ,first exp1 will be executed and if it is true, then exp2 will be checked. If exp2 is true then the exp3 will be executed, otherwise exp4 will be executed. But if exp1 is false, then only the exp5 will be executed. This is the substitute of nested-if statement.
Ex:/*program to calculate salary depending basic pay*/
      #include<stdio.h>
      #include<conio.h>
       void main()
        {
                        int sal,bp;
                        clrscr();
                        printf(“Enter basic pay:”);
                        scanf(“%d”,&bp);
                        sal=(bp!=1000)?((bp<1000)?bp+100:4*bp+200):2000;
                        printf(“\n sal=%d”,sal);
                        getch();
         }

No comments:

Post a Comment