I have read several articles and visited several websites trying to grasp the concept of the conditional operator but I cant seem to grasp it, can someone explain it to me? How it functions?
// Conditional operator
#include <iostream>
int main()
{
int i = 7;
int j = 5;
std::cout << ( i > j ? i : j ) << " is greater than " << ( i > j ? j : i ) << '\n';
return 0;
}
This will 'cout' the highest number, then the lowest.
Note how I have switched the second conditional statement at the end.
From i : j to j : i
( i > j ? i : j )
i > j - If i is greater than j i else j
If the first part is true, use the left side, else, use the right side.
I have read several articles and visited several websites
After all that, I reckon the best way to learn is to stop reading about it, and start trying it out for yourself. Get a compiler, write a few lines of code and see what happens.
Ohhhhh!!!! I got it! :) So whatever comes AFTER the colon separator is the "else"? So like if the condition is true, print the first number, if the condition is false, print the number after the colon?