• Kipper

    v0.12.1
  • Downloads

  • Docs

  • Playground

  • Changelog

  • Kipper

    v0.12.1
    • Downloads

    • Docs

    • Playground

    • Changelog

      • 0.11.0 /
      • Expressions /
      • Conditional Expressions
    Kipper Docs
    • next
    • latest
    • 0.11.0
    • 0.10.4
    • 0.9.2
    • Overview

    • Quickstart

    • Goals for Kipper

    • Supported platforms

    • Usage Examples

      • Overview

      • Compiler Setup

        • Overview

          • index

          • compiler

          • errors

          • logger

          • utils

          • config

          • index

          • index

        • Overview

        • new

        • run

        • compile

        • help

        • version

    • Variables

      • Overview

      • String Type

      • Number Type

      • Boolean Type

      • Void Type

      • Null Type

      • Undefined Type

      • Array Type

      • Overview

      • Arithmetic Expression

      • Assignment Expression

      • Conditional Expressions

      • Logical Expressions

      • Bitwise Expression

      • Relational Expressions

      • Convert Expression

      • F-String Expression

      • Member Access Expression

      • Function Call Expression

      • Overview

      • Expression Statement

      • If Statement

      • While Loop

      • Do-While Loop

      • For Loop

      • Compound Statement

      • Jump Statement

    • Functions

    • Comments

    • Built-in Functions

      • 0.11.0 /
      • Expressions /
      • Conditional Expressions
    • 0.11.0 /
    • Expressions /
    • Conditional Expressions

    Edit page

    Conditional Expressions (Ternary conditional)

    Conditional expressions are like if-statements with the major difference and advantage of being in-line and allowing evaluation of specific expressions based on a condition.

    If the CONDITION evaluates to true, the left side of the : operator is evaluated and returned, otherwise the right side is evaluated and returned.

    Syntax

    CONDITION ? EVALUATE_IF_TRUE : EVALUATE_IF_FALSE;

    Examples

    Simple Conditional Expressions

    // ✓ Simple evaluation of a number
    true ? 3 : 2; // -> 3
    false ? 3 : 2; // -> 2

    Using Conditional Expressions to assign a value

    // ✓ Simple evaluation of a number that will be assigned
    var var1: num = 2;
    var var2: num = 9;
    var smallestOrEqual: num = var1 < var2 ? var1 : var2; // -> 2

    Chained Conditional Expressions

    As the ternary operator evaluates to more expressions, you can also chain it like this:

    // ✓ Valid - Chained if ... else-if ... else-if ... else ternary operator
    var result: num = condition1 ? value1
         : condition2 ? value2
         : condition3 ? value3
         : value4;

    Equivalent to:

    if (condition1) { var result = value1; }
    else if (condition2) { var result = value2; }
    else if (condition3) { var result = value3; }
    else { var result = value4; }
  • Developed at:

    • Releases
    • GitHub
    • Security
    • Issue Tracker
    • License
  • Copyright © 2021-2025 Luna Klatzer, 2024-2025 Lorenz Holzbauer & Fabian Baitura.
    Kipper is licensed under the GPL-3.0-or-later license.