-4

What would be the literal translation for the following code. Also, please explain what it meaning and the different ways of using this operator.

int i = 1, j = 2; 
 cout << ( i > j ? i : j ) << " is greater." << endl; 

Any other useful or additional information about the ternary operator that will help me understand it better would be much appreciated.

Wulfinite
  • 209
  • 1
  • 3
  • 9
  • This is homework? I am sure that it is – Ed Heal Oct 18 '15 at 15:08
  • It does seem like it but it's not. If you still don't believe me, could you please just explain how it works? And the different ways of using it? – Wulfinite Oct 18 '15 at 15:10
  • 2
    If you don't have a [decent book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) yet then it's probably time to consider getting one - this sort of basic stuff is covered in the first few chapters of any good introductory book. – Paul R Oct 18 '15 at 15:10
  • Google ternray operator - will that do (PS: I do not believe you as it is a Sunday and this is a basic question) – Ed Heal Oct 18 '15 at 15:12
  • Haha, you sure you don't know this? Because googling it will show million examples of it. – anurag86 Oct 18 '15 at 15:14
  • 1
    searching on SO might help too: http://stackoverflow.com/questions/14763054/c-ternary-operator-operand-evaluation-rules/14763151#14763151 – jev Oct 18 '15 at 15:14
  • Okay people. This is not homework, I'm just learning C++ by myself because it's fun. My school doesn't even offer anything to do with programming. I just read somewhere that this is the best place to ask questions. – Wulfinite Oct 18 '15 at 15:15
  • @Wulfinite - Searching the internet/reading the book is quicker. Besides these questions do turn on Sunday and sort of get people thinking they have to turn in their homework for tomorrow. – Ed Heal Oct 18 '15 at 15:20
  • @Wulfinite where did you read that? Also, if you like reading stuff, try clickign on the 'Help' menu item above and reading the site policy etc. – Martin James Oct 18 '15 at 15:20
  • Any book you people would recommend for beginners? – Wulfinite Oct 18 '15 at 15:23
  • 1
    @EdHeal yes, but if you can con those naive slaves on SO to write you a unique answer, it will not match the net copypasta submissions from the other students. – Martin James Oct 18 '15 at 15:23
  • I already posted a link for you for a definitive C++ book list in the comments above. See: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Paul R Oct 18 '15 at 15:24
  • @MartinJames - Exactly my point. – Ed Heal Oct 18 '15 at 15:24
  • lol 'book you people would recommend' is specifically off-topic for SO, as you would have known if you had read the policy/roles/guides before posting. – Martin James Oct 18 '15 at 15:25

1 Answers1

1
( i > j ? i : j )

basically means:

if (i > j) is true return i, else return j


Side note: This is very similar to MS Excel's if-formula

user3437460
  • 17,253
  • 15
  • 58
  • 106