Control Flow
Conditional statements are the heart of any programming language.
If-else
src/main.ing
fn main() {
let a: int = 1;
let b: int = 2;
if (a > b) {
println!(a);
} else if (a < b) {
println!(b);
} else {
println!(-1);
}
}
For now the parentheses around the condition are required.
While
src/main.ing
fn main() {
let a: int = 0;
while (a < 10) {
println!(a);
a = a + 1;
}
}
Sample Output
0
1
2
3
4
5
6
7
8
9
Similarly, the parantheses around the condition are required.
For
src/main.ing
fn main() {
for (let i: int = 0; i < 10; i++) {
println!(i);
}
}
Sample Output
0
1
2
3
4
5
6
7
8
9
This is a C-style for loop. The parentheses around the condition are required.
Break and Continue
src/main.ing
fn main() {
for (let i: int = 0; i < 10; i++) {
if (i == 5) {
break;
}
println!(i);
}
}
Sample Output
0
1
2
3
4
src/main.ing
fn main() {
for (let i: int = 0; i < 10; i++) {
if (i == 5) {
continue;
}
println!(i);
}
}
Sample Output
0
1
2
3
4
6
7
8
9
This is similar to many other languages.
Last modified: 30 January 2024