C ternary operator

Complete Guide on C Ternary Operator. Less than 5 easy steps.

Spread this useful information with your friends if you liked.

 

Complete Guide on C Ternary Operator. Less than 5  easy steps.

In this article we are going to have a look at C ternary operator. We will find out with regards to C# ternary operators and how to utilize it to control the progression of a program.

So get ready to understand the wonderful and cheerful concept of C ternary operator.

As it is quite clear by the name Ternary means trio, means three. It takes 3 arguments to get executed and is easy for the programmers for long code. Developers utilize the ternary operators for decision making instead of longer if and else conditional proclamations.

It is an operator that determines or checks the given conditions input by the users. Below is the procedure and specifications of all three arguments:

 

● The first one is a comparison element 

● The second is the result of a true comparison 

● The third is the outcome upon false comparison 

Don’t bother if you didn’t catch it, Here is its Syntax 

 

Syntax:

condition? statement 1: statement 2

The ternary operator begins with a boolean condition. Assuming this condition is valid, it will execute the principal expression after? in any case, the second articulation after: will be executed.
The accompanying model exhibits the ternary operator.


Example: Ternary operator
int x =30, y =40;

var result = x > y ? “x is greater than y” : “x is less than y”;

Console.WriteLine(result);

Output:

x is greater than y.
Over, a conditional articulation x > y returns valid, so the primary explanation after? will be executed.

 

Accordingly, a ternary administrator is a short type of if-else statement. The above model can be re-composed utilizing the if-else condition.

When to use the ternary operator?

Ternary operators can be utilized to supplant multi-lines of code with a single line. However, we shouldn’t abuse it.

For example, we can replace if-else code by using ternary operators as shown below :

if (a > b)
{
           output= "a is greater than b";
}
else if (a < b)
{
           output= "b is greater than a";
}
else
{
           output= "a is equal to b";
}

By using the Ternary operator
(Multiple lines to a single line )

Output= a > b ? “a is greater than b” : a < b ? “b is greater than a” : “a is equal to b”;.

Advantages of Ternary Operator:

● The fundamental benefit of utilizing a ternary operator is to diminish the number of lines of code and work on the exhibition of utilization.

● Utilizing ternary operators is viewed as a more master and expert method of composing codes in contrast with utilizing if-else statements for similar business logic.

● It has over the if-else is the fact that you can use it to initialize variables (even constants and static) 

How about we think about some guide to get actionable information on ternary operators. Additionally, the Ternary Operators in C assess the conditions and executes the code block.

Using Ternary Operator find the largest number:

#include<stdio.h>

  int main() {
    int x,y, max;

   
printf("Enter any two numbers \n");
   
scanf("%d%d", &x, &y);
    /* Following statement replaces the whole if-else statement and makes the code more concise*/    max = (x >y) ? x : y;
   
printf("%d", max);
   
printf("is the largest number of given numbers");

   
return 0;
  }

Output:

Enter any two numbers

45 33

45 is the largest number of given numbers.

Conclusion:

So this was all about the Ternary Operators in C. I hope all your fundamentals are clear by now. However, if you are still left with any doubt or query please let us know in the comments section, we will try to get to you ASAP.


Spread this useful information with your friends if you liked.

Leave a Comment

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