Rust for the Novice Programmer/If statements and booleans

If Statements and Booleans
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: 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. 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: 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: 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: This will work, but is quite long and awkward to read and change. Instead we use the else operator: 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 then 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: 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. Then we can use this in main as follows: 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