Friday, July 29, 2016

C Basics - Conditional Operator Statement in C

The conditional operators ? and : are sometimes called ternary operators since they take three arguments. Their general form is,

expression 1 ? expression 2 : expression 3

if expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3.


int x, y ; 
scanf ( "%d", &x ) ; 
y = ( x > 5 ? 3 : 4 ) ; 

This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.



Related topics:
Overview of Statements in C   |   Decision Making Statements in C   |   The if Statement in C   |   The if-else Statement in C   |   The else-if Statement in C   |   Nested if-else Statement in C   |   Forms of if Statement in C   |   Switch Statement in C   |   The null Statement in C

List of topics: C Programming

No comments:

Post a Comment