Most programs will need a loop through some code.
C++ provides a several ways to do this, either by iterating with an index value or testing a logical
condition.
There are two versions of the four statement, iteration and range based.
The latter was introduced in C++ 11 and the iteration iteration version.
In this lecture, you're going to learn the iteration version of for statement.
So in iteration version you're going to add firstly init expression, then condition.
And then loop expression.
And after that, you're going to enter the court here.
So this means loop statement.
So you can provide one or more loop statements and for more than one statement, you should provide
a code block using braces here.
So the purpose of the loop may be served by the loop expression.
In which case you may not want to loop statement to be executed.
Here you'll use the null statement, which means nothing here, as you can see.
Here.
You can use just nothing here, and you won't have the loop, uh, expression here.
So within the parenthesis are three expressions here by semicolons divided by separated by semicolons
here.
The first expression here allows you to declare and initialize a loop variable.
This variable is scoped to the for statement, so you can only use it in the for expression or in the
loop statement that follow here.
So if you want more than one loop variable, you can declare them in this expression using the comma
operator here in its two expression here, as you can see here.
So the first statement here will loop while the condition expression, condition expression is true.
So if you are using a loop variable, you can use this expression to check the value of the loop variable.
So the third expression is called at the end of the loop.
So after the loop has been called following this, this condition expression is called to see if the
loop should continue here.
So this final expression is often used to update the value of the loop variable here.
So now let's create an example loop here for loop integer, we're going to assign integer integer E
to one zero, and the second is condition.
As you know, while E is less than.
Ten and.
Increment E by one at the end of the every loop here and std C out.
So we have to include the standard library here, your stream input output stream and here E here.
It's till the end here.
Here's.
As you can see here, the output is as follows here.
So in this code, the loop variable is E here.
And it is initialized to zero.
So next the condition is checked here and size is less than ten.
The statement will be executed and the statement is printing value to the console here, as you can
see here.
So the next action is the loop expression here, the plus plus e called, which increments the loop
variable E and then the condition is checked and so on.
And since the condition E here is less than ten, this means that this loop will return ten times with
the value of E between zero and nine.
So you will see this in console.
But the loop expression can be any expression you like.
So but often it's increments or decrements of value.
So you don't have to change the loop variable value by one.
For example, you can use E equals five as the loop expression to decrease the variable by five on each
loop here.
So the loop variable can be any type you like.
So it doesn't have to be integer, it doesn't have to be numeric or anything here.
And the condition and loop expression do not have to use the loop variable here.
So in fact you do not have to declare a loop variable at all.
If you do not provide a loop condition, the loop will infinite unless you provide a check in the loop
here.
So for example, let's delete this condition here and run our code here.
As you can see here, we have infinite loop here.
The loop won't stop and keeps on printing and printing.
So if you write it here, if E equals 20.
Then break our four loop here.
Here.
As you can see here, this loop started from five because E equals to five and kept incremented five
by one and at every loop this condition is checked.
So first it e was not equal to 20 and that's why the loop won't break here.
And while the E equals to 20, the condition, the if condition turned true and the the break statement
executed here.
So this uses the break statement.
So as you know, we introduced the break statement in the previous lecture.
So with the switch statement, as you know.
So and you can also use the return go to or trough, you will really see a statement that finishes to
using go to here.
But however you may see this code here for example for here and code.
What?
So in this case, there is no loop variable, no loop expression and no conditional.
So this is an everlasting loop.
And the code within the loop determines when the loop finishes.
So the third expression in the force statement, the loop expression can be anything you like.
So the only property is that it is executed at the end of the loop.
So you may choose to change the another variable in this expression, or you can even provide several
expressions separated by comma operator.
So for example, if you have two function, one is called pull data that returns true.
If there is a more data available and false when there is no more data and the function called get data
that returns the next available data item you could use for as well here.
So.
C++.
Here, introduce another way to use the for loop, which is intended to be used with the containers.
So the C++ Standard library contains templates for container classes, which you will learn in next
lecture.