Conditional Operator in C - Ishwaranand

News

Conditional Operator in C

Condition operators

Q) Give syntax for the conditional operator.
Q)Give syntax for the conditional operator and explain with flow chart.
Q)Give syntax for the conditional operator and explain with an example.

conditional operator in c, logical operators, ternary operator, conditional operator in python, conditional operator java, conditional operator javascript, conditional operator c, ternary operator exercises javascript, javascript ternary multiple statements, ternary operator react, ternary operator without else, javascript ternary operator for sorting, javascript if statement multiple conditions, conditional operator java, javascript question mark dot, if statement java, javascript logical operators, if then for the expression, special operators in c, c++ ternary operator return, which is compound assignment operator?, conditional statement in c++, c++,
Image Source ~ Crafted With ©Ishwaranand - 2020 ~ Image by ©Ishwaranand
  • The C language has an unusual operator, useful for making a two-way decision. This operator is a combination of ? and : and takes three operands. That operator is commonly known as the conditional operator. It is also known as a ternary operator as it has three operators. It has the following general form.
  • Conditional-expression ? expression1 : expression2
  • Here, conditional-expression, expression1 and expression2 are the expressions.

Working of Condition operators:

  • Conditional- expression is evaluated first. If it is true (i.e. non-zero), then the expression1 will be evaluated and return as the result otherwise expression2 will be evaluated and return as the result. Note that only one of the expression (either expression 1 or expression2) is evaluated.
consider the following piece of a code:
a=10
b=15:
x=(a>b) ? a:b;
Here, x will contain a value of b (i.e. 15)
  • Note: conditional operator,  ternary operator and operator having three operands are the same things.

Q) Write any program to demonstrate the working of the conditional operator.
Q) Write a program to find a larger number from two numbers using a conditional operator.


/* program for finding largest out of two numbers, using conditional operator */ 

#include<stdio.h>
#include<conio.h>

void main()
int a, b, 1;
clrscr();
printf("Enter two numbers : ")
scant("%d%d", &a, &b);

1= (a>b) ? a:b;
 printf("\nLargest number is %d", 1);
getch();
}

Output:
Enter two numbers: 10 12
Largest number is 15

No comments

Comment on Ishwaranand or Happy Day
Not, Add any types of links