+ 6

Please anyone explain the concept of ternary operators?

In C++

26th Dec 2018, 8:50 AM
Saad Saleem
Saad Saleem - avatar
17 Réponses
+ 11
Parenthesis make it readable and eye-pleasing! a = 5 > 4 ? ( 3 > 8 ? 40 : 20 ) : 30; or if (5 > 4) if (3 > 8) 40 else 20 else 30
26th Dec 2018, 9:33 AM
Babak
Babak - avatar
+ 3
Advantage of Ternary Operator -: Using ?: reduce the number of line codes and improve the performance of application. Syntax expression-1 ? expression-2 : expression-3 In the above symbol expression-1 is condition and expression-2 and expression-3 will be either value or variable or statement or any mathematical expression. If condition will be true expression-2 will be execute otherwise expression-3 will be executed.
26th Dec 2018, 9:06 AM
Saad Saleem
Saad Saleem - avatar
+ 3
Yes bro i.like the question. Firstly 5>4 condition becomes true it will be 40 and if 3>8 it will be 40 if at all 3<8 ut will be 20 and 4<5 it will be 30
26th Dec 2018, 10:35 AM
ch nani
+ 2
But how this work Int a; a=5>4?3>8?40:20:30; What is the output?
26th Dec 2018, 9:07 AM
Saad Saleem
Saad Saleem - avatar
+ 2
5 is greater than 4 result is 40. Next step is execute 3>8 3 is less than 8 and and result is 20.
26th Dec 2018, 9:15 AM
Izaak GOLDSTEIN
Izaak GOLDSTEIN - avatar
+ 2
" But what is the input to get 30 as answer in this program? " An example to make the first condition gets evaluated as false might be 4 > 5 ? (...) : 30
26th Dec 2018, 10:14 AM
Babak
Babak - avatar
+ 2
it is short hand operator of if else statement used in expression
26th Dec 2018, 12:52 PM
shahbaj singh
shahbaj singh - avatar
+ 2
Ternary operator is like a if else Ex c=X<y?X:y in the statement if X<y then X is assigned in C otherwise y is assigned in c
27th Dec 2018, 3:09 AM
Abdul Rahman Khan
Abdul Rahman Khan - avatar
+ 1
Okkk bro
26th Dec 2018, 10:43 AM
ch nani
+ 1
A ternary operation assign value to variable based on condition . Let me explain by example- If boolean isValid = x>y? true:false 1. It will check if x is greater than y . Operation 1 2. If operation1 comes to true then true got assigned to isValid variable. 3.if operation comes to false then false value got assigned to isValid variable.
27th Dec 2018, 6:46 AM
Narender Sharma
Narender Sharma - avatar
0
But what is the input to get 30 as answer in this program?
26th Dec 2018, 10:00 AM
Saad Saleem
Saad Saleem - avatar
0
Oke
26th Dec 2018, 10:16 AM
Saad Saleem
Saad Saleem - avatar
0
Thanks i understand it now
26th Dec 2018, 10:41 AM
Saad Saleem
Saad Saleem - avatar
0
Yes i know
26th Dec 2018, 12:53 PM
Saad Saleem
Saad Saleem - avatar
0
Guys I already understand it check upper comments and thanks
27th Dec 2018, 7:03 AM
Saad Saleem
Saad Saleem - avatar
0
In Java Boolean bool= (10 >2) ? true : false; If the (10 > 2 ) is true then the bool have true otherwise false
1st Jan 2019, 10:52 AM
Santhosh Kumar
Santhosh Kumar - avatar