Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share
Lecture Comments (3)

0 answers

Post by Andrew Showalter on February 13, 2013

Try a different browser.

1 answer

Last reply by: Matthew M.
Tue Feb 7, 2012 3:46 AM

Post by Massimo Raimondi on January 22, 2012

FYI, this exercise is misleading... it says if the number generated is EVEN output the number, if it is ODD then DO NOT output the number. meanwhile in the homework solutions it is the complete opposite...

as per the homework solutions this is what is outputted:

$randNum = 9
$randNum = 1
$randNum = 9
$randNum = 1
Because the do-while loop has exited, the number 5 has been generated.

More on Conditional Loops

  • A do-while loop functions exactly as a while loop except that its test condition is evaluated each time after its statement group is executed.
  • A continue statement is used to skip the remaining code in a loop and re-evaluate the loop’s test condition.
  • A break statement can also be used in while or do-while loops to immediately halt the execution of the loop.
  • Conditional loops whose test condition never evaluates to FALSE or never executes a break statement will loop indefinitely. This condition is known as an infinite loop .
  • Some common pitfalls when using conditional loops are:
    • counter initialization occurring within the loop
    • not incrementing a loop counter within the loop
    • having a necessary break statement be unreachable
  • Additional Resources:

More on Conditional Loops

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:08
    • Lesson Overview
  • do-while Loop 0:42
    • do-while Loop
    • Simple do-while Loop Example
    • Another do-while Loop Example
  • continue Statement 10:46
    • continue Statement
    • For Example
    • continue Statement Coding Example
  • break Statement Re-Visited 18:10
    • break Statement Re-Visited
    • break Statement In while Loops Example
  • Infinite Loops 23:26
    • Infinite Loops
    • Coding Example
  • Common Loop Pitfalls 25:59
    • Counter Initialization Occurs in Loop
    • Counter Not Incremented in Loop
    • Unreachable Break Statement
  • Coding Conventions 33:48
    • Do-while Statements Coding Conventions
  • Homework Challenge 34:28
    • Homework Challenge

Transcription: More on Conditional Loops

Hello again, and welcome back to's Introduction to PHP course.0000

In today's lesson, we are going to be continuing our discussion of conditional loops.0004

Specifically, we are going to be introducing a new style of conditional loops known as a do-while loop.0010

We are going to be talking about both the continue statement and revisiting the break statement,0017

and seeing how they apply for loops, including the while and the do-while loop.0024

We are going to talk about infinite loops and some common pitfalls that occur in creating infinite loops.0029

And we are going to talk about what infinite loops are.0036

And then, we are going to talk about coding conventions as they apply to the do-while loop.0038

So, the do-while loop essentially functions the same way as a while loop does.0044

The only difference is that, before entering a do-while loop for the first time, the test condition doesn't get evaluated.0049

In a do-while loop, as you can see down here, the test condition is at the bottom of the do-while loop, and it uses the same while keyword.0056

What happens when you enter a do-while loop: open entering, you execute the statement group within the do curly braces.0068

Then, after you have executed that, you evaluate the test condition specified here in the while statement.0078

If the test condition is still true, you continue, and you go ahead and evaluate the loop again.0086

The difference between this and the while loop is that the test condition is evaluated after the loop is run, as opposed to at the beginning.0091

What that implies is that, in a do-while loop, its statement group is always going to get executed at least once,0099

meaning you will always work through this group at least one time before you evaluate the test condition.0105

So, let's go ahead (let's erase this) and take a look at a script that makes use of the do-while loop.0113

Here is an extremely simple example: within the do statement group, we simply have an echo statement that says "in the do-while statement group."0121

So, when you enter this do-while loop, what you are going to do is go ahead and always execute the statements within the loop first.0133

Then, you are going to evaluate the test condition to see whether you should evaluate it again.0142

And in this case, for our while test condition, we have it set equal to false.0147

That, because the literal value is false, is always going to evaluate to false.0152

So, in this case, this do statement in the do statement group is only going to get executed once.0155

For example, if we go and run this file, we can see that "in the do-while statement group"--that string was only output one time.0160

Now, this is in contrast to...let's say we switch this around from a do loop to a while loop...0172

The do-while loop always has the statement group executed once; however, 0182

a while loop only has its statement executed if the condition is true on its initial run.0191

So, when we go ahead and run this script while false, the test condition false is always going to evaluate to false,0197

so this while loop is actually never going to get executed.0203

So, when we go ahead and refresh the page (looks like we had an extra curly brace), we are going to see that nothing gets output.0207

That is because this while loop never actually gets entered.0218

Now, one other thing to note about the syntax of the do-while loop is that 0224

the while keyword and its test condition come after the closing brace of the do statement group.0229

And then, also, you need to notice that there has to be a semicolon after the test condition for a while loop.0238

This really is an example of a do-while loop that doesn't do too much.0244

So, we'll create another example here that does something a little more interesting.0248

And it demonstrates an example of when you would use a do-while loop.0262

Because do-while loops always execute at least once, you use it in scenarios where you want something to be done once, 0266

and then if it doesn't work (the test condition evaluates, maybe, to false still) you continue to do it until things happen.0271

Well, a common thing in computer programming is: sometimes, if you have a server running, you need to bind to a particular port.0278

And so, what you do is: in your server application, you try to bind to a specific port.0285

And not all ports are available, so if you are successful with binding your particular port, let's say, your operation is done.0290

However, if you try to bind to (let's say) port 500, and you use some outside function to do that, and it returns false,0298

saying that you can't bind to that port, then you need to try again.0305

So, this is a perfect example of where we could use a do-while loop.0308

I have created this little mock function that might be used to bind to a particular port.0312

Basically, it takes one parameter that is an integer variable, and what it does is: if the value provided is greater than 10 and less than 900320

(so if it's between 10 and 90), then it is going to return the value that you passed in, which essentially is saying that it was successful.0332

If it is less than 10 or greater than 90, it is going to return null, which implies that it wasn't successful in binding to the port.0342

Now, a real network application would have more advanced stuff going on here,0351

but this is just a mock function to allow us to explore the do-while loop.0355

So basically, I'm going to create a variable outside of the do-while loop called randPort...excuse me, called boundPort.0361

I'm just going to set it equal to null to start off with.0378

And then, the way we are going to manage the do-while loop to exit out of it is by using a boolean flag.0381

We are going to say "isBoundTo," which means if the port was bound to it, then we are not going to execute the loop anymore.0388

We are going to initially set that to false.0395

Then, within our do-while loop, we are going to generate a random port number between 1 and 100, 0403

and we are going to use the rand function, as we had in a previous lecture.0408

And we are going to make use of our bindToPort function that we defined down here to try and see 0415

if we were able to successfully bind to a port, which would give us a condition to test.0429

So, we are going to pass it randPort, which is a randomly-generated port number between 1 and 100.0440

Now, if this function successfully binds to your port, it is going to return the integer that we pass into it.0446

It is going to return it as boundPort; if it fails, it is going to return the value null.0453

So, what we can do is: we can then create a test that says, "If boundPort is not equal to null," which means that it was successful,0459

"then we are going to go ahead and set the boolean flag isBoundTo equal to true."0473

And then, as our test condition, after we evaluate this do statement group for the first time, we are going to test if isBoundTo is true or not.0482

If isBoundTo was false (which means that this operation didn't succeed), then isBoundTo is still going to be false,0495

because that is what is was initialized to.0503

NOT false is true, so it is going to execute again.0505

However, if we were successful in binding to a port, boundPort is not going to be equal to null; isBoundTo is going to get set equal to true.0508

And then, NOT true is false, so the loop is going to exit.0515

And then, we are just going to afterwards enter a message that says "successfully bound to" and then the value of boundPort,0519

which is the variable that is returned by bindToPort.0533

So, we go ahead and run this example; we can see, it said it was successfully bound to port 27.0538

Now, what that means is: that do-while loop only had to execute one time; and we know that because...0546

Actually, one thing we should have done...I forgot to include a message that says "attempting to bind to port."0555

Actually, every time we go through the do-while loop, we are going to output a message that says, 0574

"We are attempting to bind to port randPort," which is the random port we generated.0577

Then, after we have successfully bound to port, then we output a successful message.0583

So when we go ahead and reload the page, we can see, it is attempting to bind to port 33, which means it was in that do loop.0586

It succeeded on the first time, and then we output a message "successfully bound to port 33."0594

Now, if we refresh the page the second worked on the first time...the third time it didn't work (and it looks like I forgot a break statement here...).0600

Because this rand function is not always going to return an integer between 10 and 90, 0611

sometimes this bindToPort function is going to fail; it is going to return null.0617

And then, we have to execute the do-while loop again.0621

So, this time, when I refreshed it, it successfully bound the first time; and we'll just do it a couple of times.0623

Now it took 2 times to actually bind to port: it tried to bind to port 1, but 1 is not between 10 and 90, so it didn't succeed.0631

When it generated the random port 85, that is between 10 and 90, so it successfully was able to bind to it.0639

Now, a continue statement is something that you can use within while and do-while loops, as well as other loop that we are going to get to within the course.0648

And what it allows you to do is: when you encounter a continue statement within a loop,0656

it skips the rest of the execution of the loop and goes back and re-evaluates the test condition.0662

For example, we have this while loop here: if this continue statement is executed, instead of going on 0667

and continuing the rest of the while statement group, what it does is actually 0674

jumps back up to the top here and re-evaluates the test condition.0678

If that test condition is true, it goes ahead and repeats executing the statement group for the while loop.0681

So here, we have set a variable that is initially equal to 1.0691

We are going to say, "While i," this variable, "is less than or equal to 10, it is going to perform these functions."0696

And at the end, we are going to increment it; so it is going to go through this loop 10 times.0701

And we are only going to output the value of i (which can be considered a counter) if it is an odd number.0706

The way we can do that is using the modulus operator.0714

We know, if you remember from the modulus operator, that it returns the remainder of integer division.0718

So, if we divide i by 2, and the remainder is equal to 0, that means it is an even number.0724

And because it is an even number, we don't want to output it, so before we leave the loop, we increment the counter0731

so that the loop will go through one more time, and then we execute the continue function, 0737

which is going to send it back up here to the beginning.0743

Every time this loop runs from the numbers 1 through 10--every time it encounters an even number--0746

it never reaches this section down here, because this statement ends up being true.0751

It hits the continue statement, and it bounces up to the top.0756

So, this loop is going to output just a string of numbers: 1, 3, 5, 7, 9.0758

Here is an example of continue in a more practical application.0768

We have defined a shopping cart, kind of like in our web application; it is a multidimensional array 0774

that contains associative arrays with two keys in them, one key being price, and one key being quantity, 0784

which represent the price and quantity of an item in a cart.0790

And what we are going to do is calculate the total of the cart, making use of the continue statement.0794

So, we initially define the total of the cart to be equal to 0.0798

And then, what we are going to do is, making use of the list and each functions that we had learned about,0803

we are going to loop over the cart array, which is the items in the cart.0822

And what this, again, is going to do is: every time this each function is run, 0826

it is going to extract the current key and value information of where the current array cursor is.0830

So, the first time it runs, it is going to extract information about the first element.0838

In this case, if we had had a value here, we could set that equal to the key, which--because this cart is actually a numeric array--would be 0.0843

But then, we want the value, and the value is always returned from the each function with the index 1.0852

So, the value is going to be this array right here; so we are going to call that a variable item.0860

And so, every time this loop runs, each is going to return an element of the cart array, which is going to be an associative array0865

for each item that has its price and quantity.0872

Now, what we are going to do is test to see, for each time this loop occurs, if the item's quantity is 0.0876

And the reason we are going to do that is because we don't need to perform any calculations if the quantity is 0.0886

We can just continue the loop and go on to the next item in the cart to process that and see whether that needs to be added to the total.0890

So, if item quantity equals 0, we can just type the continue statement.0898

And what that is going to do is: any time an item's quantity is 0, is just going to say, "OK, I don't need to add anything to the total."0914

"I'm going to jump back to the top of the loop and get the next item."0920

Now, if the quantity is not 0 (because continue skips the rest of the loop), the remaining statements are going to get executed.0923

We want to update the cart total, so we are going to use a similar math operation as we did in our web application,0931

which was simply going to be the item's price times its quantity.0939

And then, we are just going to output a statement saying that a particular item of a certain price and quantity was added to the cart.0950

Basically, we are just saying that so many items were purchased at a certain price.0992

I'll just double-check the syntax here.1001

And then, after the loop completes, which means we have processed all of the items in the cart, we are just going to output the total.1014

Let's go ahead and run this script.1034

And what we can see, if we go back and look at our array that we had set up--we have one item in the cart with a quantity 2 at a price of $5.00.1040

We have one item in the cart that has a price $10.00, but a quantity of 0, and then one item with a quantity 1 that has 25.1050

So, if we add up this total, it is going to be 2x5 (which is 10), plus 1 item at $25, which is going to be $35.1057

So, as you can see, it says "Two items were purchased at $5; one items were purchased at $25; final cart total equals $35."1064

And so here, you can see how we use the continue statement to avoid running this calculation, 1070

and also outputting this echo statement that says a particular item is purchased,1076

because you wouldn't need to output, in this case, "0 items were purchased."1080

You don't need to output that, so using the continue statement is a way to avoid doing that.1085

So now, we are going to talk about something we have talked about before when it came to the switch statement, which is the break statement.1093

And it is something that you can use in while loops and do-while loops to halt the execution of the loop.1098

Normally, as we have known do-while loops and while loops, their execution stops when their while condition evaluates to false.1106

Well, with the break statement, you actually can break out of the loop without having to have the while condition evaluate to false.1114

And what happens when you encounter a break statement: just like in a switch statement, 1124

the loop is going to exit immediately after a break statement is executed.1127

And what that means is that, once you hit a break statement (kind of like when we hit continue--1133

it doesn't execute anything else in the end of the statement; it jumps back up to the top)...1137

the difference is that, when you hit a break statement, it doesn't hit the rest of the loop.1140

But it jumps to the bottom of the loop, to whatever statement follows the loop.1145

The one thing to note is that, even in a while statement, let's say you have a boolean flag that we have been using,1152

and we set it equal to false somewhere within the middle of the loop.1158

Even though it is false (which is going to eventually make the test condition equal to false, which means the loop is going to stop),1162

everything below it still gets executed--all of the statements.1169

However, once you hit a break statement, it is like immediately saying that the test condition is false, and you jump out of the loop.1172

So, let's take a look at an example of that.1178

Here, we have a file called break.php, and what we are going to do is generate a random number each time we go through the while loop.1182

And if the number is equal to 5, we are going to break out of the loop; 1192

and if not, we are going to output the number that was generated on that iteration of the loop.1195

And then, at the end, we are going to output the number of tries that it took to generate the number 5.1201

We are going to actually run this loop "forever," because we are going to run it until the break statement occurs.1210

And you will see where the break statement is going to be in a second.1217

The first thing we will do is: we are just going to increment the numTries.1219

So, when we first enter this loop, we are going to know that number of tries is going to be incremented to 1.1222

Then, we are going to generate, using our rand function, a random number between 1 and 10.1231

And then, we run our test: if the random number we generated equals 5, then we want to break out of the loop.1242

The way we do that is by using the break statement.1256

So, if we come in here, and rand generates 5 on the first try, if randNum=5, it is going to equal true.1259

We are immediately going to break out of the loop; the remaining statements of the loop are not going to be executed.1267

And then, we are going to jump out to the end.1271

However, if randNum is not equal to 5, we are going to just echo a statement saying what random number was generated during this current loop iteration.1275

So again, this break statement...once it's encountered, it jumps immediately out of the loop.1298

Unlike continue, it doesn't jump back and evaluate the test condition; it jumps to the end.1303

And in doing so, it is going to bypass this last echo statement.1307

So, if the number is equal to 5, the number 5 is actually not going to be outputted; it is not going to say "number 5 was generated."1311

It is only going to say that for numbers not equal to 5.1318

And then, at the end, we are just going to output the number of tries it took to find the number 5.1320

So, if we go ahead and run this script, we can see that it took (actually, let me make this a little more readable)...1337

Let's run it again; it is going to generate different random numbers.1360

Here, it actually took several times to generate the number 5; it took 9 tries.1364

Here, you can see, 1, 2, 3, 4, 5, 6...the first 8 times, it didn't work; and then, on the ninth time, it finally generated the number 5.1368

And so, you can see that, on the times that it didn't generate the number 5, this break statement was not executed.1381

It was only executed when num equals 5, and because of the nature of the random function, 1388

this is going to operate a different number of times, depending on what the rand function generates.1391

So, next time we run it, it took three tries to generate the number 5.1398

So, that is an example of using the break statement.1402

I want to talk now, just quickly, about infinite loops.1408

And basically, what an infinite loop is, is when you have a loop sort of running out of control.1411

It is a loop where the test is never possible to set it equal to false, or it never reaches false.1415

Because we know, in the absence of a break statement, that a while loop or a do-while loop is only going to stop executing1422

once its test condition equals false, if that never happens, the loop is going to keep executing indefinitely.1429

As we just learned, there is a break statement, also, that we can use to break out of a loop.1434

So, an infinite loop occurs if either the test condition of a loop never equals false, or a break statement never happens.1437

Like in our last example, the test condition was always true; so the loop is going to loop indefinitely until the break statement occurs.1446

So, an infinite loop will occur if test condition never equals false, and if a break statement never occurs.1451

That is known as an infinite loop, because it just keeps looping over and over again, and it actually jams up the computer and stalls the program.1460

As the programmer, it is up to you to ensure that all your while loops or do-while loops end,1468

by ensuring that either a test condition will eventually equal false at some point (which is by incrementing a counter1475

that eventually is going to reach some maximum value that it says, if it is greater than this value, to exit the loop),1482

or at some point a break statement will have to be eventually executed within the loop.1491

If we go take a look at a file here called infiniteLoops, this is an example of a simple infinite loop.1498

It is a while statement where the test condition is always true, and we have no break statements in the loop,1503

and nothing that alters this test condition, because the test condition is a literal.1509

So, all it is going to do is repeat this loop over and over again.1512

It is going to say, "While true, OK, let me echo 'stuck in loop.'"1515

And then, it is going to reevaluate the test condition "while true"; that is true; it is going to echo it again.1519

So, it is just going to do it over and over again, and it is going to halt the execution of the program.1523

If we go and try to load this page, it is going to take a while to load; and eventually, the PHP just gives up.1529

But if you look, we can scroll down here and see: it just continually outputs "stuck in loop" over and over again.1538

I'm going to go ahead and stop that; that is an example of what an infinite loop is.1545

And the reason you want to avoid these is because it halts the execution of your program,1550

because your program gets stuck in this one section of code.1553

Now, some common loop pitfalls that occur, that enable an infinite loop to happen, are:1560

if you are using a counter, and you initialize the counter within the loop (which we are going to talk about in a second);1566

if you have a loop counter, but you forget to increment it each time you go through the loop;1573

or if your loop only exits upon a break statement, and your break statement is unreachable.1578

Let's take a look at each of these different scenarios.1584

I'll go ahead and delete this while loop.1588

The first one is that counter initialization occurs in a loop.1592

We have a while loop with a counter, and we say while the counter is less than 5, 1597

we want to echo the counter; we want to output the value of the counter.1609

So, we are going to have a simple echo statement in here.1615

And then, on each iteration of the loop, we increment it.1625

So, when this occurs, i is going to equal 0; the first time it goes through, 0 is less than 5; OK, it is going to output 0.1629

It is going to increment it to 1, and it is going to repeat.1635

And what is going to happen is: 0 through 4 is going to be output.1638

So, if we go and look at this page now, we can see, 0, 1, 2, 3, 4 is output.1642

However, let's say we accidentally (and this is something that can happen) put the counter initialization 1648

(setting it equal to 0) within the loop, instead of doing it before the while loop.1656

OK, so what is going to happen? First of all, it is going to give an error here when it says that i is undefined.1660

But let's say, actually, we had defined it out here, and we said i equals 0, so we don't get an undefined error.1669

It is going to say 0 is less than 5; and then, what it is going to do is: the first thing it does is initializes the variable i; so, it is going to set i equal to 0.1677

It is going to output 0; it is going to increment i equal to 1; it is going to go back up to the loop.1685

It is going to say, "1 is less than 5; OK, let me enter the loop," and it is going to reset the value of i equal to 1.1690

In this case, the initialization of the counter occurs at the beginning of each loop, so this loop is going to run indefinitely.1696

So, if we go ahead and run this script, we can see that it just keeps repeating "0...0..." over and over again.1702

And the reason is because, every time this loop is entered, i equals 0.1714

That is one problem that can happen...actually, that froze up the browser, and that is one of the things that can happen with infinite loops.1718

So, let's remove this; we have the counter initialization outside of the loop.1735

But let's say we forget to increment the counter during the loop.1743

What is going to happen? We have i=0 when we enter the loop; 0 is less than 5; it is going to say, "OK, I am going to echo i."1747

i is still equal to 0; when it goes back up here, it is going to say 0 is less than 5; OK, it enters the loop--it is going to output 0 again.1755

It is going to get stuck in this loop indefinitely.1761

I'll just output the title for this one.1767

This is where the counter does not get incremented in the loop, as it is supposed to be.1773

If we reload our browser...Firefox sometimes tries to reload the last pages that you had up, so let's close that tab and open a new tab.1778

I'm going to run infiniteLoops, but I am going to do it quickly and stop it.1806

We can see that, without the counter being initialized, that when this page loads, it is just going to output 0 over and over again.1810

Actually, if it even works at all...that is one of the problems with infinite loops.1823

In fact, this page isn't loading: it is saying that it is getting stuck in this infinite loop...and actually, there it goes; let me stop it.1826

It is going to say the counter was not incremented in the loop, so you keep getting 0 over and over again.1833

That is an example of that type of infinite loop.1836

And then, another common thing that happens is if you have an unreachable break statement.1839

For example, let's say you have a while loop, like we had in our last file, break.php, 1852

that the test condition is always true, and it only exits on a break statement.1859

So, while true, this is going to be incremented over and over again; so let's generate a random number between 1 and 10.1875

And then, we are going to have a test condition, just like we did before.1893

If randNum=5, we are going to break out of the loop; if not, we are going to echo randNum.1896

When we run this, it is going to work just like our last program did; it is going to output random number until it reaches 5,1919

and then it is going to break out of the loop and output this statement that I had included to the outside loop.1926

However, let's say we coded incorrectly, and we made this break statement unreachable.1933

One way you could do that is: let's say we had two conditions: randNum=5 and another condition.1938

And for another condition, let's say that it always evaluates to false.1946

In this case, we will directly set the literal value to the literal value false.1950

So, we know anything "AND"-ed with false is always going to equal false; therefore, this if statement is never going to get executed,1955

so break is never going to get executed: you are going to get stuck 1959

in this infinite loop, outputting random numbers over and over again.1962

If we go and reload the page again, and let it work for a little while, hopefully it will generate some output.1966

That is the problem with infinite loops.1988

So, it is stuck in this infinite loop, and because it was stuck there, it is not even generating any output.2000

The behavior becomes unpredictable, because it clogs up the computer's CPU.2006

And actually, here we go: it has the output, and you can see, it is just a bunch of randomly-generated numbers,2010

output over and over and over again, infinitely, until the computer memory runs out.2014

Those are some of the common pitfalls that will cause infinite loops to occur.2021

Just really quickly, I want to talk about coding conventions as they relate to do-while statements.2030

You have the do keyword, followed by a space, and then the opening curly brace.2036

You have the statement group for a do statement indented one indent further than the keyword.2040

And then, you have the closing curly brace on its own line, indented the same level as do.2045

And in between the closing curly brace and the while keyword, there is a space.2050

Just like in the while statement, there is a space between the while keyword and the test condition within parentheses.2055

And then, you always have the semicolon, here at the end, to signify to PHP that you are done with the do-while loop.2061

So now, just quickly, I want to go over the homework challenge for this course.2069

I would like you to create a do-while loop that generates a random number (using the rand function) between 1 and 10.2072

It does that on each iteration through the loop.2080

If the number generated is even, I want you to output the number.2083

If the number generated is odd, I don't want you to output the number; I just want you to start the loop over again.2086

If the number is 5, I want you to break out of the loop (this is the word break),2091

and then output a message stating that the number 5 had been generated.2095

And this example is going to give you practice; your solution should use both continue and break statements, which we learned about.2099

And it should have a test condition that is always equal to true.2107

What that means is that you are going to have to correctly include a break statement 2110

that is reachable within your code in order to break out of that loop.2113

And in this case, the break statement is going to occur when the random number generated is equal to 5.2117

So, you need to make sure that your test condition is appropriately set up, so that that break statement will work.2122

And then, you use the continue statement to decide whether to output a number, based on whether it is odd or even.2128

And then, just verify that it works, as appropriate.2137

And remember to format your code according to the coding conventions we just talked about, as it comes to do-while loops.2140

That ends today's lesson; thank you for watching look forward to seeing you next time.2146