conditional ternary operator

Write your question here.
i want to evaluate 7== 5+1 using conditional operator..... but i don't know how to show this in the code cout<<...


here is my code....
// conditional operator
#include <iostream>
using namespace std;

int main ()
{
int c;
c = 7;

c == 5+1 ? 4:3 ;

cout << c << '\n';
}
Ternary syntax:
expression ? if_true : if_false

Example, printing true/false strings:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main(int argc, const char* argv[])
{
    auto constexpr valueToCompareTo = 7;
  
    std::cout << (5 + 1 == valueToCompareTo ? "true" : "false") << std::endl;
    std::cout << (5 + 2 == valueToCompareTo ? "true" : "false") << std::endl;
    std::cout << (3 + 4 == valueToCompareTo ? "true" : "false") << std::endl;
    std::cout << (9 + 1 == valueToCompareTo ? "true" : "false") << std::endl;
    std::cout << (10 - 3 == valueToCompareTo ? "true" : "false") << std::endl;
    return 0;
}
cout << (c == 5+1 ? 4 : 3) << '\n';
Note that you must put the ?: expression in parentheses. That's because << has higher precedence than ?: and == Without the parentheses, the compiler would evaluate the expression like as if it were written like this:
((cout << c) == 5+1) ? 4 : (3 << '\n');

Last edited on
thanks :):)
Topic archived. No new replies allowed.