Ternary or Conditional Operator in C/C++.

Ternary or Conditional Operator in C/C++.

ternary or conditional operator in C or C++

Ternary also called as conditional operator is substitute to if else statement. It is the shortest method to reduce the if else structure. When we have to check only one or two conditions, if else structure increases the length of program. In such cases ternary operator helps us reduce the length and get the operation done as fast as possible. In this post we will see ternary or conditional operator in C or C++

We will first see the syntax for ternary operator.

Syntax -
(condition)?(true? then perform statement1):(else perform statement2);

? - the statement after "?" gets executed if mentioned condition is true.
: - if the specified condition is false then statement after ":" is executed.

Now we will see an example to get more clear idea of the ternary or conditional operator.

Example – Write a program to find greatest number entered by the user using ternary (conditional) operator.

1. Code

C program

//compare two nos in C using ternary operator
#include <stdio.h>

int main()
{
    int a,b;
    printf("\n Enter 2 nos:");
    scanf("%d%d",&a,&b);

    //using ternary operator
    (a > b)?printf("\n First no is greater"):printf("\n Second no is greater");

    return 0;

}

C++ program

//compare two nos in C++
#include <iostream>

using namespace std;

int main()
{
    int a,b;
    cout<<"\n Enter 2 nos:";
    cin>>a>>b;

    //using ternary operator
    (a > b)?cout<<"\n First no is greater":cout<<"\n Second no is greater";

    return 0;

}

2. Output

Ternary or conditional operator in C C++
Ternary or conditional operator in C C++

3. Program Parts

  1. #include is a pre-processor directive. It allows us to include header files.
  2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
  3. .h is an extension for header file.
  4. int is data type. It indicates that the program returns a value.
  5. main() is main function that indicates the compiler that the user written programs starts from here.
  6. a,b are variable names.
  7. print/cout is the keyword use to print a message.
  8. Scanf/cin is the keyword use to store values in variables.
  9. ? will check the specified condition. If it is true it will perform specified instructions
  10. : is used for false part of the condition.
  11. return statement is use to return a value. If any error occurs the program will return 0.

4. Explanation

At first compiler will check header files. If required header files are present then the compiler will enter the main function. First, two variables are declared a and b of integer data type. The compiler then asks user to enter two numbers. The two numbers are stored in a and b respectively. Now compiler will encounter ternary statement. Here it will check the specified condition i.e. a is greater than b. If the condition is true then “First number is greater is printed”. Else compiler will print “Second number is greater”. And finally program execution will terminate.

Conclusion

So this was the example ternary or conditional operator in C or C++. There are many other possible ways to find greatest number. If you know any of them do tell us in the comment section below. Hope you liked it. If yes then please do share it with your friends and programming buddies. Also tell us what other programs you would like us to post.

For more such content follow us on instagram. Visit again!

ErrorFreeProgram.

2 COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *