learn-rust

Free Rust ๐Ÿฆ€ course in English ๐Ÿ‡ฌ๐Ÿ‡ง

View on GitHub

Table of Contents๐Ÿ“š

if else statements

comparison operators

The comparison operators are used to compare two values.

Operator Description
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to

if

Imagine that you are a programmer and you have to write a program that will tell if the value of a variable is positive

let x = 5;

For that, you will have to check if x is superior to 0.

We can do that with the if statement.

if x > 0 {
    println!("x is positive ๐Ÿ‘");
}

The code between the {} will be executed because the condition x > 0 is true.

If we change the value of x to a negative number, the code between the {} will not be executed.

else

Imagine that you are a programmer and you have to write a program that will say hello if the value of a variable is equal to 5, and goodbye if it is not.

To do that, you will first have to check if x is equal to 5, and then use the else statement to say goodbye if the condition is not true.

if x == 5 {
    println!("Hello ๐Ÿ‘‹");
} else {
    println!("Goodbye ๐Ÿซ‚");
}

Letโ€™s run the code with x = 5:

Hello ๐Ÿ‘‹

And with x = 6:

Goodbye ๐Ÿซ‚

else if

It is possible to have more than one condition to be checked.

Imagine that we want to say hello if x is equal to 5, good morning if it is equal to 6, and good night if either of the two conditions is not true.

we can do that with the else if statement.

if x == 5 {
    println!("Hello ๐Ÿ‘‹");
} else if x == 6 {
    println!("Good morning ๐ŸŒ…");
} else {
    println!("Good night ๐Ÿ›Œ");
}

lets run the code with x = 5:

Hello ๐Ÿ‘‹

And with x = 6:

Good morning ๐ŸŒ…

And with x = 7:

Good night ๐Ÿ›Œ

โ„น๏ธ We can put as many else if as we want. The condition of the else if will be checked after the condition of the previous else if.


Home ๐Ÿ  - Next Section โญ๏ธ


Course created by SkwalExe and inspired by Dcode