Lesson 04 — Control Flow & Expressions
Series: index · prev: 03 · next: 05 — The
.sstsheet & Nordshrift
The statements inside a method are Java-like: conditionals, loops, assignment, and expressions.
Conditionals
int classify(int n) {
if (n < 0) {
return -1;
} else {
if (n == 0) {
return 0;
}
return 1;
}
}
if (cond) { ... } else { ... }. Conditions are boolean expressions.
Loops
while and for both exist. A real while (from Demo.sleela):
int i = 1;
while (i <= 5) {
print("square(" + i + ") = " + square(i));
i = i + 1;
}
A for counts explicitly:
for (int i = 0; i < 3; i = i + 1) {
print("i = " + i);
}
Operators
| Kind | Examples |
|---|---|
| Arithmetic | + - * / % |
| Comparison | == != < <= > >= |
| Logical | && || ! |
| String | + (concatenation) |
A worked example that mixes them — align a length up to a 4-byte boundary
(Message.sleela):
int align4(int n) {
int r = n % 4;
if (r == 0) {
return n;
}
return n + (4 - r);
}
Printing
print(x) writes one line. Build strings with +:
print("body=" + body + " aligned=" + align4(body));
print("symmetric = " + (senderSize == receiverSize)); // prints true/false
Note that a boolean expression can be concatenated into a string directly —
handy for quick assertions in a main().
A complete runnable
#sleela 1.2
class Counter {
int sumTo(int n) {
int total = 0;
for (int i = 1; i <= n; i = i + 1) {
total = total + i;
}
return total;
}
void main() {
print("sum 1..10 = " + sumTo(10)); // 55
}
}
You now have enough language to write real Wrappers. Next, learn how the build selects and drives them.