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
So now let's see how we can use and when we should use while loops in our C programming language.
That's saying that we would like to print an asterisk.
And from now on, I will use instead of asterisk, because it's kind of hard to say.
Right.
I will use just the star.
OK.
So an asterisk equals two staff.
From now on.
OK.
So let's say that we would like to print just one star.
And to do so, first of all, we will include the standard input output and creates our main function.
Now, if we want to print these star, we just use a single print of command.
Right?
That's pretty simple.
And what about printing two stars?
Simply add another print, a function.
And in the same way, if you would like to print three, four, five and more stars, just use the copy
and based technique.
But the real question arises when you are asked to print, for example, 10 stars, what will you do
then?
Will you use then print off lines?
Because I think it's starting to get kind of messy, right?
We can definitely see that these print off-line print of star line.
Basically, it's it's kind of a ability of command.
We repeat the same command over and over again.
We can see it clearly here in this example that we do it for 10 times.
And one of the things that we've learned in the previous video is that whenever we have repeating commands,
we should think of using loops.
So let's see the following lines of code.
So first of all, we create a variable called count.
And this variable will specify the number of times we've printed a star.
So it will simply be some sort of indicator or as specified for us to know how many times a star was
printed.
And then we will use a while loop.
So you, first of all, specify a wild statement, right?
Just like we've discussed in the general introduction.
And now in the parentheses, you can see that we specify the condition, which is count less than 10.
Basically, what these lines of code mean is that wild count is less than 10.
While this condition is true, we are going to execute the loop body, which is specified in this section
between these nice curly brackets.
And what we are going to do here is simply to print a star to the screen.
So a star will be printed on every iteration of these loop.
So if all around this code, you can try that also on yourself.
But just do it carefully, OK?
We can see that the printing of the stars to the screen never stops.
And why is that?
Can it be that what we have here right now is kind of an endless loop?
Let's try to figure out what's going on here.
So we have the wild statement and then in the parentheses, we have our condition.
Right.
We can see that count is less than 10 is our condition in this wild statement.
And this is very similar to the conditions we've seen in the previous chapter where we've used our different
F statements.
You remember that, right?
So the same goes here in the parentheses of the wild statement.
We have our condition.
And the result of that condition may be either true or false.
If it's true, then all the commands under with these curly brackets are going to be executed.
Which in our case is just one command to print a star to the screen.
And it's located under the section that we said it is called of a little body.
Okay.
So the commands of the lobe are in these nice curly brackets and it's called the loop body, all the
commands that are under these while statement.
Okay, so the little body is going to be executed every time that the loop condition happens to be true.
His longest count is less than 10.
But if and once the result of the condition happens to be false.
We do not execute any lion in the wild block.
And we just move on to whatever comes afterward after the while loop, which in our case is just the
return zero.
So once again, we have our wild statement, our condition and the Lewy body that is executed based
on the result of the condition.
So we know, for example, we can see that the condition is count less than zero.
And at the beginning of the program, we've set count to be equal to one.
So for the first time that we see the wild block, this is the first time we've seen it.
And we check out the condition, which is one less than 10.
And the end result of this condition happens to be true.
Happens to be true.
Then in this case, we will bring these stories to the screen and go up again to check out if the condition
has changed or maybe it still remains true.
Right.
It may be either either one of them.
And if it's still true and count is actually less than 10, it's still less than.
Right.
It is still is it is just one.
Nothing's changed that.
Nothing's changed our account variable.
So in this case, we are going to round the commands in the wild block.
Once again, we are going to print another story to the screen, and then we are going to do it again
and again and again and again and so on.
You get it?
You got the point.
And as you probably noticed, the variable count, these variable will never change.
It will never change.
There were nothing that changes the inside of these while block inside of this little body and nothing
change changes.
These variable.
So it's always these condition is always going to remain true, which means that we have an infinite
loop.
We are going to print stories to the screen over and over again and nothing will stop that.
And that's basically not what we wanted.
Right.
We barely can stop this monster program that runs an infinite printing command.
So don't try this one at home.
And what what do you want us to do?
Think about is that basically that's not exactly what we wanted.
Right?
We said it previously.
So let's take another a look and try to to understand what's going on and how we should fix our house.
How should we fix this problem that we have right now of using the infinite loop?
So that's kind of simple.
And we should just increase the value.
We should just increase the value of count by one right.
On every duration.
We said that running from these curly brackets to these curly brackets, it's it's considered to be
running a whole iteration of the loop body.
So on every duration when we print a star of the screen, we are going to increment count by one.
So that will look something like this count equals to the previous count plus one.
So at the end of every duration, count is going to be incremented and then we're going to check out
the condition once again.
And now let's take a look at how the flow of the code looks like.
So basically, we're getting into the main section.
We initially the variable count to be equal to one.
We can see it right here that it's kind of fee.
It's a variable.
It's kind of a box with the name count and the value of one inside of it.
And now we come to the wire loop for the first time when count equals to one.
And we check out the condition if one is less than 10 and the result is obviously true.
So we will execute the commands in this lower body.
Right.
We said this is the loop body.
So we will print one star on the screen and we can see it right here and we will increment count by
one.
And we can see that count currently equals two two.
So that's the value of count after the first iteration.
And you can see once star printed on the screen.
This is our console.
Right?
This is just the screenshot of our console.
And then we are going to eat the rate once again.
We are going to check out if two is less than 10.
We are going to enter these while now while you buddy to print another estado, the screen increment
count by one.
Now, which equals two, three and so on.
You get the point.
You just get inside of the loop, buddy.
You executed, you increment.
What you have to do in this case is just the count itself.
And then you are going to basically come to a point where these count will not be less than 10.
So, for example, if we are going like this and we have all of the stories printed for the console,
uplink to the console and we have nine.
So we print the last Starwind Grimm and count by one and now count equals to 10.
So at this point, we can definitely say that if we come back to check out this condition once again,
the result of this condition, if.
If 10 is less than 10 will be false.
And that's why we are not going to execute the loop body anymore.
We are just going to go onto the next command, which is return zero and the end of these end mean program.
And actually, if we take a look, another look, here are the printed stories on the console.
We can see that there is only nine stories printed out.
Which, first of all, we wanted to print 10 of them.
Right.
So where did we mess up the last?
Then we checked the condition count was 10 and the condition.
If Dan is less than then happens to be false.
So that's why we didn't bring the last star on the screen.
The tenth star.
So one of the ways to fix that is to modify the condition a little bit, to modify this condition by
adding also the equality signs.
So this will be as long as count is less than or equal to 10.
As long as this condition happens to be true, we are going to print another star here, the 10th,
which will satisfy.
Actually, what we wanted it to do, so I hope that makes sense to you, how you should use the while
Lupino received programming language.
And I hope you got all the flow of how the loop's structure works.
Basically, first of all, you start the loop statement, which is while in this case include the loop
condition and the loop body and you executed until the condition is false.
So that's a good practice, guys.
And I hope you enjoyed this video.
And I'll see in the next video where we are probably going to practice a little bit these wild loops.
So I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.