[No Audio]
Hello and a warm welcome again
in the new section of the course.
This section intends to cover the
basic programming constructs called the
control structures, that are part of almost
all programming languages. control structures
are the constructs, that analyze variables and
choose directions in which to execute the
code, based on some parameters. The basic
control structures in programming languages
are conditionals. Conditional statements or
conditional expressions are also sometimes
referred to as conditional constructs or
programming language commands for handling
decisions inside the program.
The first, and very basic type of conditional construct,
which is used in almost all the programming
languages, is the If conditional statement.
This tutorial will discuss this in detail, so
let's start with today's learning agenda.
The first question is, when the if statement is
being used? We use it when we need to execute
a block of statements or code segment, when a
certain condition is being true. Let us look
at the general syntax for the if statement,
which will make things more easy to
understand and comprehend.
[No Audio]
The statements inside the curly brackets, are
referred to as the body of the if statement,
and they will only execute if the given
condition in front of the if statement
evaluates to a value of true. In case the
condition is false, then the statement inside
the if are skipped, and the remaining code
after the ending curly brackets, are executed
as normal. Let us have some examples. I will
declare a variable some_number and
will assign to it a value of 40.
[No Audio]
Next I will check, if the value of some_number
is less than 50.
[No Audio]
If it is true, then I will display the
message, the number is less than 50.
[No Audio]
Outside the body of the if statement, I will
add another print statement which will
execute, irrespective of the condition
in the if statement.
[No Audio]
In this case, now since the condition in the
if statement will be true, as the value of the
variable is less than 50. Therefore, the
print statement inside the if statement, will
be executed. The statement outside the if
statement, since it is not associated with any
condition, so this statement will also execute.
Let us Cargo run to confirm.
[No Audio]
if I change the value of the variable, to that
of, let's say 55 and execute again.
You may note that, the statement inside the if
statement is not executed, but the statement
outside the if statement has executed.
[No Audio]
This is because, there is no condition associated with it.
Okay now, let us move ahead. The
condition in the if statement, can be composed
of multiple conditions that are joined
together using the logic operators, such as
the AND and OR operations. Let us see an
example on this. Let me first declare a
variable of marks, and assign it a value of 65.
[No Audio]
Let us now check, if the student marks are
in a certain range, let's say in the range of 60 to 70.
[No Audio]
For the marks to be in this range,
we will have conditions to check that they
are greater than or equal to 60, and less than
or equal to 70. The two conditions can be
combined, with the help of logical AND
operation. The logical AND operation will
return a true, when both the conditions to the
left of it, and to the right of it, are both
true at the same time. The logical AND
operation is indicated by the two && signs.
[No Audio]
If the marks are in the range, then we will
display a suitable message.
[No Audio]
Let us execute this.
[No Audio]
This statement has been
printed which indicates to us that the
condition was overall true. Please note that,
one & sign is used for a reference in Rust.
And two & signs are used, for a logical AND
operation. Similar to the logical AND
operation, we can use the logical OR operation
to construct the conditions for the if statement.
Let us do an example on this.
I will declare a couple of Boolean variable,
called flag_1 and flag_2.
[No Audio]
Next I will set condition, if flag_1 == true
or flag_2 == true, then the body of
the if statement should be executed.
The logical OR is mentioned using two vertical
pipes, let me add a suitable display message
inside the if statement and execute.
[No Audio]
The message is displayed,
which means that the conditions it will await
it to true. If we want to construct a
condition by checking if two things are
equal, then we will use two equal signs
instead of one equal sign, between the value
of the two items. Negation operation can also
be used to construct the conditions, let us
also learn this.
I will create a boolean variable,
and we'll assign to it a value of true.
Next, I will check if the value of the
flag is not equal to false, is true or not.
If flag is not false, then this condition will be true.
Since flag is not false, therefore,
this condition will evaluate to a value of true.
Let us confirm this by adding suitable
print statement, and then execute.
[No Audio]
We can always combine the simpler expressions
involving AND or OR operation to make more
complex conditions. Let us see detailed
examples on this.
I will define two boolean
variables, and one integer variable with a value of 60.
[No Audio]
I will now construct a complex condition, by
setting flag_1 == true,
and a flag_2 == false,
or number < 50
[No Audio]
This condition has two parts, the first
part will be only true, when both the
conditions are true. That is flag_1
has a value of true, and flag_2
has a value of false. The OR outside the
brackets means that, either the condition to
the right, or to the left is being true.
That is for the overall condition to be true,
either the value should be lesser than 50, or
the value inside the bracket should be true.
Since the value is not lesser than 50, so we
will consider the first part, where both the
conditions are true. So therefore, the entire
condition is true. Let us add a suitable
print statement to the body of the if
statement and execute.
[No Audio]
The complex condition has evaluated to true.
Please note that, if I change the brackets and
enclose the second and third conditions
together inside the small brackets, then this
expression will have a different meaning.
This means that you have to pay special
attention when aiding smooth brackets for
constructing complex expressions. Okay, now
we will learn some variants of the basic if
statement. The first variant is called if else
conditional statement. Let us start from
the general syntax first.
[No Audio]
If the condition associated with the if
statement is true, then the statements inside
the body of if are executed, and the statement
inside the body of else are skipped and not
executed. On the other hand, if the statement
associated with the if statement is false,
then the statement inside the body of the
else are executed, and the statement inside,
the body of the if are skipped from execution.
Let us understand this with the help of an example.
We will declare a variable of marks,
from a value of 80.
[No Audio]
Then we will check if marks > 50,
then I will print the statement
that you have passed, and if it
is not true, then in the else part,
I will print the message that you have failed.
[No Audio]
Let us execute to see the result.
[No Audio]
The candidate has passed. Another frequently used
variant of the if statement, is the if else, if ladder.
We will start with the general syntax.
[No Audio]
The conditional expression in this case are
evaluated from top to bottom. As soon as a
true condition is found, the statement
associated with that if statements are
executed and the rest of the ladder is
skipped. In other words, the conditions are
checked in sequential order and the body of
the first condition happening to be true is
only executed. If none of the conditions
are true, then the else part will be executed.
Also note that the else included at
the end is optional. Let us understand this
further, with the help of an example. I will
write a simple program, which based on the
marks of the student, will assign a suitable
grade to the student. I will create a
variable which will contain the marks of the
student, and initialize it from a value of 95.
Now based on the marks, I will assign a
letter grade to the student. To store the
grade, I will declare a grade variable and
will initialize it from a value.
And is a sort of default value, having a meaning of no break.
[No Audio]
Now, I will first aid conditions
for the maximum grade, which is A, and it is
given to a student securing greater than or
equal to 90 marks, so let me code it.
[No Audio]
The next grade is B, which corresponds to
marks greater than or equal to 80.
[No Audio]
Next comes the grade of C, which corresponds
to marks greater than or equal to 70.
[No Audio]
The next grade given on marks, greater than or
equal to 60.
[No Audio]
In the optional else part, I will assign a grade of F.
Let us also add a suitable display
message for printing the student grade.
[No Audio]
Let us execute to examine the grade.
[No Audio]
The grade is A in this case, the first if statement
happens to be true in this case, and
therefore, the grade was being set to that of A.
Although the remaining conditions are also
true in this case, that is the marks are also
greater than or equal to 80, and 70, and 60. But
only the body of the first if statement is
being executed. This is because as pointed
out earlier also, in the if-else ladder, the
conditions are checked in sequential order,
and the body of the first condition happening
to be true is always executed. That brings us
to the end of this tutorial. Our discussion
on the if statement is still not complete and
we will discuss some of its variants in the
next tutorial also. Along with another
relevant statement, which is very frequently
used in Rust, called the Match statement.
That will be fun and exciting, so do come back for
learning that. And until next tutorial,
happy Rust Programming.
[No Audio]