Ternary Operator or Conditional Operator ?: in C++ (C++ में टर्नरी ऑपरेटर)

The Ternary Operator is a special operator available in C and C++. It is also known as the Conditional Operator. It is a shorthand way of writing a simple if-else statement. The ternary operator is represented by ?:. First, the condition is evaluated. If the condition is true, the expression after ? is selected. If the condition is false, the expression after : is selected.

Ternary Operator C और C++ में उपलब्ध एक विशेष ऑपरेटर है। इसे Conditional Operator भी कहा जाता है। इसका प्रयोग simple if-else स्टेटमेंट को संक्षिप्त रूप में लिखने के लिए किया जाता है। Ternary Operator को ?: से दर्शाया जाता है। इसमें सबसे पहले condition check की जाती है। यदि condition सत्य होती है, तो ? के बाद वाला expression select होता है। यदि condition असत्य होती है, तो : के बाद वाला expression select होता है।

Syntax (सिंटैक्स)

(condition) ? expression_if_true : expression_if_false;

Example (उदाहरण)

int x = 10, y = 20;
(x < y) ? cout << y << " is greater"
: cout << x << " is greater";

Output:

20 is greater

Here, x < y is true, so the expression after ? is executed.

यहाँ x < y condition सत्य है, इसलिए ? के बाद वाला expression execute होता है।


Example 1: Check Whether a Number is Even or Odd

Write a C++ program to find whether a given number is even or odd using the ternary operator.

एक C++ प्रोग्राम लिखिए जो Ternary Operator की सहायता से दी गई संख्या के even या odd होने का पता करे।

Program:

#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter number = ";
cin >> num;
(num % 2 == 0)
? cout << num << " is Even Number" << endl
: cout << num << " is Odd Number" << endl;
return 0;
}

Output 1:

Enter number = 8
8 is Even Number

Output 2:

Enter number = 7
7 is Odd Number

Explanation (व्याख्या)

The condition:

num % 2 == 0

checks whether the remainder after dividing the number by 2 is zero.

  • If true, the number is even.

  • If false, the number is odd.

यह condition check करती है कि संख्या को 2 से divide करने पर remainder zero है या नहीं।

  • यदि condition true है, तो संख्या even होती है।

  • यदि condition false है, तो संख्या odd होती है।


Example 2: Find the Greatest Among Three Numbers

Write a C++ program to find the greatest among three numbers using the ternary operator.

एक C++ प्रोग्राम लिखिए जो Ternary Operator की सहायता से तीन संख्याओं में से सबसे बड़ी संख्या ज्ञात करे।

Program:

#include <iostream>
using namespace std;
int main()
{
float a, b, c, greatest;
cout << "Enter Three Numbers: ";
cin >> a >> b >> c;
greatest = (a > b)
? ((a > c) ? a : c)
: ((b > c) ? b : c);
cout << greatest << " is greatest" << endl;
return 0;
}

Output 1:

Enter Three Numbers: 10 25 15
25 is greatest

Output 2:

Enter Three Numbers: 50 20 80
80 is greatest

Explanation (व्याख्या)

First, the program compares a and b.

  • If a > b, it compares a with c.

  • Otherwise, it compares b with c.

  • The greatest value is stored in the variable greatest.

सबसे पहले प्रोग्राम a और b की तुलना करता है।

  • यदि a > b है, तो a और c की तुलना होती है।

  • अन्यथा b और c की तुलना होती है।

  • सबसे बड़ी वैल्यू greatest वेरिएबल में store की जाती है।

The ternary operator is useful for simple conditional expressions. For complex conditions or multiple statements, an if-else statement is generally easier to read and understand. The ternary operator ?: is a conditional operator in C++. It evaluates a condition and selects one of two expressions based on whether the condition is true or false.

Ternary Operator simple conditional expressions के लिए उपयोगी है। Complex conditions या multiple statements के लिए if-else स्टेटमेंट अधिक सरल और readable होता है। C++ में Ternary Operator ?: एक conditional operator है। यह condition को evaluate करता है तथा condition के true या false होने के आधार पर दो expressions में से एक को select करता है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Complete C++ Programming Syllabus - From Basics to Advanced C++ , including OOP and Data Structures and Algorithms

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)