Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Ternary Operator

In this tutorial, we will learn about C++ ternary operator and how to use it to control the flow of program with the help of examples.


Ternary Operator

Ternary operator are a substitute for if…else statement. So before you move any further in this tutorial, go through C++ if…else statement (if you haven’t).

The ternary operator allow us to execute different code depending on the value of a condition, the result of the expression is the result of the executed code.

The conditional operator is kind of similar to the C++ if…else statement as it does follow the same algorithm as of C++ if…else statement but the conditional operator takes less space and helps to write the if-else statement in the shortest way possible.

The Ternary operator returns one of two values depending on the result of an expression.


Syntax of Ternary Operator

The syntax of the Ternary Operator is:

Condition ? Expression1 : Expression2;

The ternary operator works as follows:

  • If the expression stated by Condition is true, the result of Expression1 is returned by the ternary operator.
  • If it is false, the result of Expression2 is returned.

For example, we can replace the following code

if (number % 2 == 0)
{
	Even = true;
}
else
{
	Even = false;
}

with

Even = (number % 2 == 0) ? true : false ;

Why is it called ternary operator?

This operator takes 3 operand, hence called ternary operator.


Example 1: C++ Program to find the even or odd number using Ternary Operator

// Lets understand the ternary operator to find the even or odd number

#include <iostream>
using namespace std;
int main(){

    int n;
    cout << "Enter an integer: ";
    cin >> n;
    (n % 2 == 0) ? cout << n << " is even." :  cout << n << " is odd.";

    return 0;
}

When we run the program, the output will be:

Enter an integer: 20
20 is even.

In the above program, User entered the value 20 is assigned to a variable n. Then, the ternary operator is used to check if number is even or not.

Since, 20 is even, the expression (number % 2 == 0) returns is even. We can also use ternary operator to return numbers, strings and characters.


Example 2: C++ Program to Find maximum number

// Program to find maximum number using ternary operator

#include <iostream>
using namespace std;
int main() {

    int a = 10;
    int b = 30;
    int max = a > b ? a : b;
    cout << "Maximum Value: " << max << "\n";

    return 0;
}

When we run the program, the output will be:

Maximum Value: 30

 

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!