×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

A simple example of Ternary Operator in C++ (check whether a year if Leap or not)

Example of Ternary/Conditional Operator in C++: using Example to check Leap year, we will learn what Ternary operator is, how it is works in C/C++?

What is Ternary Operator in C++?

It is called Ternary Operator because it has 3 operands to operate a statement. It is also known as Conditional Operator or ? : (Questions Mark and Colon) Operator.

The three operands are:

  1. Operand1: Condition_part (Here, we write a conditional statement to validate).
  2. Operand2: True_part (Here, we write statement to execute if conditional part is true).
  3. Operand3: False_part (Here, we write statement to execute if conditional part is false).

Syntax ot Ternary Operator

Operand1? Operand2: Operand3;

In this program, we are using Ternary (Conditional) operator to check whether a Year is Leap Year or not.

Example of Ternary Operator

Given a year and we have to check whether it is Leap year or not using Ternary Operator/Conditional Operator.

Consider the program:

/*
C++ program to check whether entered
year is Leap year or not
*/

#include <iostream>
using namespace std;

int main() {
  int year;

  // input a year
  cout << "\nEnter year: ";
  cin >> year;

  // using TERNARY OPERATOR/CONDITIONAL OPR/?: OPR
  ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0))
      ? cout << year << " is Leap year." << endl
      : cout << year << " is not a Leap year." << endl;

  return 0;
}

Output

First run:
Enter year: 2000
2000 is Leap year.

Second run:
Enter year: 2017
2017 is not a Leap year.

Third run:
Enter year: 1900
1900 is not a Leap year.

Fourth run:
Enter year: 2016
2016 is Leap year.

Program using goto to run program until input year is not 0

/*
C++ program to check whether entered
year is Leap year or not
*/

#include <iostream>
using namespace std;

int main() {
  int year;

// Label to run again program from here
INIT:
  // input a year
  cout << "\nEnter year (ENTER 0 for exit...): ";
  cin >> year;

  // using TERNARY OPERATOR/CONDITIONAL OPR/?: OPR
  ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0))
      ? cout << year << " is Leap year." << endl
      : cout << year << " is not a Leap year." << endl;

  //////////////
  // condition to go to label INIT or end of the program
  if (year != 0) goto INIT;
  //////////////
  return 0;
}

Output

Enter year (ENTER 0 for exit...): 2000
2000 is Leap year.

Enter year (ENTER 0 for exit...): 2017
2017 is not a Leap year.

Enter year (ENTER 0 for exit...): 1900
1900 is not a Leap year.

Enter year (ENTER 0 for exit...): 2016
2016 is Leap year.

Enter year (ENTER 0 for exit...): 0 
0 is Leap year.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.