Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
We learned that loops can control how many times your code runs.
And there are two types of loops, the loop, which runs code a specific number of times, and the wire
loop, which runs code as long as something is true.
Now, the for loop is good if you know in advance how many times something should run.
And if you don't, then use a while loop.
So in this lesson, you're going to understand when to use for loops and when to use while loops on
top of learning how a while loop works.
First things first.
Inside your Section five folder, make a new class name the while loop Java.
And here it has the main method.
The while loop keeps running code, as long as something is true, the while loop is good.
If you don't know in advance how many times a loop should run, because unlike the for loop a while,
Loop doesn't have any fancy counters to keep track of how many times the code ran.
It's just going to keep running the code as long as some condition remains true.
So it stands to reason that the only thing a while loop needs is a condition and it's going to keep
running until that condition turns false.
Let's see how this works.
In the world of class, I'm going to define a variable that stores a random integer and number is going
to equal twenty five.
Then I'm going to make a while loop, and this loop is going to keep running as long as that number,
which we just defined is less than or equal to 30, as long as this condition remains true, this while
loop is going to keep running and every time it runs, we're going to print that number.
OK, I'm going to run the code.
And to no surprise, the code runs forever.
Now, don't panic, we can start the program by pressing control, see the supplies, the both Mac and
Windows, and if you're using a Mac, it's control C, not command C, so that's really important.
Now, the reason why this happens is because this condition that you see here, a number is smaller
than or equal to 30.
It's always going to be true because the number variable equals twenty five, which is smaller than
30.
And we're not doing anything to change that.
So this condition is always going to be true and the loop is going to keep running forever.
OK, so we need to fix our loop in that every time it runs, it needs to update the number variable
by one and then eventually the variable is going to increase to 31, which will cause this condition
to turn false and break the loop.
So I'll run my code.
And as I predicted, the while loop keeps running as long as the number is smaller than or equal to
30, beyond which the condition turns false and the loop breaks.
So from a diagram perspective, the loop runs six times.
First, the number is twenty five.
The condition is true.
So the loop runs in.
During each run we're increasing the number by one in our condition is going to remain true until the
sixth run.
Because in the sixth run, the number is going to increase to thirty one, the condition turning false
and the loop breaks.
Now, you know me, I like to visualize things from a runtime perspective, so this is how Java runs
the wire loop first.
The number is twenty five.
So the condition is true.
Prince twenty five and then inside the wire, updating the number to twenty six.
Now the code inside the wire loop is done, but the condition is still true.
It's going to keep printing and updating the number again and again.
Now I'm just going to let this play out until we get to the sixth run.
And here the number increases to thirty one, the condition turns false and the while loop breaks.
Now, you might be asking yourself, shouldn't I have used the for loop instead?
Yes, yes, you should have.
The while loop that you created is very specific, and when you do run code a specific number of times,
you need to use a for loop.
In other words, when you know in advance how many times a piece of code needs to run, then use a for
loop because the for loop combines the counter condition and increment in one line.
If you ask me, it's a lot cleaner and the for loop is more compact.
It's much easier to read and it was designed specifically for this purpose.
So back in our code, we're going to reshape this into a for loop.
We can put the start here.
We can put the stop condition.
And here we can put the step, the increment.
And the fact that we can rewrite this while loop is a for loop so easily means that we shouldn't have
been using a while loop to begin with.
So I'm going to rerun the code.
And we get the same output, but our code looks much nicer.
Counter starts at 25 minute increments by one after each run, and it's going to keep running until
I reach thirty one.
At this point, the condition is false and the loop breaks.
So when do I use a while loop, use a while loop, if it's not clear how many times the code should
run, it's as simple as that.
Let me give you an example.
Delete the fallout, because this is a lesson on wire loops and I'm going to make two decimal variables.
The first variable is some random number that you choose.
Double choice is equal to zero point zero one.
And I'm going to make another variable guess which for now is a random guess that I'm going to set to
zero point nine nine.
Now, I'm going to define a while, this while loop is going to keep running as long as gas is bigger
than choice.
In which case, I'm going to print hi, I'm in the wire loop.
Rerunning my code.
And to no surprise, the code runs forever because guess is bigger than choice, so the condition is
always going to be true and the code is always going to run because we're not modifying guests in any
way.
So what I'm going to do is press control, see the stop the output once again, this applies to both
Mac and Windows.
Mac users do not press command, see press control, see.
Now, back in my code, what I can do is inside the wire loop set guests equal to Mathoura random.
And instead of printing the string, I'm going to print the gas variable so that you can see exactly
what's going on.
And so basically what I'm doing is the computer is going to keep guessing random numbers until it comes
up with a number that's smaller than the choice which will turn the looping condition false.
And when is that going to happen?
We don't really know.
The while loop is going to have to keep running while this condition is true and the loop is only going
to break in the event that it comes up with a guess that's smaller than the number that you chose and
that's going to be random.
So we don't know when this loop is going to break, but let's just run our code.
And check it out, our code keeps guessing and no and after many, many loops, after many, many iterations,
it finally guesses a number that smaller than zero point zero one.
In this case, zero point zero zero seven and the loop breaks.
I'd like for you to be able to visualize this, so once again, here we have our condition, which starts
off true because the guess is bigger than choice.
So it keeps running.
And after many, many loops, eventually it's going to guess a number that's smaller than choice.
As soon as it does that, the condition is going to turn false and the loop is going to break.
Now, the one thing I want you to take away from this is that it's impossible to know in advance how
many times this code needs to run before Java comes up with a good guess.
So this is a perfect use case for while loops, because unlike the for loop, a wire loop doesn't use
counters to keep track of how many times each chunk of code needs to run.
It's just going to keep running the code as long as some condition remains true.
So remember, these rules use a for loop.
If you know in advance how many times you need to run a piece of code and use a while loop to run code
as long as something is true.
In this video, you learn how to use a while loop and the while loop will keep running as long as some
condition is true.
First, you call it a wire loop that runs code exactly six times.
But when you need to run code a specific number of times, a for loop is much better.
It combines the start, the stop and the step in one line.
It's more compact and it's much easier to read.
But when you don't know how many times a piece of code needs to run, using a for loop is impossible
because inside of a for loop, you need to specify exactly how many times it needs to run.
So you needed to find a while loop that keeps trying to guess a number that's smaller than yours.
It's impossible to know in advance how many times this code needs to run.
So this was a perfect use case for why loops?
So remember, these rules use a for loop, if you know in advance how many times you need to run a piece
of code and use a while loop to run code, as long as something is true, that is really it.
As long as you keep those two rules in mind, you should be good to go.
And I think now you're ready to tackle the next three workbooks.
Good luck.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.