[No Audio]
Loops are one of the fundamental programming
concept, and is present in almost all the
programming languages. They help us in
executing one or more statements up to a
desired number of times. It is a sequence of
instructions or programming statements, which
may be referred to as code blocks, that is
continually repeated until a certain
condition is reached. The condition may be
based on some predetermined number or times
of execution, or some expected result is being
produced, or some expected behavior is being
achieved or reached. So let's start learning the loops.
The simplest form is one which has
no associated condition. To have such a loop,
we will write loop followed by parentheses.
This means that, whatever we include inside
the parentheses, it will be executed infinite times.
Let me add a print statement to the
body of the loop, let us execute now.
[No Audio]
You may note that, the print statement is
executing infinite times, and it keeps on
printing the print statement. Ctrl + C is
used to stop an infinite loop,
so let me stop it.
[No Audio]
Generally speaking, you may not
frequently need this type of loop inside your code.
Such kind of loops are rarely
being used by programmers, even if they intend
to use, they will typically have some
statement inside the body of the loops,
which quits from the body of the
loop, once a certain condition is being reached.
Now, let us look at some more useful
kinds of loops. There are mainly two kinds of
loops apart from the one that we have just
seen, they are known as while loops and for
loops. We will first look at the details of
the while loop. A while loop is used to
repeat a specific block of code in unknown
number of times, until a condition is met.
For example, if we want to ask the user to guess
a number between 1 and 20, we don't know
how many times the user may enter a number
before he can guess the number correctly.
He may do it in the first attempt, or in the
second attempt, or he may take more than two
attempts, the user will keep on guessing
until he accurately guessed the number.
Let us implement this guessing number using the
while loops. I will start by having a
variable of my_number, and we'll
assign it a value of 5.
[No Audio]
Next, I will define a boolean variable and initialize
it from a value of false.
[No Audio]
I will set this variable to true, whenever the
user correctly guess the number, and at that
point I will quit from the loop. I will ask
the user to guess my number.
[No Audio]
Next, I will write a while loop. A while loop
is indicated by the keyword of while followed
by a suitable condition to check for
terminating the loop. As long as the
condition remains true, the loop body will
execute, and as soon as the condition becomes
false, so the loop body will terminate.
In this case, I will terminate the loop when the
guess becomes true.
[No Audio]
At the start of the while loop, this condition
is true, because guess has a value of false,
and false is not equal to true. So therefore,
the condition evaluates to a value of true.
Inside the loop, I will take the user input
and we'll convert it to u8 type.
[No Audio]
If the user input is equal to my_number,
so, I will change the boolean
variable guess to that of true, and if that
is not the case, so I will print a message to
the user to try again.
[No Audio]
Now, what will happen that, the loop will be
executed many times, each time the condition
at the start of the loop will be checked.
The loop will continue to run the code segment, as
long as the condition remains true. When the
condition becomes false, the loop will be
terminated, so let us execute this.
[No Audio]
Ok so I will enter 15, it says try again.
Let me enter 13, and it will say try again.
And now I will enter the correct number, which
is my guess, so I will enter 5. And now it
says, that the guess is correct. And at this
time the loop has also terminated, because the
condition at the start of the while loop
evaluates to false.
Let us see some more examples.
I want the user to enter a number,
and I will tell the user the next number,
following his number, which is divisible by both
2 and 5. Let us implement a simple program.
First I will display a useful
message to the user, so that he knows what the
program is expecting as an input.
[No Audio]
Next I will take the user input.
[No Audio]
I will declare a bool variable, which will be
used to test the condition of the while loop,
initially, it will be set to false value.
[No Audio]
I will next add the while loop, along with a
bool variable for checking when to exit from the loop.
[No Audio]
Inside the body of the loop, I will increment
the value of the number by one.
[No Audio]
Another way of writing the same statement is,
to write number += 1 which
essentially has the same meaning. I will next
check if the number is, divisible_by_2 _5.
The mod will be used for checking this
condition, if the remainder of the number
after dividing by 2 and 5 are 0, so
this means that the number is divisible by both 2 and 5.
[No Audio]
Inside the if statement, I will add a suitable message.
[No Audio]
Next, I will also update the boolean variable value.
[No Audio]
This will enable termination from the while
loop, when next time the condition is
being checked. Okay we are done with the code,
so let's execute.
[No Audio]
I will provide the input of, let's say 12, and
it gives me the next number, which is
divisible_by_2 _5. Please note
that, we can further refine this code and get
rid of the extra variable, that is divisible_by_2 _5
In fact we may note that,
the if condition is basically
controlling the execution of the while loop.
So instead of checking this with the if
statement, we can check it at the top, in
front of the while loop. I will cut this
condition from here, and we'll paste it in
front of the while loop.
[No Audio]
I will delete the variable divisible_by_2 _5,
because we will not need it anymore.
[No Audio]
Finally, I will bring the print
statement outside the body of the while loop,
because I do not want to execute it many times,
rather I would like to execute it when
we have found the desired number.
[No Audio]
Now you may note that, our code is more compact
and short. In summary, when the execution of
the loop is dependent on some condition
inside the loop, then it may be sometimes added
to the condition of the loop itself.
Let us execute to check, if everything has been
coded correctly or not.
[No Audio]
I will use the input of let's say 11, and
it gave me the correct result. There is
however a small computation error in this code.
If I run again this code, then it will
return to me the same number which is not,
which is not a number, after my number is
indicated by the description of the program,
and indicated in the print statement also.
To import this program to always check for the
number after my number, I will add 1 to the
number when while loop starts.
[No Audio]
Now it will give me the correct answer.
Please note that, in the previous code, this
computation error was not there. Because at
the start of the loop, before checking the
condition, we updated the variable number and
adds 1 to its value. A key point to notice
that, we should pay special attention to the
variable values at the start and ending parts
of the loop. That is when the loop starts and
when the loop terminates.
Majority of the times the
error in our code happens at these
point, and we have to be very careful about these.
Okay, one final note before we end this tutorial,
we have pointed out that the
while loop is generally being used in
situations, where we do not know in advance
the number of times we want to execute a code block.
This however, does not mean that, we do
not, that we cannot use it in situations
where we know the actual number of times we
want to execute a code block.
However, for that, we should typically use the for loops,
which will be covered in the next tutorial.
Okay, that brings us to the end of this
tutorial. In the upcoming tutorial, we will be
looking at a for loop with some interesting examples.
Do come back for covering that and
until next tutorial. Happy Rust programming.
[No Audio]