File: conditionals.md

package info (click to toggle)
surgescript 0.5.4.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,876 kB
  • sloc: ansic: 13,674; makefile: 16
file content (72 lines) | stat: -rw-r--r-- 1,536 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Conditionals
============

Introduction
------------

Conditionals are *if ... then ...* statements. If a certain `condition` evaluates to `true`, execute a block of code. If not, don't execute it.

```
if(condition) {
    // this will be executed only if
    // the condition is true
}
```

Alternatively, we may include an `else` statement followed by a block of code that will only be executed if the condition is **not** true:

```
if(condition) {
    // this will be executed only if
    // the condition is true
}
else {
    // this will be executed only if
    // the condition is false
}
```

Example
-------

The following example will print *underaged* if variable `age` is less than 18, or *adult* otherwise:

```
object "Application"
{
    age = 23;

    state "main"
    {
        if(age < 18) {
            // variable age is less than 18
            Console.print("underaged");
        }
        else {
            // variable age is not less than 18
            Console.print("adult");
        }
    }
}
```


Inline conditionals
-------------------

Just like other languages with C-based syntax, the expression `condition ? true_value : false_value` evaluates to `true_value` if `condition` is `true` and to `false_value` if `condition` is `false`.

For example, the script below will print *underaged* if variable `age` is less than 18, or *adult* otherwise:

```
object "Application"
{
    age = 23;
    message = age < 18 ? "underaged" : "adult";

    state "main"
    {
        Console.print(message);
    }
}
```