Jump to content

Rust for the Novice Programmer/If statements and booleans

100% developed
From Wikibooks, open books for an open world

If Statements and Booleans

[edit | edit source]

One fundamental concept is that we might want to do something different depending on something. For example, we might want to cap a number at a certain threshold, say 10. Thus, if the number is higher than 10, we want to set it to 10. To express this, we use what are called if statements. an if statement uses what are called booleans. A boolean is a value of either true or false. In Rust it is shortened to 'bool' to make it quicker to type. For example:

 fn main() {
     let number = 5;
     let is_six: bool = number == 6;
 }

Here we are using == which checks whether the two things on the left and right of it are equal. If they are it becomes true, otherwise it becomes false. Since 5 is not equal to 6, is_six will be false. There are other things like ==, called 'operators' since they operate on inputs. The most common ones are == which compares if two things are the same, != which compares if two things are not the same, > which compares if the left thing is greater than the right thing, >= which compares if the left thing is greater than or equal to the right thing and the reverse for < and <=.

We use these in if statements by putting a boolean in and if the boolean is true, we will go inside the {} of the if statement, otherwise not.

 fn main() {
     let mut number = 5;
     let is_six: bool = number == 6;
     if is_six {
         number += 4;
     }
     println!("{}", number);
 }

So here, since is_six is false, we won't go inside the if braces and the number printed out will be 5. If however, we changed it to:

 fn main() {
     let mut number = 5;
     let is_five: bool = number == 5;
     if is_five {
         number += 4;
     }
     println!("{}", number);
 }

Then since the number is equal to 5, is_five is true and we go inside the braces, causing number to be equal to 9 at the end. This is quite verbose so normally the boolean value wouldn't be put into a variable and would go directly into the if statement as follows:

 fn main() {
     let mut number = 5;
     if number == 5 {
         number += 4;
     }
     println!("{}", number);
 }

This is quicker to write, more concise and easier to change later so is generally preferred.

Let's say we wanted to add 4 if the number was equal to 6, and subtract 4 if the number wasn't equal to 6. How would we do this? We could do a second if statement using the != not equals operator like this:

 fn main() {
     let mut number = 5;
     if number == 6 {
         number += 4;
     }
     if number != 6 {
         number -= 4;
     }
     println!("{}", number);
 }

This will work, but is quite long and awkward to read and change. Instead we use the else operator:

 fn main() {
     let mut number = 5;
     if number == 6 {
         number += 4;
     } else {
         number -= 4;
     }
     println!("{}", number);
 }

Here, since number isn't equal to 6, we go to the else and subtract 4 so number is equal to 1.

We can also combine if and else to do different things: For example, let's say we wanted to add 4 if number was less than 7, subtract 4 if number was greater than 9 and add 1 otherwise. This would be awkward to do as separate if statements, since if the number was 6, it is less than 7 so we would add 4, making it 10, which means we would then subtract 4 from it since it is greater than 9. Therefore, using else statements to separate each section into its own block is convenient. We can do that behaviour as follows:

 fn main() {
     let mut number = 5;
     if number < 7 {
         number += 4;
     } else if number > 9 {
         number -= 4;
     } else {
         number += 1;
     }
     println!("{}", number);
 }

And the number ends up as 9.

Let's go back to the example at the start of capping a number at 10. Now we can do that and we can express it as a function.

 fn cap_at_10(input: i32) -> i32 {
     if input > 10 {
          return 10;
     } else {
          return input;
     }
 }

Then we can use this in main() as follows:

 fn main() {
     let mut number = 8;
     number = cap_at_10(number);
     let mut number2 = 14;
     number2 = cap_at_10(number2);
     println!("{}", number);
     println!("{}", number2);
 }

This will print out 8 and 10 since the 8 is less than 10, we go to the else {} part of the if statement, so just return 8 from the function. While with 14, it is greater than 10 so we go to the if {} part of the if statement, so return 10.

A key point about all these programming things is that they are quite simple. Their power comes from using them together. From little things big things grow!

Next: Loops and iteration