3.9 While Loops in Java
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
Hi and welcome back to the course.
00:02
Now I hope you guys are doing well. And if you remember, in the last lab, we went over a program called the Month guesser in which we implemented the idea and concept of switch statements in Java.
00:16
Now, in this video lecture, we will be going over a another type of control floor structure called the Wild Loop.
00:25
All right, so, as always, I've listed out some learning objectives for us a foe.
00:30
The first thing that we're gonna be aiming to understand is what a wild loop essentially is and what it does.
00:38
Then we're gonna move on to discussing a specific variant of the regular wild loop called the Do a Loop.
00:47
And finally, we will be focusing on understanding the syntax or grammar of wild loops so that we are able to interpret an example or any example, as a matter of fact.
01:00
All right, So what is a while, Luke, To begin
01:03
now, here is the formal definition of a wallop.
01:07
The wild statement continually executes a block of statements. While a particular condition is true,
01:12
the value that the wild evaluates must return a Boolean value. I remember, a bullion value can either be true or false
01:21
if the expression evaluates to true. The Wild statement executes a statement in the wild block.
01:27
The wild statement continues, testing the expression and executing its block until the expression evaluates to false.
01:34
Now, in order to understand what we're talking about here, let's have a look at the structure of a wild loop.
01:41
All right, Perfect. So here I have
01:44
any picture of the structure of a while. Luke, you start writing your wire loop with the walk you word, and this is the expression that the definition is talking about. This expression were either result in true or in force,
01:57
and as long as a result in true, these statements in the block of code will execute and will print or do whatever they're supposed to do.
02:06
But as soon as his expression results to false, that's when the Wild Luke will end.
02:13
Now let's look at the do a loop.
02:15
So here we have a picture of the structure of a do a loop.
02:20
Unlike the wild loop, you start the duel of wild loop with the keyword do and your regular curly braces with your code inside this block of code your statements
02:30
and then you have the wild expression.
02:32
Now, the main difference between a while and do a loop is that a do a loop
02:38
is essentially the same as a wild loop. But the difference is that it executes the statements here inside the block of code at least once before we continue to check for the condition in the wild statement here.
02:54
All right, so let's go on and look at an example of a wire loop.
03:01
Here we have a class called Wild Demo.
03:05
Now, in Wild Emma, we have our regular public static. Boyd mates are a main method,
03:10
and then we have defined and we have declared an initialized a variable called count. Now count is
03:19
set to one,
03:21
and then we have our wild
03:23
are while loop is checking. If the count is less than 11
03:28
and if it happens to be less than 11 then we print out count is plus the count. So the numbers stored in count and after this print statement is executed, that is when we increments are count variable.
03:44
So let's go through the first or second and third integration of the swat loop
03:49
nowhere. So we're gonna start at counter being one.
03:52
So we would say while count.
03:54
So So while one is less than 11
03:57
that is true because one is less than 11. Print count is one increment. One,
04:03
22
04:04
So the value of one is now implemented to the value of two.
04:09
Now count stores the number two.
04:12
Then we move on with the second iteration.
04:15
So now we say while two is less than 11 print count is too incremental value of the count to three.
04:23
Now, this will continue looping until we get to a condition
04:28
that,
04:29
uh, does not satisfy the wild expression over here.
04:32
Now, the first time this would happen
04:35
is when we reach at
04:39
11. So when count is 11 11 is not less than 11. Therefore, this statement over here, the prince statement and the block of code inside this world, while Luke will not execute, and that's when the wild loop will end because it does not satisfy the expression
04:57
inside the wild loop.
05:00
All right, so now let's look at a do a loop.
05:03
So we have a class called do a Loop with our main method. Then again. We have the count variable.
05:10
And in this case,
05:11
we have the same print statement with the same implementation for the count Variable.
05:15
But what's happening here is that
05:17
this will be executed once
05:20
before the wild statement is evaluated. So the first iteration of this loop will be count is
05:31
plus one. So we will print out to the consul or to the ER
05:35
to the start to the terminal. Whatever. Maybe we will print out one
05:41
because we're gonna evaluate three condition after it is after these statements in a block of code are executed at least ones.
05:48
And when we move on to the second iteration,
05:51
that is when we will start evaluating our while. Expression over here.
06:00
All right, So to wrap up this video lecture, we discussed What? Why? Loops are
06:05
the do I loop
06:08
and as well as an example of a wild loop. And they do a loop where we saw the syntax of how these control floor structures work.
06:19
Okay, so I hope you guys enjoyed the video. And in the next lab, we will be going over a number guesser where we will be implementing our newly learned concept of wild loops.
06:34
So, as always, if you guys have any questions at all, feel free to contact me via Lincoln. And I would be more than happy to answer any of your questions. Thank you very much for watching, and I'll see you in the lab falling the City Electric.
Up Next
Similar Content