Conditional Operator

Conditional Operator combines the Condition, True execution and False Execution in a single line. It has 3 parts.




PartsWhat They Do ..What it Contains
Condition Checks for a certain condition.Contains the condition (Part before '?')
True ExecutionExecutes the true condition statements.Contains the True execution (Between '?' and ':')
False ExecutionExecutes the false condition statements.Contains the False execution (After ':')




Syntax :

[Condition check]?[Statement(s) for true execution]:[Statement(s) for false execution]



C Example 1


#include<stdio.h>

int main() {

	int a,b,c,d,e,f,g,h;

	a=10;

	b=11;

	c=~a;

	// Bitwise NOT

	d=b<<2;

	// Bitwise LEFT SHIFT

	e=a>>2;

	// Bitwise RIGHT SHIFT

	f=a&b;

	// Bitwise AND

	g=a|b;

	// Bitwise OR

	h=a^b;

	// Bitwise XOR

	printf("a=%d,b= %d",a,b);

	printf("c=%d,d= %d,e=%d,f= %d,g=%d,h=%d,",c,d,e,f,g,h);

	return 0;

}