Conditional Operator

Conditional Operator
Conditional Operator को Ternary operator भी कहते है। इसमें दो symbols ? और होते है . 
Conditional Operator को Use करने का सामान्य तरीका

Syntax 

expression1 ? expression2 : expression3

If the expression1 evaluates to a non zero value (i.e. true) then expression2 is solved otherwise expression3 is evaluated. Consider an example –
x % y = = 0 ? x++ : y++ ;
Here, if x is divisible by y then x will be incremented otherwise y will be incremented. The true use of this operator is discussed in next chapter – Selection 

Example 
*/ Program to check whether the number is equal to 10 or not */

          #include<stdio.h>

         #include<conio.h>

         int main(void)

        {
            int number;
            clrscr();
           printf("Enter a number");
           scanf("%d",&number);
           number ==10? printf("Right"): printf("Wrong");
           getch();
           return 0;
        }

Output :
Enter a number 10
Right

दोबारा रन कराने पर


Enter a number 20

Wrong






NEXT   : Miscellaneous
PREVIOUS :  Bitwise Operators








1 comment: