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;

# HELP!! Passed the test but i don't get the method i used.

Resolved

Comments (3)

- Popular
- New
- Old

You must be signed in to leave a comment

Gax
Level 7, Nairobi

11 February, 18:04

Thank you very understandable

+1

Guadalupe Gagnon
Level 20, Tampa

11 February, 18:05

No problem! Good luck with your continued learning

0

Guadalupe Gagnon
Level 20, Tampa

11 February, 17:49
Solution

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:

```
// this lines declares a variable 'm2' and sets it to (a is less than b) ?
//'a' if that is true : 'b' if that is false
int m2 =a < b ? a : b; //'m2' now holds the lessor of 'a' and 'b'
// this line now checks if 'm2' is less than 'c' and sets it to 'm2' if that is true or
// 'c' if that is false
m2 = m2 < c ? m2 : c; // 'm2' now holds the lessor of 'm2' and 'c'
// checks if 'm2' is less than 'd' and sets 'm2' to the lower of the '2'
m2 = m2 < d ? m2 : d;
// checks if 'm2' is less than 'e' and sets 'm2' to the lower of the 2
m2 = m2 < e ? m2 : e;
// 'm2' will now hold the lowest value of 'a', 'b', 'c', 'd', 'e'
return m2;
```

A great use of the ternary operator is adding an 's' to pluralize nouns in a sentence. Take this bit of code for example:
```
// code to declare a reader
int num = Integer.parseInt(reader.readline());
System.out.println("You have completed " + num + " task" + (num > 1 ? "s" : ""));
```

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'
+3