1 Conditionals
Rust's if/else is an expression. Combine tests with && and ||.
fn main() {
for n in 1..=6 {
if n % 2 == 0 {
println!("{} even", n);
} else {
println!("{} odd", n);
}
}
}
Conditionals, accumulation and iterating over ranges.
Rust's if/else is an expression. Combine tests with && and ||.
fn main() {
for n in 1..=6 {
if n % 2 == 0 {
println!("{} even", n);
} else {
println!("{} odd", n);
}
}
}
Use a mut accumulator and a loop to fold a range into a single result.
fn main() {
let mut product = 1;
for i in 1..=6 {
product *= i;
}
println!("6! = {}", product);
}
while repeats until its condition is false — handy when the count is data-dependent.
fn main() {
let mut n = 27;
let mut steps = 0;
while n > 1 {
if n % 2 == 0 { n = n / 2; } else { n = 3 * n + 1; }
steps += 1;
}
println!("steps = {}", steps);
}
Code runs on a learning-oriented simulated runtime that supports common constructs (output, variables, arithmetic, conditionals and loops).
5 tasks across 2 pages — multiple-choice and fill-in (type the answer). Score 70% or higher to earn your certificate.
🔒 Pass the quiz above (70%+) to unlock your downloadable certificate.
Congratulations! Enter your name to generate your certificate.