---
title: "Control flow and the ternary in TypeShade, lowered to WGSL"
description: "Conditions, counted loops, while, switch, break, continue, discard and the ternary, which WGSL lacks, each with its WGSL, and the loops refused."
url: https://typeshade.dev/guide/language/from-typescript/control-flow/
language: en
---

# Control flow

A loop has to be one the GPU can finish, and a choice between two values has to be one the target has an operator for.

## Conditions and loops

- ### `if`
  An `if`, as written.
  TypeScript
  ```ts
  export function capped(x: f32): f32 {
    if (x > 1.) {
      return 1.
    }
    return x
  }
  ```
  Emitted WGSL
  ```wgsl
  fn capped(x: f32) -> f32 {
    if ((x > 1.0)) {
      return 1.0;
    }
    return x;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float capped(float x) {
    if ((x > 1.0)) {
      return 1.0;
    }
    return x;
  }
  ```
- ### Counted `for`
  A counted loop: an integer variable, a constant step, and an exit that compares it to a bound the body does not write. [block-scope](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/block-scope/block-scope.shade.ts)
  TypeScript
  ```ts
  export function ramp(x: f32): f32 {
    let acc: f32 = 0.
    for (let i = 0; i < 4; i += 1) {
      acc = acc + x * f32(i)
    }
    return acc
  }
  ```
  Emitted WGSL
  ```wgsl
  fn ramp(x: f32) -> f32 {
    let acc_1 = (x * 0.0);
    let acc_2 = (acc_1 + x);
    let acc_3 = (acc_2 + (x * 2.0));
    let acc_4 = (acc_3 + (x * 3.0));
    return acc_4;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float ramp(float x) {
    float acc_1 = (x * 0.0);
    float acc_2 = (acc_1 + x);
    float acc_3 = (acc_2 + (x * 2.0));
    float acc_4 = (acc_3 + (x * 3.0));
    return acc_4;
  }
  ```
- ### `for` to a runtime bound
  The start and the bound may be values the program learns at run time, and no trip count is too many.
  TypeScript
  ```ts
  export function ramp(x: f32, n: i32): f32 {
    let acc: f32 = 0.
    for (let i = 0; i < n; i += 1) {
      acc = acc + x
    }
    return acc
  }
  ```
  Emitted WGSL
  ```wgsl
  fn ramp(x: f32, n: i32) -> f32 {
    var acc: f32 = 0.0;
    for (var i: i32 = 0; (i < n); i += 1) {
      acc = (acc + x);
    }
    return acc;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float ramp(float x, int n) {
    float acc = 0.0;
    for (int i = 0; (i < n); i += 1) {
      acc = (acc + x);
    }
    return acc;
  }
  ```
- ### `while`
  A loop with a counter the compiler adds. Nothing checks that the body moves toward the bound.
  TypeScript
  ```ts
  export function halve(x: f32): f32 {
    let acc: f32 = x
    let w: i32 = 0
    while (w < 4) {
      acc = acc * 0.5
      w = w + 1
    }
    return acc
  }
  ```
  Emitted WGSL
  ```wgsl
  fn halve(x: f32) -> f32 {
    var acc: f32 = x;
    var w: i32 = 0;
    for (var _w: i32 = 0; (w < 4); _w = (_w + 1)) {
      acc = (acc * 0.5);
      w = (w + 1);
    }
    return acc;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float halve(float x) {
    float acc = x;
    int w = 0;
    for (int _w = 0; (w < 4); _w = (_w + 1)) {
      acc = (acc * 0.5);
      w = (w + 1);
    }
    return acc;
  }
  ```
- ### `switch`
  A `switch`. Each case ends with a `break`, which the lowering drops. A case whose end is reachable is refused ([`TS8017`](https://typeshade.dev/reference/errors/ts8017/)), because TypeScript would run on into the next case and WGSL runs only the one. [bitfield-bands](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/bitfield-bands/bitfield-bands.shade.ts)
  TypeScript
  ```ts
  export function band(m: i32): f32 {
    let v: f32 = 0.
    switch (m) {
      case 0:
        v = 0.25
        break
      default:
        v = 1.
        break
    }
    return v
  }
  ```
  Emitted WGSL
  ```wgsl
  fn band(m: i32) -> f32 {
    var v: f32 = 0.0;
    switch m {
      case 0: {
        v = 0.25;
      }
      default: {
        v = 1.0;
      }
    }
    return v;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float band(int m) {
    float v = 0.0;
    switch (m) {
      case 0: {
        v = 0.25;
        break;
      }
      default: {
        v = 1.0;
      }
    }
    return v;
  }
  ```

## Choosing a value

- ### Ternary on a scalar
  WGSL has no `?:` operator, so the ternary becomes `select`: the false value first, the condition last. WGSL evaluates both values, so an arm that calls a function of the file, reads a texture or holds eight or more operations is hoisted into a slot and an `if` instead, and only the arm taken runs. GLSL ES 3.00 keeps the ternary for the cheap arm.
  TypeScript
  ```ts
  export function nearer(a: f32, b: f32): f32 {
    return a < b ? a : b
  }
  ```
  Emitted WGSL
  ```wgsl
  fn nearer(a: f32, b: f32) -> f32 {
    return select(b, a, (a < b));
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float nearer(float a, float b) {
    return mix(b, a, (a < b));
  }
  ```
- ### Ternary on a struct
  Neither target has an operator for it, so the value is hoisted into a slot and an `if`. [pick-composite](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/pick-composite/pick-composite.shade.ts)
  TypeScript
  ```ts
  export function chosen(
    warm: Palette, cool: Palette, c: bool
  ): vec3 {
    const shade: Palette = c ? warm : cool
    return shade.lo
  }
  ```
  Emitted WGSL
  ```wgsl
  fn chosen(warm: Palette, cool: Palette, c: bool) -> vec3<f32> {
    var _sel0: Palette;
    if (c) {
      _sel0 = warm;
    } else {
      _sel0 = cool;
    }
    var shade: Palette = _sel0;
    return shade.lo;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  vec3 chosen(Palette warm, Palette cool, bool c) {
    Palette _sel0;
    if (c) {
      _sel0 = warm;
    } else {
      _sel0 = cool;
    }
    Palette shade = _sel0;
    return shade.lo;
  }
  ```

## Leaving early

- ### `break`
  A `break`, which is how a counted loop leaves early. [julia-twin](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/julia-twin/julia-twin.shade.ts)
  TypeScript
  ```ts
  export function escape(x: f32): f32 {
    let acc: f32 = x
    for (let i = 0; i < 8; i += 1) {
      acc = acc * 2.
      if (acc > 1.) {
        break
      }
    }
    return acc
  }
  ```
  Emitted WGSL
  ```wgsl
  fn escape(x: f32) -> f32 {
    var acc: f32 = x;
    for (var i: i32 = 0; (i < 8); i += 1) {
      acc = (acc * 2.0);
      if ((acc > 1.0)) {
        break;
      }
    }
    return acc;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float escape(float x) {
    float acc = x;
    for (int i = 0; (i < 8); i += 1) {
      acc = (acc * 2.0);
      if ((acc > 1.0)) {
        break;
      }
    }
    return acc;
  }
  ```
- ### `continue`
  A `continue`. In a `switch` that no loop encloses it is refused.
  TypeScript
  ```ts
  export function odds(x: f32): f32 {
    let acc: f32 = 0.
    for (let i = 0; i < 4; i += 1) {
      if (i === 2) {
        continue
      }
      acc = acc + x
    }
    return acc
  }
  ```
  Emitted WGSL
  ```wgsl
  fn odds(x: f32) -> f32 {
    var acc: f32 = 0.0;
    for (var i: i32 = 0; (i < 4); i += 1) {
      if ((i == 2)) {
        continue;
      }
      acc = (acc + x);
    }
    return acc;
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  float odds(float x) {
    float acc = 0.0;
    for (int i = 0; (i < 4); i += 1) {
      if ((i == 2)) {
        continue;
      }
      acc = (acc + x);
    }
    return acc;
  }
  ```
- ### `discard`
  A `discard`, in a fragment entry or in a function one calls. [cutout](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/gallery/cutout/cutout.shade.ts)
  TypeScript
  ```ts
  @fragment
  export function fs(
    frag: builtin<"position">
  ): vec4 {
    if (frag.x < 0.5) {
      discard
    }
    return vec4(1.)
  }
  ```
  Emitted WGSL
  ```wgsl
  @fragment
  fn fs(@builtin(position) frag: vec4<f32>) -> @location(0) vec4<f32> {
    if ((frag.x < 0.5)) {
      discard;
    }
    return vec4<f32>(1.0);
  }
  ```
  Emitted GLSL ES 3.00
  ```glsl
  layout(location = 0) out vec4 _ret;

  void main() {
    vec4 frag = gl_FragCoord;
    if ((frag.x < 0.5)) {
      discard;
    }
    _ret = vec4(1.0);
  }
  ```
