+ 9
How do we calculate this Exp. ? ↓↓
#include <stdio.h> #include<stdlib.h> int main (){ int exp=1?0:2?3:4; printf("%d",exp); return 0; } //output:0
17 odpowiedzi
+ 3
Mik_RDC
Ternary operator syntax:
exp = expression ? True : False;
So
1 ? 0 : 2 ? 3 : 4
Is like
if (1)
exp = 0
else if (2)
exp = 3
else
exp = 4
+ 6
Mik_RDC
1 ? 0 : 2 ? 3 : 4 means
1 ? 0 : (2 ? 3 : 4)
= 0 as 1 means true
+ 5
A͢J HungryTradie William Owens AnnaiD Hacker-KR4636 Danny Wanyoike Igor Kostrikin Aly Alsayed
first of all, what is a test because I don't see a loop or a function, we just assign it to a variable?
you don't think it should first test two numbers or true before being assigned like
(1&&0)1:0;
+ 3
Your line is defining an int variable called exp with the result of your ternary (second ternary).
You could say:
int exp;
if (1==TRUE){
exp = 0;
}
else if (2 ==TRUE){
exp = 3;
}
else{
exp = 4;
}
As I said before, I'm learning... But I think this is correct.
+ 3
Hungry Tradie let's see if this explanation helps more:
The ternary operator is used to execute code based on the result of a binary condition.
It takes in a binary condition as input, which makes it similar to an 'if-else' control flow block. It also, however, returns a value, behaving similar to a function.
result = binaryCondition ? valueReturnedIfTrue : valueReturnedIfFalse;
So in the example it was
if (1){
exp = 0;
} else if (2){
exp = 3;
} else {
exp = 4;
}
Remember any non-zero integer evaluates to true. So the binary test
if ( true == 1) {
exp = 0;
} else if (true == 2) {
exp = 3;
} else {
exp = 4;
}
In ternary the result must match the return type.
+ 3
Mik_RDC the program outputs 0 because expression:
1?0:2?3:4
Uses ternary operator: condition + "?" + first value + ":" + second value
But in a compound form...
The important part here and the part which defines the result is the way that expression was written (There's no parentheses but is not needed) The inner condicional operator is evalueted first, because that value is needed for outer ternary operator.
This means that.... 1?0:2?3:4 is evalueted in the following way...
First, separe expressions
1?0:x --> X is the other value that we don't know yet. But we do know that the second value is the result of expresion 2?3:4
2? 3:4
2 --> is a value greater than 0, so is True, this means the value returned is the first value "3" (if true --> first value, else --> second one)
So expression now is 1?0:3
1 is a value grether than 0, so is true, it means the output is 0 (if true --> first value, else --> second one).
+ 3
Mik_RDC
like A͢J explained, it's just shortcut if-else.
The line:
int exp =
means we are going to assign a value to the variable named exp.
then:
1?0:2
means if 1 is true, exp = 0. If 1 is false, exp = 2.
But 1 is true, so exp=0, and the rest is not evaluated and we just print the value of exp, which gives us the output 0.
Try changing the expression to:
int exp = 0?0:2?3:4
because 0? is false,
this becomes:
exp = 2?3:4
because 2 is true,
it will output 3.
and try:
int exp = 0?1:0?3:4
because 0? is false,
this becomes
0?3:4
because 0 is false,
it will output 4.
+ 2
I catch nothing bro can you plz good explain me as it output 0?
What result of (2?3:4) ???
+ 2
I'm not up to speed with ternary yet, but I think it is
if (1) 0
else{
if (2) 3
else 4
}
+ 2
Think of it like AJ stated:
If(true) then 0 else if 2 then 3 else 4.
This is because any non-zero integer evaluates to true so
If 1 written as '1 ?'
Then 0 written as '? 0 :'
If the 0 and 1 we're swapped then exp == 3
Ternary
X ? A : B
If(X){
A;
} else {
B;
}
+ 2
#include <iostream>
#include<string>
using namespace std;
int main() {
int num;
double height;
string name;
cout<<"enter an integer: ";
cin>>num;
cout<<"\n";
cout<<"enter the first name";
cin>>name;
cout<<"\n";
cout<<"enter the height";
cin>>height;
cout<<"\n";
cout<<"name:"<<name<<"\n";
cout<<"height:"<<height<<"\n";
return 0;
}
How can I make my input visible in output display!?
+ 2
HungryTradie I'm starting to understand a little with the last exemple but can you try to combine the ternary and your second
Or a simple eg
+ 2
thank you all for enlightening me, you are really strong
+ 1
If (exponent==1)
//set exponent to 0
exponent = 0;
else
set exponent to 2
xeponent = 2;
// Now the value contains in
//variable exponent could be
// 0 OR 2.
//;Enter new if statement or
// next if-then-else
// statement.
If (exponent==0)
//set exponent to 3
exponent = 3;
else
//set exponent to 4
exponent = 4;
+ 1
Oliver you are a bit off on your assessment, reread some of the post.
+ 1
Not really, William Owens. I explain the real application of this problem. This is common in value assignment or set or retain the value of certain variable, or quantity. It is like adjusment mechanism. typical control system'.
- 1
My name is junior