---
title: "Trouble with ternary tester"  
description: "Trouble with ternary tester"  
author: "Anonymous User"  
published: 2015-04-28  
updated: 2015-04-28  
canonical: https://www.mindstick.com/forum/23153/trouble-with-ternary-tester  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Trouble with ternary tester

Could someone please help me make sense of this [ternary operator](https://www.mindstick.com/forum/12944/ternary-operator-in-razor-using-a-model). I checked the [web](https://www.mindstick.com/articles/12783/the-ultimate-bunch-of-free-web-design-resources) and couldn't find any examples to clear it up for me. All the examples I saw included only one [boolean](https://www.mindstick.com/forum/160332/how-does-the-boolean-function-work) [expression](https://www.mindstick.com/articles/1861/sqlite-expressions) followed by expression1 : expression 2. \
[int](https://www.mindstick.com/forum/159137/how-to-convert-date-int-to-date-in-sql) x = 5; System.out.println(x > 2 ? x < 4 ? 10 : 8 :7);}}\
It looks like it is ( booleanExpression ? booleanExpression ? not sure here : expression1 : expression2 ) \

## Replies

### Reply by Anonymous User

the ternary syntax is: \
\

```
booleanExpression ? value if true : value if false; 
```

\
\
which is shorthand for an if statement \
\

```
if (booleanExpression){  value = valueIfTrue;} else {  value = valueIfFalse;}
```

\
\
In this example it looks like their "value if true" expression is itself a ternary expression. I'll add some brackets to make it obvious how it is evaluating: \

```
x > 2 ?  (x < 4 ? 10 : 8) :7
```

\
\
i.e. the equivalent of a nested if/else statement \
\

```
int answer;if (x > 2){  if (x<4) {    answer = 10;  } else {    answer = 8;  }} else {  answer = 7;}
```

\
\
Of course if I ever saw anybody actually WRITING code like this, I would probably (not so) politely suggest that they rewrite their code to make it more readable\


---

Original Source: https://www.mindstick.com/forum/23153/trouble-with-ternary-tester

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
