Anyone who could explain me what this "cout << (n<m?n:m);" does | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Anyone who could explain me what this "cout << (n<m?n:m);" does

the main question is about "?" and ":".

17th Dec 2016, 8:59 AM
Catalin Dervesteanu
Catalin Dervesteanu - avatar
5 Answers
+ 6
its terniary operator to use in place of if/else
17th Dec 2016, 12:22 PM
Bruh
+ 5
this is short hand of if thn els (condin)?(true code):(falsecode) (m>n)?cout<<"greater":cout<<"small"; if(m>n) codes here tru; else codes here fals;
17th Dec 2016, 9:13 AM
Sandeep Chatterjee
+ 2
analising the content of parenthesis (...) the first condition is a if statement, after the char "?" are the answers separated by ":", in the order: if true the first and if false the second. cout will print the answer.
17th Dec 2016, 9:09 AM
Michael Isac Girardi
Michael Isac Girardi - avatar
+ 2
If n<m evaluates to true, then n will be outputted to cout, else m. This is your classic ternary statement - the "?" specified the condition to be evaluated and the ":" separates the result if true (left side) from if false (right side). It is shorthand for if(..).. else ..
17th Dec 2016, 9:21 AM
Ettienne Gilbert
Ettienne Gilbert - avatar
+ 2
thanks guys
17th Dec 2016, 11:12 AM
Catalin Dervesteanu
Catalin Dervesteanu - avatar