Most programs will need a loop throughout some code.
C plus plus 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 for statement, iteration and range based.
The latter was introduced in C plus plus 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 code 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 a 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 parentheses are three expressions here by semicolons divided by separated by semicolons.
Here.
The first expression here allows you to declare an 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 it.
Two expression here, as you can see here.
So the fourth 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, um, 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 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 stdcout.
So we have to include the standard library here, your stream input output stream and here.
E here.
Arrested.
And now here.
Here.
As you can see here, the output is as follows here.
So in this code, the loop variable is here.
And it is initialized to zero.
So next the condition is checked here and signs is less than ten.
This 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 count, 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 0 and 9.
So you will see this in console.
So the loop expression can be any expression you like.
So but often it increments or decrement the 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 the 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 we write it here, if E equals 20.
Then break our for loop here.
Here.
As you can see here, this loop started from five because E equals to five and keep incremented five
by one.
And at every loop this condition is checked.
So first it 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 brick statement.
So as you know, we introduced the brick 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 rarely see statement that finishes the
using go to here.
But however you may see this code here for example for here and code.
Quote.
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 fourth statement, the loop expression can be anything you like.
So the only property is that it is executed at the end of the loop here.
So you may choose to change the another variable in this expression.
Or you can even provide several expressions separated by comma operators.
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 plus plus here.
Introduce another way to use the for loop which is intended to be used with the containers.
So this Cplusplus standard library contains templates for container classes, which you will learn in
next lecture.