Conditional Operator in C
The conditional operator is used to add conditional code in our program. It is similar to the if-else statement. It is also known as the ternary operator as it works on three operands.
Syntax of Conditional Operator
(condition) ? [true_statements] : [false_statements];
Flowchart of Conditional Operator
Example of Conditional Operator
C
// C Program to illustrate the use of conditional operator #include <stdio.h> // driver code int main() { int var; int flag = 0; // using conditional operator to assign the value to var // according to the value of flag var = flag == 0 ? 25 : -25; printf ( "Value of var when flag is 0: %d\n" , var); // changing the value of flag flag = 1; // again assigning the value to var using same statement var = flag == 0 ? 25 : -25; printf ( "Value of var when flag is NOT 0: %d" , var); return 0; } |
Output
Value of var when flag is 0: 25 Value of var when flag is NOT 0: -25
Decision Making in C (if , if..else, Nested if, if-else-if )
The conditional statements (also known as decision control structures) such as if, if else, switch, etc. are used for decision-making purposes in C programs.
They are also known as Decision-Making Statements and are used to evaluate one or more conditions and make the decision whether to execute a set of statements or not. These decision-making statements in programming languages decide the direction of the flow of program execution.