Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

Short Hand If

Short Hand If…Else (Ternary Operator)

Additionally, there exists a shorthand for if…else statements, referred to as the ternary operator due to its three operands. It’s employed to condense multiple lines of code into a single line and is commonly used to simplify straightforward if…else statements.

Syntax

variable = (condition) ? expressionTrue : expressionFalse;

Instead of composing:

Example

int time = 20;
if (time < 18) {
  printf(“Good day.”);
} else {
  printf(“Good evening.”);

You can express it simply as:

Example

int time = 20;
(time < 18) ? printf(“Good day.”) : printf(“Good evening.”);

The decision to employ either the traditional if…else statement or the ternary operator is entirely yours to make.