Bitpath

Lesson 1 · Unit 02

Conditions

Comparisons, and, or, not, if, else and a first match

13 exercisesabout 3 min

What you practise

  • Comparisons, &&, ||, !, if and else, if as an expression with a value, a first match on 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?

Rust
let age = 15;
println!("{}", age >= 18);

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.

Rust
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.

Rust
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.

Rust
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.

One email when Bitpath is ready to try. Nothing else. Privacy