Advertisement

Programmer Coding

Conditional Operator in C Language

What is Conditional Operator?

This operator is used to evaluate a specific condition that ultimately affects the choice of two Boolean values ​​or expressions. The result of all tests is true or false. It is unique in that it is a ternary operator and has only one operator, which is the correct operator.

Here is a detailed description of their products and uses:

Condition: The situation is evaluated first. If the condition is true, the expression evaluates to 1 and becomes the result of all expressions. If the condition is false, expression 2 is evaluated and the result is .

Expression 1: This is the instruction that should be evaluated if the condition is true.

Advertisement

Expression 2: This is the instruction that should be evaluated if the condition is false.

Syntax

condition ? expression1 : expression2;

The first operand (instruction here) is implicitly cast to type bool (true or false).

Statement 2 (third operand) is evaluated when the first operand evaluates to false. the correct operator.

Example

#include <stdio.h>

Advertisement

int main() {

int x = 10;

int result = (x > 5) ? 100 : 200;

printf(“Result: %d\n”, result);  // Output will be 100, since x > 5 is true

return 0;

}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top