[No Audio]
Welcome back again in the course. We will
complete our discussion of the if statement
in this tutorial, by learning the remainder of
its variants. So, let's get started.
The first variant of the basic if statement we
will learn is, the nested if statement. By
nested, we mean an if statement which contains
another if statement inside it. Let us see
the general syntax for the nested if variant.
[No Audio]
The outer conditions are being checked first,
if it happens to be true, then the statement
included inside it's respective body will get
a chance to execute. When both the outer and
inner conditions are true, then the statement
inside the inner if statement will also get a
chance to execute. Please note that the outer
and inner if statements may have the
respective else part also.
Let us do a quick example
on this, where we will take an input
from the user, which will be checked for being
zero or not. If it is not zero, then we will
check if it is being odd or even. We will
assume that the number zero is neither odd
nor even. Let us code this problem.
Next I will add a suitable display message, so
that the user knows what the program is
expecting as an input.
[No Audio]
First we will take an integer input from the user.
[No Audio]
Next we will check if the value is zero or
not, using an if statement.
[No Audio]
The same statement may be written as not,
and then some_num == 0.
[No Audio]
Inside the body of the if statement, I
will add additional conditions
to check if the number is even or odd.
[No Audio]
The percent operator is also called as mod
operator, and it computes the remainder
after dividing the number mentioned to the
left of it, by the number mentioned to the
right of it. In this case, it will divide the
value of some_num by 2, and will
compute the remainder. If the remainder
happens to have a value of 0, then it will
be an indication that the number is even, and
if the remainder is not zero then the number
will be odd. If the outer if condition is
true, the inner if condition will be checked.
Based on whether the associated inner if
condition is true or not, we will either
execute the inner if or, or its associated else parts.
For the outer if statement, we may have
an optional else part also. We may add a
suitable print statement to the else part to
indicate that the number is 0, and it is
therefore neither even or odd.
[No Audio]
Let us cargo run this.
[No Audio]
I will provide a value of 13, it
displays that the number is odd, okay great.
It is a good programming practice to
properly indent the inner if statement
and its respective body, so that the code is
easily readable. Let us now learn the if let
syntax variant of the if statement.
[No Audio]
With the help of this variant, we are able to assign a
value to some variable based on the body of
the if statement. Let us look at the
general syntax first.
[No Audio]
Inside the body of the if and the else parts,
we need to have a single value or statement,
after which there is no semicolon, which will
be the value that will be assigned to the
variable_name variable. Finally,
we will need to put a semicolon at the end of
the complete block of the if else statements.
Let us look at this variant in action.
I will declare a variable, a value, which will store
the result of the if else statement.
[No Audio]
The condition associated with the if statement, in this case
is true, which means that the body of the if
statement will be executed. And therefore the
value 1 will be assigned to the variable value,
let us execute this.
[No Audio]
The value is being displayed
for the variable. Please note
that the return value from the if part is
without a semicolon, and the value returned
from the else part is also without a
semicolon, and both of these values are of the same type.
One of these values will be
assigned to the variable value, and we know
that a variable in Rust should have a defined
type at compile time, otherwise the compiler
will complain. If for instance, we change the
value of 2 in the else part to that of 2.0
and save. So the compiler will rightfully
complain, because it will be confused
regarding the data type of the variable value.
Now, let us look at another scenario,
or variant of the if let syntax. We can use the
same syntax in the if-else ladder, that we
have seen in the previous tutorial.
Let us see how to use if let variant in the
context of if-else ladder.
We will use the same program
that we saw in the previous tutorial,
and we will modify it.
Instead of rewriting it, I will paste it.
[No Audio]
Now instead of assigning and setting the
value of the variable grade, inside each of
the blocks, I will assign it using the
if let syntax by declaring the grade
variable at the start and then I will mention
all the conditions.
[No Audio]
Inside each of the block, I
will remove the assignment and we'll just
keep the character letters.
Let us do the remaining necessary changes.
[No Audio]
You may note, let the code is now more
reduced and therefore more readable and compact.
Please note that, since we need to
make an assignment to the variable, so we
cannot skip the else part in this case,
because, that will leave the variable empty.
The point to remember is that, with the
if let syntax, we cannot leave a variable empty.
Finally we also need to put the
mandatory semicolon.
[No Audio]
Okay let us execute.
[No Audio]
You may note, that the variable has been
assigned a value, based on the marks of the student.
The important point to note is that,
we should have at least one block which
returns some value for the variable.
With this we end this tutorial, we have learned
additional two ways in which the if statement
can be used, that is the nested if and the if let syntax.
In the upcoming tutorial, we will
be looking at a very similar but different
construct called match. Which will also work
very similarly, but has some essential differences also.
Do come back for covering that
and until next tutorial, enjoy Rust programming.
[No Audio]