Question

1
2
cout << (n % 2 == 0) ? -n : n;// how this line of code is working i am not 
understanding


1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;
	cout << (n % 2 == 0) ? -n : n; // can something plz explain 
	system("pause");
}
It's a short form of:
1
2
3
4
if (n % 2 == 0)
  cout << -n;
else
  cout n;


http://www.cplusplus.com/articles/1AUq5Di1/
a ? b : c means if a then b otherwise c.

The condition n % 2 == 0 checks if n is even so the meaning of cout << (n % 2 == 0) ? -n : n; is that if n is even -n will be printed, otherwise n will be printed.

1
2
3
4
5
6
7
8
if (n % 2 == 0)
{
	cout << -n;
}
else
{
	cout << n;
}
@peter87 and @Thomas1965 thanks for the responce

first i also thought this like if and else but output is not same

if user input 4

in ternary operator output is 1

but in if and else output is -4

what is this
Last edited on
It's because << has higher precedence than the ternary operator, so it is evaluated like this:
 
(cout << (n % 2 == 0)) ? -n : n;

For that reason you need to put parentheses around the ternary operator.
 
cout << ((n % 2 == 0) ? -n : n);
Last edited on
I think it hat to do with the priority of the operators. 1 is the result of (n % 2 == 0) which is true for 4.
If you try with
cout << ( (n % 2 == 0) ? -n : n); output will be -4.
> first i also thought this like if and else but output is not same

Tip: in general, and particularly while learning the language,
compile the code with warnings enabled; ideally test the code with more than one compiler.

(Linux: compile with both g++ and clang++.
Visual Studio: also compile the the code with the 'Clang With Microsoft CodeGen' front end).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>

int main()
{
    int n;
    std::cin >> n;

    std::cout << ( n % 2 == 0 ) ? -n : n ; // can something plz explain
    // clang++:warning: operator '?:' has lower precedence than '<<'; '<<' will be evaluated first
    // clang++:         std::cout << ( n % 2 == 0 ) ? -n : n ; // can something plz explain
    // clang++:         ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
    // clang++:   note: place parentheses around the '<<' expression to silence this warning
    // clang++:   note: place parentheses around the '?:' expression to evaluate it first
    // clang++          std::cout << ( n % 2 == 0 ) ? -n : n ; // can something plz explain
    // clang++:                                     ^
    // clang++:                      (                      )

    // g++: warning: second operand of conditional expression has no effect
    // g++:          std::cout << ( n % 2 == 0 ) ? -n : n ; // can something plz explain
    // g++:                                        ^~
    // g++: warning: third operand of conditional expression has no effect
    // g++:          std::cout << ( n % 2 == 0 ) ? -n : n ; // can something plz explain
    // g++:                                               ^

    std::cout << ( n % 2 == 0 ? -n : n ) ; // this is probably what the programmer intended
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/535b1c63bd13e5f1
Topic archived. No new replies allowed.