[No Audio]
In this tutorial, we will be covering two
important concepts in connection with loops,
they are called break and continue statements.
Again you will find similar and
equivalent concepts in almost all the
programming languages. We will cover the
break statement first, so let's get started.
First I will write some suitable comments.
[No Audio]
Okay, the break statement is inside the loops
for stopping the loop execution. When a break
statement is encountered inside a loop, the
loop is immediately terminated, and the
program control resumes at the next statement
following the loop body. Let us explain this
by having a simple example. Suppose, I want
to write a program, which given a certain
number typically greater than or equal to 100,
will compute the smallest number lesser than
the given number, which is divisible by 13.
Of course, there are many ways in which this
program may be coded, but I will code it with
the help of simple loop. Again, this is just
for the sake of illustration, and the solution
may not be the optimal solution, but the
intent here is to learn the usage of break.
So I will first declare a variable, which
contains the given number. So, I will write
let mut, and then var, and I will make it
equal to let's say a value of 100. Next, I
will write a simple loop by writing, loop
followed by two curly brackets. Inside the
loop, I will first deincrement the value of
the variable var by one, because we are
looking for a number which is strictly less
than the current number, which is divisible by 13.
So, I will write a var = var - 1.
Next, I will check if the number
is divisible by 13 or not. So, I will write
var and then % sign, which is a mod operator,
and then 13 == 0.
Now, if the if statement is true, it will
mean, that we need to exit from the loop,
because we have found our desired number.
To exit from the loop, we will use a break
statement, inside the body of the if statement
by writing a break. In case the condition is
not true, we will continue with the next
iteration, where again, the variable var will
be reduced by a value of one, and the
condition will be checked again. Outside the
body of the loop, I will display the number
which I may found inside the loop by writing
println, followed by exclamation mark, and
then within the parentheses, I will put two
quotes where I will write, The highest number
lesser than the given number divisible by 13
is, curly brackets, var.
The break statement inside the if statement will
terminate the loop, once it is encountered,
and it will shift the control to the first
statement outside the body of the loop,
which will be the print statement. So let us cargo run this.
Yes, the correct answer, which is 91,
is being displayed to us. Again please note
that the break will move the control to the
first statement after the body of the loop.
Let us now learn the second topic of today's lecture.
I will add suitable comments first,
and will comment out the code that we have
written so far.
[No Audio]
The continue statement skips the current
iteration of the loop and continues with the
next iteration. When the program encounters
the continue, the continue statement, all
the statements beneath the continue
statement will be skipped, and the next
iteration of the loop will be executed.
Let us do a simple example to illustrate the
use of continue. I will write a simple
program in which I want to compute the first
three numbers with the property, that they are
divisible by both 3 and 5. The program
will start with the number 0, and will
keep on incrementing it by a value of one, and
keep on checking for the property. We know
that the first such number will be the number 15,
and the second number is 30, and the third
number is 45. When the first three numbers
are found with the proposed property, the
program should stop processing further
numbers. Let us start implementing this program.
I will start by declaring a variable
var with a value of zero, so I will write
let mut var: i32 = 0.
This variable will be incremented sequentially and
will be checked for the required property.
Next, I will declare another variable, which
will keep on counting the numbers with the
property. So, I will write let mut count : i32 = 0.
Next, I will declare an
infinite loop by writing, loop followed by curly brackets.
During each iteration of the
loop, I will first increment the variable
var by one, so I will write var = var +1
Next, I will check if the
current number under investigation has the
property or not, so I will write var %
sign, followed by a value of 5 == 0.
And then since I am
looking for another condition also, so I
will write &&, and then
followed by var % 3 == 0.
Inside the if body, I will add a
print statement by writing println
followed by exclamation mark, and then
within the parentheses, I will put two quotes
where I will write the number which is
divisible by both 3 and 5 equals to, curly brackets,
and then followed by /, var.
This statement will be
printed each time I found the number with the
property. Next, inside the same if body, I will
add one to the value of count, by writing
count = count +1. Next, inside
the if statement, I will check if the value
of the count is equal to 3 or not,
because when it reaches the value of 3,
I want to quit the program, because I intend to
find the first 3 numbers with the property.
So, I will write, if count == 3,
and then within the curly
brackets, I will write break. Next,
outside the body of the outer if statement, I
will add a simple print statement which will
print the current number under investigation
by writing println
exclamation mark. And then within the
parentheses, I will put two quotes, and inside
the two quotes, I will write curly brackets, var.
This means that, during each
iteration of the program, I will first
increment a variable var, and then we'll
check the condition. And if the condition is
satisfied, I will print this message followed
by this print, and then at this condition.
Let us cargo run this program, and then we
will refine its output by making use of the
continue statement. Okay, you may note that we
have the number that is being printed. And
when the condition is being met, we have the
special output afterwards, the program again
starts to print the numbers starting from the
value of 15. This looks a bit bad, because we
already have a special print statement for
the value of 15. Now, we want the program to
skip printing the value of 15, and in the same
way, after the value of 30. Let us see how we
can skip printing the values of 15 and 30.
The if statement we see that, when the
count == 3, the loop is being terminated.
However, when the count is
not equal to three, then the loop continues
with the remaining statements beneath the if statement.
That is, it keep on executing the
statement of the print statement. If we write
after the if statement, else, and then within
the curly brackets, continue, the program will
not continue with the remaining of the
statements in the loop, and will not print the number.
This means that, when the outer if
statement is true, the inner if will get a chance.
If in that case, the inner if is true,
the loop will terminate. However, in case
when it is not true, the else part will be
executed, which will skip the execution of the
current iteration, thereby, avoiding the print
statement beneath it to get executed, and will
shift the control to the next iteration.
Let us cargo run to check, if the desired output
is being achieved by making use of the
continue statement. Yes, we may note that, the
value of 15 and 30 are not part of the
printed values anymore. In some sense,
the continue has some similarities with the break.
However, instead of forcing termination
of the entire loop, it forces the next
iteration of the loop to take place,
skipping any code coming after the continue in the
same iteration. There is a special use of the break,
when used in association with loops.
It can also be used to return a value from
that loop. This is only valid with simple loops,
and not with any other type of loops,
such as for loops or while loops. For
instance, if in the same program, I want to
return the third value with the property, that
it is divisible by both 3 and 5.
So I will write something like, something like,
let req_number = loop at the top.
Next, when I use the break,
which basically exits from the loop, I will
mention the variable name, which I want to
assign to the req_number,
which in this case will be the variable of var.
So I will write break followed by the
variable name var. Outside the body of the
loop, I will add semicolon. Let us add a
print statement outside the body of the loop
for displaying the value of the variable
req_number, by writing println
followed by a exclamation mark, followed
by parentheses, and then within the two quotes
I will write the required third highest
number divisible by both 3 and 5 is,
curly brackets, req_number.
Let me cargo run this, and we may note that
the variable value has been correctly
displayed. Please note that, this is a special
use of the break, which can only be used when
the break is used in connection with simple loops.
Also note, that in the same way, is used
with simple loops. The two constructs of
break and continue may be used in combination
with the for loops and while loops. The
essential working is the same as explained,
and the behavior will not change. Okay, that
brings us to the end of this tutorial. I hope
you would have enjoyed this. That also brings
us to the end of this section, and now in the
new section we will be coming up with more
interesting concepts. So do come back to cover
those. And until next tutorial, happy Rust programming.
[No Audio]