Lesson 1 · Unit 02
Conditions
Comparisons, and, or, not, if, else and a first match
What you practise
- Comparisons,
&&,||,!,ifandelse,ifas an expression with a value, a firstmatchon numbers
Try a question from this lesson
Pick one
>= asks a question: is the left side greater than or equal to the right? What does this print?
let age = 15;
println!("{}", age >= 18);Correct answer
false
Why
15 is not greater than or equal to 18, so the answer to the question is no. Rust writes no as false and yes as true. These two values are the type bool you met with variables.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Comparisons
A comparison gives a bool: true or false.
There are six: == equal, != not equal, < less, > greater, <= less or equal, >= greater or equal.
Careful: one = stores a value under a name. Two, ==, ask whether two values are the same.
let lives = 3;
println!("{}", lives == 3);
println!("{}", lives != 3);
println!("{}", lives < 3);
println!("{}", lives <= 3);Concept
`if`, `else if`, `else`
else gives the path for when the test is false. Exactly one of the two paths runs.
else if adds another test. Rust goes down the chain, takes the first test that is true, and skips everything after it.
let n = 0;
if n > 0 {
println!("positive");
} else if n < 0 {
println!("negative");
} else {
println!("zero");
}Concept
And, or, not
a && b is true only when both sides are true.
a || b is true when at least one side is true.
!a flips a bool: not true is false, not false is true.
let sunny = true;
let warm = false;
println!("{}", sunny && warm);
println!("{}", sunny || warm);
println!("{}", !warm);9 more exercises in the app
- 1× match the pairs
- 3× multiple choice
- 2× type the answer
- 2× fill the blanks
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.