---
title: "WGSL statements and expressions, and how to write them"
description: "The statements are TypeScript, and each one means what the WGSL beside it means. The text comes from one file compiled while this page is built."
url: https://typeshade.dev/guide/language/from-wgsl/statements/
language: en
---

# Statements and expressions

The statements are TypeScript's, and each one means what the WGSL beside it means. The emitted text comes from one file compiled at build time, so the parentheses and the folded constants are the writer's own.

WGSL TypeShade Note

```wgsl
let a = (uv.x * 2.0);
```

```ts
const a = uv.x * 2.
```

A WGSL `let` is a value that stays as it was, which is what `const` says in TypeScript.

```wgsl
var b: f32 = uv.y;
```

```ts
let b = uv.y
```

A WGSL `var` is a local that changes. `let b: f32` with no initializer declares one and leaves the value for a later assignment. A read on a path that has not assigned it is refused ([`TS8075`](https://typeshade.dev/reference/errors/ts8075/), [Rule 7.6](https://typeshade.dev/reference/rules/7-6/)), and every target starts the local at zero.

```wgsl
for (var i: i32 = 0; (i < n); i = (i + 1)) {
```

```ts
for (let i: i32 = 0; i < n; i++)
```

A `for` means what it means in TypeScript. The compiler records it as counted when it has an integer induction variable, a constant step, and an exit that compares the variable to a bound the body does not write; any other `for` is an open loop, as a `while` is. The start and the bound may be runtime values, and no trip count is too many. The step may be `+=`, `-=`, `*=` or `/=`.

```wgsl
for (var _w: i32 = 0; (w < 4); _w = (_w + 1)) {
```

```ts
while (w < 4)
```

A `while` takes the place of `loop`. It is an open loop: it ends when its condition fails, or at a `break` or a `return`, and `while (true)` needs one of the two in its body.

```wgsl
switch i32(b) {
```

```ts
switch (i32(b))
```

Each case ends with `break`, which a WGSL case does without and the lowering drops. A case whose end is reachable would run on into the next case in TypeScript, so it is refused ([`TS8017`](https://typeshade.dev/reference/errors/ts8017/)). The last case needs no `break`. A label is an integer constant that may appear once. Two labels stacked over one body are one case with two selectors, `case 0, 1:` on WGSL.

```wgsl
let c = select(b, a, (uv.x > 0.5));
```

```ts
const c = uv.x > 0.5 ? a : b
```

WGSL has no ternary, so a scalar or vector conditional becomes `select`, whose first argument is the arm the condition does not choose. A conditional on a struct or an array is hoisted into a slot and an `if`, since neither target has an operator for that, and so is one whose arm is costly: a call of a function of the file, a texture read, or eight or more operations, so the arm not taken does not run.

```wgsl
bump(u32(b));
```

```ts
bump(u32(b))
```

A function called for what it does, with its result dropped.

```wgsl
_ = atomicAdd(&counter, 1u);
```

```ts
atomicAdd(counter, 1)
```

A value-returning builtin dropped the same way takes the phony assignment, because Tint reads every such builtin as `@must_use`.

```wgsl
discard;
```

```ts
discard
```

The same word, and the same statement.

```wgsl
struct VsIn {
  @location(0) pos: vec3<f32>,
}
```

```ts
interface VsIn { pos: location<0, vec3> }
```

A WGSL struct is a class. A type alias of an object shape and an interface both reach the same declaration.

```wgsl
fn bump(i: u32) {
```

```ts
function bump(i: u32): void
```

A function with no stage attribute is a helper of the module.
