From 7ad3dd4bd979ab5462920733a6d383d7979ce745 Mon Sep 17 00:00:00 2001 From: Muralidharan Sekar <30570051+murali97@users.noreply.github.com> Date: Mon, 19 Nov 2018 07:49:25 +0530 Subject: [PATCH] Add the text "Conditional operators" to the article (#22235) * Add the text "Conditional operators" to the article * fixed numbered list formatting --- guide/english/c/operators/index.md | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/guide/english/c/operators/index.md b/guide/english/c/operators/index.md index 1b977258ba..3f1b46b329 100644 --- a/guide/english/c/operators/index.md +++ b/guide/english/c/operators/index.md @@ -217,3 +217,38 @@ with higher precedence will be evaluated first. - Conditional `?:` - Assignment `= += -= *= /= %= >>= <<= &= ^= |=` - Comma `,` + +## 7. Conditional Operators + +## Syntax + ```conditionalExpression ? expression1 : expression2``` + +The conditional operator works as follows: + +The first expression conditionalExpression is evaluated first. This expression evaluates to 1 if it's true and evaluates to 0 if it's false. +1. If conditionalExpression is true, expression1 is evaluated. +2. If conditionalExpression is false, expression2 is evaluated. + +## Example +```c +#include +int main(){ + char February; + int days; + printf("If this year is leap year, enter 1. If not enter any integer: "); + scanf("%c",&February); + + // If test condition (February == 'l') is true, days equal to 29. + // If test condition (February =='l') is false, days equal to 28. + days = (February == '1') ? 29 : 28; + + printf("Number of days in February = %d",days); + return 0; +} +``` +## Output + +``` +If this year is leap year, enter 1. If not enter any integer: 1 +Number of days in February = 29 +```