<p>The difference is obvious. The ternary operator <em>will</em> return one of the two alternative values it is given; that's what it is for. An <code>if</code> condition that doesn't have an <code>else</code> block won't do anything if the condition is false.</p>
<p>The ternary operator version <em>will set</em> <code>color</code> to <code>axesColor</code> or <code>background</code>. But by that point in your code, you've gone past 3 other conditions that may have changed the value of <code>color</code> from its initial value of <code>background</code>. But the ternary operator doesn't care. It will <em>overwrite</em> whatever is in <code>color</code> with either <code>axesColor</code> or <code>background</code>.</p>
<p>What you probably wanted was <code>color = abs(coord.y) &lt; 0.005 ? axesColor : color;</code>, so that the value will be preserved if the condition is false.</p>

CC BY-SA 4.0
2020-11-09T03:24:04.903Z
Doesn't compilers tend to compute both branches of the ternary and just discard when needed? From what I have read smart enough compilers should do the same (when not very performance intensive) with branches.
Felipe Gutierrez
CC BY-SA 4.0
2020-11-09T03:37:02.403Z
@FelipeGutierrez: That has nothing to do with the issue here. That's a matter of optimization. The OP's issue is that the `if` version specifically does something different from the `?:` version. Because they're *supposed to* do different things; the statements aren't equivalent.
Nicol Bolas
CC BY-SA 4.0
2020-11-10T03:07:48.263Z
Yep I am aware, just adding to the knowledge base I guess. That's why I didn't add it as an answer.
Felipe Gutierrez
CC BY-SA 4.0
2020-11-08T15:00:21.450Z
2020-11-08T15:00:21.450Z
Nicol Bolas
<p>I'm practicing GLSL on ShaderToy</p>
<pre><code>void mainImage( out vec4 fragColor, in vec2 fragCoord ) {

vec2 coord = fragCoord.xy/iResolution.xy;

vec3 background = vec3(1.);

vec3 axesColor = vec3(1.,0.,0.);
vec3 gridColor = vec3(0.85);

vec3 color = background;

const float tickWidth = 0.1;
for(float i = 0.0 ; i &lt; 1.0 ; i += tickWidth)
{
    if(abs(coord.x - i) &lt; 0.002) color = gridColor;
    if(abs(coord.y - i) &lt; 0.002) color = gridColor;
}

if(abs(coord.x) &lt; 0.005) color = axesColor;
**if(abs(coord.y) &lt; 0.005) color = axesColor;**
**//color = abs(coord.y) &lt; 0.005 ? axesColor : background;**

fragColor = vec4(color,1.0);  }
</code></pre>
<p>my question is on last 3 lines.</p>
<p>First, I used ternary operator for branch, but it made weird result that only drawing &quot;x axes&quot;.</p>
<p>However, when I changed the operator to if, it works well. The code drew x, y axes and all grids as I wanted.</p>
<p>So I wonder is there different between if and ternary operation in GLSL?
If that, how can I choose correctly?</p>

CC BY-SA 4.0
2022-08-05T00:24:28.213Z
for the folks that came here expecting the technical explanation, exclusive to shader execution difference between an if statement and an equivalent ternary operator, see this precise and polite explanation: [https://itecnote.com/tecnote/r-shader-optimization-is-a-ternary-operator-equivalent-to-branching/](https://web.archive.org/web/20220918050755/https://itecnote.com/tecnote/r-shader-optimization-is-a-ternary-operator-equivalent-to-branching/)
Drout
<opengl><glsl>
Is there different between 'if' and 'ternary operator' in GLSL?
Oh Seo
# Is there different between 'if' and 'ternary operator' in GLSL?

(Asked by: username_0 on 2020-11-08)

I'm practicing GLSL on ShaderToy

```
void mainImage( out vec4 fragColor, in vec2 fragCoord ) {

vec2 coord = fragCoord.xy/iResolution.xy;

vec3 background = vec3(1.);

vec3 axesColor = vec3(1.,0.,0.);
vec3 gridColor = vec3(0.85);

vec3 color = background;

const float tickWidth = 0.1;
for(float i = 0.0 ; i < 1.0 ; i += tickWidth)
{
    if(abs(coord.x - i) < 0.002) color = gridColor;
    if(abs(coord.y - i) < 0.002) color = gridColor;
}

if(abs(coord.x) < 0.005) color = axesColor;
**if(abs(coord.y) < 0.005) color = axesColor;**
**//color = abs(coord.y) < 0.005 ? axesColor : background;**

fragColor = vec4(color,1.0);  }

```

my question is on last 3 lines.

First, I used ternary operator for branch, but it made weird result that only drawing "x axes".

However, when I changed the operator to if, it works well. The code drew x, y axes and all grids as I wanted.

So I wonder is there different between if and ternary operation in GLSL?
If that, how can I choose correctly?

**Tags:** opengl, glsl

> **Comments:**
> **username_1**: for the folks that came here expecting the technical explanation, exclusive to shader execution difference between an if statement and an equivalent ternary operator, see this precise and polite explanation: [https://itecnote.com/tecnote/r-shader-optimization-is-a-ternary-operator-equivalent-to-branching/](https://web.archive.org/web/20220918050755/https://itecnote.com/tecnote/r-shader-optimization-is-a-ternary-operator-equivalent-to-branching/)  

---

## 1 answers

### Answer by username_2 on 2020-11-08. Score: 1

The difference is obvious. The ternary operator *will* return one of the two alternative values it is given; that's what it is for. An `if` condition that doesn't have an `else` block won't do anything if the condition is false.

The ternary operator version *will set* `color` to `axesColor` or `background`. But by that point in your code, you've gone past 3 other conditions that may have changed the value of `color` from its initial value of `background`. But the ternary operator doesn't care. It will *overwrite* whatever is in `color` with either `axesColor` or `background`.

What you probably wanted was `color = abs(coord.y) < 0.005 ? axesColor : color;`, so that the value will be preserved if the condition is false.

> **Comments:**
> **username_3**: Doesn't compilers tend to compute both branches of the ternary and just discard when needed? From what I have read smart enough compilers should do the same (when not very performance intensive) with branches.  
> **username_2**: @username_3: That has nothing to do with the issue here. That's a matter of optimization. The OP's issue is that the `if` version specifically does something different from the `?:` version. Because they're *supposed to* do different things; the statements aren't equivalent.  
> **username_3**: Yep I am aware, just adding to the knowledge base I guess. That's why I didn't add it as an answer.  

---

