0% found this document useful (0 votes)
82 views

Conditional Operators

Conditional operators (?:) in C programming execute different statements based on whether a test condition is true or false, using the syntax "conditional_expression?expression1:expression2", where expression1 is returned if true and expression2 if false. For example, a program takes user input for whether a year is a leap year or not, and uses a conditional operator to set the number of days in February to either 29 or 28 depending on the input.

Uploaded by

slspa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Conditional Operators

Conditional operators (?:) in C programming execute different statements based on whether a test condition is true or false, using the syntax "conditional_expression?expression1:expression2", where expression1 is returned if true and expression2 if false. For example, a program takes user input for whether a year is a leap year or not, and uses a conditional operator to set the number of days in February to either 29 or 28 depending on the input.

Uploaded by

slspa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Conditional operators (?

:)
Conditional operators are used in decision making in C programming, i.e, executes different statements according
to test condition whether it is either true or false.

Syntax of conditional operators


conditional_expression?expression1:expression2

If the test condition is true, expression1 is returned and if false expression2 is returned.

Example of conditional operator


#include <stdio.h>
int main(){
char feb;
int days;
printf("Enter l if the year is leap year otherwise enter 0: ");
scanf("%c",&feb);
days=(feb=='l')?29:28;
/*If test condition (feb=='l') is true, days will be equal to 29. */
/*If test condition (feb=='l') is false, days will be equal to 28. */
printf("Number of days in February = %d",days);
return 0;
}

Output
Enter l if the year is leap year otherwise enter n: l
Number of days in February = 29

Other operators such as &(reference operator), *(dereference operator) and ->(member selection) operator will be
discussed in pointer chapter.

You might also like