In this article, we will talk about ternary and logical operators used in the C language. If you are a GATE aspirant looking for CSE notes, then you are at the right place. Read ahead to learn more.
Ternary Operator in C
We use the ternary operator to execute a code that is based on a binary condition’s result. The ternary operator in C takes in a binary condition in the form of input. It makes it very similar to the ‘if-else’ control flow block. However, it also returns a value that behaves very similar to a function.
Benefits of ternary operator:
- The ternary operator is basically the class of conditional operator that will serve the very basic function, and in simple words, provide the programmers with ease of making different kinds of decisions.
- It will perform the same function as that of the if/else condition, but it will change the overall outlook and length of the available code.
- The ternary operator will make the code that will be very easy to understand.It will look much more professional as well as algorithmic in the whole process. In this way, there will be no chance of any kind of difficulty to the concerned users throughout the process.
- The decision-making will become very much easy in this particular case because ternary operator will provide people with easy-to-use systems to avoid any kind of chaos.
So, the ternary operator’s main advantage is that it will improve the application’s performance by reducing the number of code lines.
Also Read: Who can apply for the BPSC CDPO exam?
Here is a syntax of the ternary operator in C
result = binaryCondition ?valueReturnedIfTrue : valueReturnedIfFalse;
Note- We cannot use the ternary to execute code. So, in this case, it must either be returned in the given function or set equal to the variable with a similar data type as that of the returned values.
Here is an example of the ternary operator in C
//Looking at the maximum example
int findMaximum(int x, int y){
//if x > y, it returns x, if not it returns y
return (x > b) ? x : y;
}
Logical Operators in C
The logical operators constitute to be a part of binary operators. The logical operators are used specifically when we combine multiple requirements together. Then these can be utilised in numerous relational and conditional expressions. So, when we evaluate these conditions, these are the Boolean expressions that give an output of either 1/0 for True/False respectively. Let us learn below about different logical operators used in the C programming language.
Logical Operators in C – The three primary logical operators are ‘!’, ‘||’ and ‘&&’. We can understand the truth tables by:
p q p && q p || q
1 0 0 1
1 1 1 1
0 1 0 1
0 0 0 0
And for NOT operator:
a !a
1 0
0 1
The output 0’ and ‘1’ ‘denotes the False and True. Through these, those conditional operations that we perform can be very well understood.
Want to learn more or logical operators? Visit here at logical operators in C or check more CSE notes. Are you a GATE aspirant? So, learn more about every topic in detail and prepare for GATE 2023! All the best for your exams. Ciao!