Julia Flow Control
Flow control statements are implemented by setting one or more condition statements in the program. Execute the specified program code when the condition is true, and execute other specified code when the condition is false.
Julia provides a large number of flow control statements:
* **Compound Expressions**: begin and ;.
* **Conditional Expressions**: if-elseif-else and ?: (ternary operator).
* **Short-Circuit Evaluation**: logical operators && (and) and || (or), and chained comparisons.
* **Loop Statements**: loops: while and for.
* **Exception Handling**: try-catch, error and throw.
* **Task (Coroutine)**: yieldto.
* * *
## Compound Expressions
The begin ... end expression can evaluate several sub-expressions in order and return the value of the last sub-expression:
## Example
julia> z = begin
x = 1
y = 2
x + y
end
3
Since these are very brief expressions, they can simply be put on one line, which is where the ; chain comes from:
## Example
julia> z = (x = 1; y = 2; x + y)
3
In actual usage, the begin block does not need to be multi-line, nor does the ; chain need to be single-line:
## Example
julia> begin x = 1; y = 2; x + y end
3
julia>(x = 1;
y = 2;
x + y)
3
* * *
## Conditional Expressions
Conditional expressions can decide which code block to execute based on the value of a boolean expression.
The if-elseif-else syntax:
if boolean_expression 1
/* execute when boolean expression 1 is true */
elseif boolean_expression 2
/* execute when boolean expression 2 is true */
elseif boolean_expression 3
/* execute when boolean expression 3 is true */
else
/* execute when none of the above conditions are true */
An **if** statement can be followed by an optional **else if...else** statement, which can be used to test multiple conditions.
When using if...else if...else statements, the following points should be noted:
* An if can be followed by zero or one else, and else must come after all else if.
* An if can be followed by zero or more else if, and else if must come before else.
* Once an else if matches successfully, other else if or else will not be tested.
Below is an analysis of the if-elseif-else conditional syntax:
## Example
if x y
println("x is greater than y")
else
println("x is equal to y")
end
If the expression x y, and if it is true, execute its corresponding code block; if no expression is true, execute the else code block.
## Example
julia>function test(x, y)
if x y
println("x is greater than y")
else
println("x is equal to y")
end
end
test (generic function with 1 method)
julia> test(1, 2)
x is less than y
julia> test(2, 1)
x is greater than y
julia> test(1, 1)
x is equal to y
### Ternary Operator
The ternary operator ?: is similar to if-elseif-else syntax:
a ? b : c
The expression a before ? is a conditional expression. If condition a is true, the ternary operator evaluates expression b before :; if condition a is false, it executes expression c after :.
**Note:** Spaces around ? and : are mandatory. Expressions like a?b:c are not valid ternary expressions (but newlines after ? and : are allowed).
## Example
julia> x = 1; y = 2;
julia>println(x x = 1; y = 0;
julia>println(x < y ?"less than" : "not less than")
not less than
If the expression x test(x, y) = println(x y ?"x is greater than y" : "x is equal to y")
test (generic function with 1 method)
julia> test(1, 2)
x is less than y
julia> test(2, 1)
x is greater than y
julia> test(1, 1)
x is equal to y
For convenient chained passing, operators are connected from right to left.
Similar to if-elseif-else, the expressions before and after : will only be executed when the conditional expression is true or false:
## Example
julia> v(x) = (println(x
YouTip