Wednesday - March 13, 2024
#C, C++ and C# #Blog #C Program #Computer Science

Conditional Operator in C

Conditional operator

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

  • 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.
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++,

Working of Condition operators:

  • Conditional- expression is evaluated first. If it is true (i.e. non-zero), then expression 1 will be evaluated and returned as the result otherwise expression2 will be evaluated and returned as the result. Note that only one of the expressions (either expression 1 or expression 2) is evaluated.

consider the following piece of 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 the 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
Conditional Operator in C

What is Escape sequence…?

.