For the complete documentation index, see llms.txt. This page is also available as Markdown.

Control Flow

In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. [1]

This page provides syntax for some of the common control flow methods in Julia . Each section includes an example to demonstrate the described methods.

Use Cases and Syntax

  • Test if a specified expression is true or false

  • Short-circuit evaluation

    • Test if all of the conditions are true x && y

    • Test if any of the conditions are true x || y

    • Test if a condition is not true !z

  • Conditional evaluation

    • if statement

    • if-else

    • if-elseif-else

    • ?: (ternary operator)

Conditional Statements

Input:

Output:

Loops

  • Repeat a block of code a specified number of times or until some condition is met.

  • while loop

  • for loop

  • Use break to terminate loop

Input:

Output:

Comparison Operators and Functions

Operator
Example

Equality

x == y or isequal(x, y)

Inequality

x != y or !isequal (x, y)

Less than

x < y

Less than or equal to

x <= y

Greater than

x > y

Greater than or equal to

x >= y

Input:

Output:

References

  1. Wikipedia contributors. (n.d.). Control flow. In Wikipedia. Retrieved May 1, 2024.

Resources

Last updated