Let's now talk about loops which are easily one of the
most important aspects of any kind of program or programming language.
What exactly are they? Well, they are used typically to repeat a block of code
multiple times
until a specified condition is met or
for a set number of iterations.
In other words, a loop will run until all items in a sequence or list
have been processed,
or
the loop will continue to run while a condition
continues to be true or is satisfied.
So basically, it's a loop,
it'll keep going on and on and on and on until something eventually happens.
Now,
they are very important because they help us to automate repetitive tasks.
So instead of us having to repeat the same task over and over again,
we can just write a loop that will run that task force automatically.
And of course, we use loops for building efficient programs
that will adapt based on dynamic conditions.
And when it comes to loops, there are two main types.
The first one is going to be the for
loop.
Look at this, we have
a list of
129 OK numbers
and we can write a very simple program that will say, hey,
print
each number out. So basically print 123456789.
But instead of us having to write
each individual line for each individual numbers, a
print one second line, print two third line, print three,
we can just write a loop that will go through all the numbers
in this list and then print out those numbers until of course,
the last number has been printed, which is nine.
This will be the definition of an example of a for
loop.
The second one
is the while
loop.
So say for example, the time is between
12:01 p.m.
and 4:59 p.m.
So as long as the time of the day is between this range, say good
notice the difference between the two structures. OK.
The first one here,
which is the fold loop already has like a predefined list or sequence.
And as long as we have items in that particular list, then do something.
The while loop is a little bit different because it says, OK,
as long as this condition continues to be
satisfied or this condition continues to be true.
What is the condition here?
As long as the time is between 12 to 1 pm and 4:59 p.m. Say good afternoon.
Alright.
In closing
the loops must have an end point or will run forever.
Basically, a
loop can crush the entire program.
Let's look at the example in here,
what if the numbers in this list was number one to infinity,
right?
That means
the program will continue to print the numbers over and
over and over again and it will never stop,
this will easily crash the program.
And what if in this example right now
there is no specified time period.
What if the program just checks to see if it's doing
the day or any time during the night, just say good afternoon.
Well, it's always going to simply print out good afternoon, good afternoon,
good afternoon, good afternoon.
And it will eventually crash the program as well. So
it is very, very important that whenever we're working with loops,
there must always be a certain kind of condition
that will eventually be satisfied for that loop to end.
Otherwise it will crush the program.
Thank you for watching the video. I will see you in the next class.