I passed the test, but I don't seem to understand what this means and under what topic it is. Please break it down for me to understand i.e. what the question mark and the colon mean etc:
int m2 =a < b ? a : b;
m2 = m2 < c ? m2 : c;
m2 = m2 < d ? m2 : d;
m2 = m2 < e ? m2 : e;
return m2;
hidden #10424566
Level 7
HELP!! Passed the test but i don't get the method i used.
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
hidden #10424566
11 February 2019, 18:04
Thank you very understandable
+1
Guadalupe Gagnon
11 February 2019, 18:05
No problem! Good luck with your continued learning
0
Guadalupe Gagnon
11 February 2019, 17:49solution
This is the ternary operator. Its usage is:
<boolean expression> ? <result if true> : <result if false>
It is the same exact as typing this:
if(<booleanexpression>){
<result if true>
} else {
<result if false>
}
Stepping through the code, this is what it is doing:
A great use of the ternary operator is adding an 's' to pluralize nouns in a sentence. Take this bit of code for example:
if the user enters 1, the output would be:
You have completed 1 task
and if the user enters 2 or higher, the output would be:
You have completed 2 tasks <- notice the 's' +13
Leopold Bschorr
26 February 2020, 17:02
you're the best !
+1