Hey there loops are used in programming in order to execute the same code several times so as not to
rewrite it one hundred one thousand or so one hundred thousand times.
Let's get started with the loops from the wire.
Look it is written as follows.
Let's remove the template code and declare their variable in type name it for example a and assign it
the value one.
Next we write the key word while open the parentheses and write in parentheses.
A less than 10 fold by curly braces.
And in there we arrive at the border over the loop.
For example let's write a standard comment for outputting to the screen system of printed land.
We will display the text.
How low.
For example
their next line.
We write the code a assign a plus one let's run the program.
And as you can see the message Hello appeared nine times.
Let's see how this code works.
So first in parentheses the condition is checked whether our variable counter is less than 10 at the
moment.
The variable is 1 one less than 10.
Therefore the condition is satisfied.
It is the true this means that the block of code that is placed in the body all the while loop is executed.
The Texas hello is displayed and new value is assigned to the variable.
It increases by 1.
It becomes 2.
Then the loop proceed to the next step.
The value of the variable is checked again at the moment it is 2 and says 2 is less than 10.
That is the condition is true.
This code blog is executed.
Again the Texas hello is displayed and the variable a increases by 1 and this happens until the variable
A takes the value 10 turn less than 10 is false.
Therefore after this the execution of the wild cycle is terminated the operation of increasing their
variable a by want is called the operation of increment patient.
It can be rewritten in short of form
just a plus plus is written.
This means the same as the line earlier.
That is each time in the body of the loop the variable a will increase by 1 way to restart the code.
The result is the same in the same way we can in the body over there loop output not only the same text.
We cannot put different values.
For example let's set the value of the variable a instead of pressed to execute and the values of the
variable.
A that it takes at each step of the cycle are displayed.
If we remove this code from the body of the loop and do not increase our variable a that this loop as
you might imagine will work forever.
This will cause our browser to freeze or the application to crash because they're bored of the loop
will be executed infinitely.
Return this code.
And now let's try it.
Output only even values for this we can do following.
We can change the initial value of a 2 0 and here we can write a equals a plus 2
run the program
as you can see only even values were displayed were printed but also zero.
If we do not want 0 to be displayed we can change our code as follows.
We can change this line.
Increase their variable a buy to put it before the output comment system our printer land a.
And now even numbers from two to ten including ten are displayed but if we do not want to tend to be
displayed we can indicate in the condition a less aid.
For example as you can see four values are displayed all we can do it differently.
The value a set it equals one the value of a in the body of the loop.
We will also increase by 1 that is increment A plus plus and here we will output a multiplied by 2 let's
try this
even values were printed but in a larger range to display virus from 2 to 8 including the 8 you can
change the value here to 5
add as you can see then values from 2 to 8 are displayed another operator I want to introduce you is
uh The while loop it is very similar to the while loop but differs in that that the condition is checked
in the while loop first and then the bottom of the loop is executed in the do while loop the condition
check is checked after the loop body is executed and this loop is written this way let's remove these
code these takes right do first keyword then the loop body which will be executed in but of the loop
and then the wireless written and the condition so harmful for God to put a semicolon let's run it again
as you can see everything is done correctly as before if we put value here for example 6 that is the
condition will be false immediately 6 not less than 5 but since the condition is checked after the loop
bodies executed this block of code will Iran once
as you can see the code has started and the value is twelve and now a small task for you display on
the screen all the odd numbers in the range from 1 to 10.