Skip to content

Commit

Permalink
tour: add more detail about block and if
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin committed Dec 17, 2024
1 parent 2f98acb commit 4786507
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Control flow
# Loop

In this example, we use for loops, while loops, and an if-else expression to
iterate over an array.
In this example, we use for loops and while loops to iterate over an array.

## For loop
## For Loop Expression

The for loop is analogous to a C-style for loop:

Expand All @@ -18,11 +17,11 @@ The loop initializes the variables in the `init` part before it starts. When the
In MoonBit, the for loop is more expressive than the C-style for loop. We will
explain it in the following chapters.

## While loop and if-else expression
## While Loop Expression

The while loop is also similar to the C-style while loop.

It tests the condition before executing the loop body. If the condition is true,
it executes the loop body and repeats the process until the condition is false.

MoonBit also supports both `continue` and `break` within the loop.
MoonBit also supports both `continue` and `break` statements within the loop.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn add(a : Int, b : Int) -> Int {
a + b
return a + b
}

fn compute() -> Unit {
Expand Down
26 changes: 26 additions & 0 deletions moonbit-tour/tour/chapter1_basics/lesson4_block/index.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
fn main {
let a = 100

{
let mut a = 0
println("checkpoint 1")
a = a + 1
}

println("checkpoint 2")
println(f())
}

fn f() -> Int {
let b = 3.14

let result = {
let b = 1
println("checkpoint 3")
b + 5
}

result // same as `return result` here
}


29 changes: 29 additions & 0 deletions moonbit-tour/tour/chapter1_basics/lesson4_block/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Block and Statements

A block is an expression contains sequence of statements and an optional
expression.

```
{
statement1
statement2
expression
}
```

For example, the code above will execute `statement1`, `statement2`
and evaluate `expression` in order. The evaluation result of the block is the
evaluation result of the expression. If the last one expression is omitted,
the block will result in `()`, which type is `Unit`.


A statement can be a variable declaration, variable assignment, or any
expression which type is `Unit`.


A block is also associated with a namespace scope. In the `main` clause, the variable `a` declared in the inner block will shadow the outer `a`. It is only visible within the inner block.





35 changes: 35 additions & 0 deletions moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
fn fib(x : Int) -> Int {
if x < 2 {
x
} else {
fib(x - 1) + fib(x - 2)
}
}

fn main {
if 5 > 1 {
println("5 is greater than 1")
}
println(fib(5))
println(weekday(3))
}

fn weekday(x : Int) -> String {
if x == 1 {
"Monday"
} else if x == 2 {
"Tuesday"
} else if x == 3 {
"Wednesday"
} else if x == 4 {
"Thursday"
} else if x == 5 {
"Friday"
} else if x == 6 {
"Saturday"
} else if x == 7 {
"Sunday"
} else {
"Invalid day"
}
}
14 changes: 14 additions & 0 deletions moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# If expression

An if expression is a conditional control flow expression that has a result value.

In an if expression, each branch must have the same type. If the condition is true, it returns the result value of the first branch. Otherwise, it returns the result value of the second branch.

The `else` part is optional. If it is omitted, the type of the whole if expression will be `Unit`.

Nested if expressions in the else part can be shortened by using `else if`.





0 comments on commit 4786507

Please sign in to comment.