Not let's look at a very commonly used Concord for a statement called for a statement or follow up. 2
So it has something to do with looping a fun loop is typically used to iterate or loop over elements 3
of an array or a similar data structure and do something with each element like printing what the element 4
is. 5
For example when we visit e-commerce websites Visi products displayed as a list. 6
This is usually done with a follow up. 7
That is the products would be stored in some kind of a data structure like an IRA under for a loop. 8
What they don't want the array to display each part product stored in the art that is in each iteration 9
data associated with one product is displayed. 10
Let's see how a for loop accomplishes something like this. 11
Follow up is basically an iteration statement. 12
Need it you know it's our executive block of code for some number of things. 13
How many times a block of code is executed is dependent on some condition. 14
I think the best way to understand follow is to be a simple example. 15
And here we have one here you have an idea containing an immense Indigos zero to nine under statement 16
simply because there are elements so the statement includes the key word for followed by parenthesis 17
which includes a few xperience that and this is followed by the body of follow up riches and President 18
quali basis know within the band this is we have three expressions separated by semi-colons and these 19
two semi-colons are mandatory the first expulsion is basically an initialization expression and it is 20
initializing the variable to zero. 21
I is basically used as a risk index number also since I is being declared here it is visible only within 22
the first statement that is it is accessible outside of the for statement the second expression is a 23
condition and it is what decides whether the body or for statement should be executed or not. 24
So during the first iteration the condition would be zero less than 10 done is the size of the third 25
expression is simply an increment operation which helps and it did it in the ADI So those are different 26
expressions within the patent cases. 27
Now let's look at the concurrent fruits and so initially in the first iteration the variable I would 28
be initialized to zero. 29
And then next up condition is checked that is whether I is less than 10 is true or false since I have 30
zero and zero is less than 10. 31
The body of first statement is executed. 32
So the first element of the array gets printed. 33
Next I will be incremented to one due to the increment expression end up a nation will be checked once 34
again. 35
Distend the condition will be run less than 10 which is also true. 36
And so the body of false statement gets executed again and this time the first element of the array 37
would be printed next I would be incremented to after it is a condition Bulla's then is ject. 38
So the cycle of checking the condition excluding the body or first statement and then doing the increment 39
operation continues until I become certain at which point the condition fails as done unless less intent 40
would be false. 41
So at this point the fun look for the exit under statement following the for loop would be exaggerated 42
. 43
So the initialization expression is executed only once. 44
But the other two expressions on the body are for a loop are executed multiple times at the end of the 45
lesson Wyburn exhibit the simple example and we will also try on some of the stuff that we are going 46
to see in this lesson here is a general form of a statement like we discussed the parenthesis includes 47
three parts initialization condition expression and the exhibitionist expressionless part is the part 48
that usually does the increment or sometimes even decrement operation. 49
Here in addition Parker can be either a variable declaration statement or it can be a list of common 50
separated expression statements if you recall a statement can be of three types declaration expression 51
or a control flow statement is the expression this part can also have a list of comma separated expressions 52
statements but it cannot have any declarations statements condition expression will always be some kind 53
of a boolean expression not just look at each of these parts up lately although for the sake of completeness 54
we will look at several different ways in which four statements can be written. 55
Typically you would not write for loops that are different from the example we saw earlier. 56
So let's begin with the initialization part. 57
The initialization part is optional. 58
And the example we just saw can also be rewritten as shown here. 59
Hear the index variable initialization is done outside of outlook on the initialization part of the 60
for loop is empty. 61
Initialization part can have a single declaration statement on this is the most common scenario. 62
On Here are a few examples. 63
First example is straightforward. 64
On something we have already seen second example is declaring variables and we know that it is a valid 65
declaration statement third and fourth examples are invalid third is invalid. 66
Even the board wearables are same type so you can only have one declare Mission Statement in the initialization 67
part. 68
Note that these last two examples are invalid even if those statements appeared independently outside 69
of a statement. 70
So basically what we are seeing here are proper declarations statements and if they are invalid outside 71
of her statement then they are invalid even when used within the first statement. 72
Initialization but can also how a list of explicit statements that are called Must up later on let's 73
look at few examples. 74
Here in the first example the variables i and j are being re-initialize that is they're already declared 75
outside of Outlook and they are simply being reinitialized here. 76
Second example shows that you cannot mix an expression statement with their declaration statement next 77
we can have all of these expressions statements and they are all valid. 78
And I would say you should avoid using initialization part and this bit as I'm sure many birds are not 79
even aware of such a possibility and hence the code would be very confusing. 80
So use any or they can but for either variable declaration or variable re initialization next is condition 81
exhibition on Binoy it must evaluate the boolean value it can also be optional. 82
And so if omitted it Drew would be as you were and we may end up with an infinite loop. 83
And here is an example. 84
No one didn't find a group that can use a break statement like in the case of switch statement using 85
break statements with followups will be discussed later. 86
Find them if you have the experience part they can also have a list of call most x Mission Statements 87
like in the case of initialization part. 88
And here is one example which has been just rewritten here the print statement has been moved into the 89
expressionless part which also includes the increment operation not the semicolon is required at the 90
end of her statement as there is no explicit body we can also take this one step further and make it 91
even more compact. 92
Unheeded is here the inexplainable is incremented within the print statement itself. 93
All we can write our statements. 94
This meant Typically you would have only an increment or decrement operation in the expression of the 95
spark. 96
So that's a fair statement. 97
No let's go ahead and Bouchard them all of some of the stuff we have learned in this lesson. 98
In the interest of time I'm going to show this to mind my Eclipse ID. 99
And here we are in the basic class within domain. 100
And this is the same example that we have seen earlier where we are printing the elements from zero 101
to nine. 102
So let's just go ahead and run this. 103
Just click this button here. 104
And as you can see all the elements of getting printed here. 105
OK. 106
And so this is the most typical way in which you would use a follow up. 107
OK. 108
But we have also seen different reason which could be used different Boehmer additions and combinations 109
. 110
And you look at some off them so one of them was that the initialization part can be optional. 111
So the declaration can be done outside of loop. 112
So it should give us the same result and you can see that we have the same result here. 113
So let me just move it back now we also said that we cannot have this kind of declaration. 114
So it is invalid and it giving it completion. 115
So you can see that red line here. 116
So the right re is. 117
So this is the right way off declaring it. 118
OK. 119
So this is still a declaration statement. 120
Now we said that we can that initial edition but and also how ex-Special statements. 121
So so this is a declaration. 122
And here we can re-initialize them do i equal to one. 123
And Jake will do one not this would only print the elements 1 2 9. 124
OK. 125
So we how IAG is not being used anywhere. 126
So in this case you can see that it's printing 8:59 now other than that we also can how expression statements 127
over here multiple exhibitions statements in the exhibition list and that's also put I lay off de Let's 128
put an empty string here Mr. double caught quote then whitespace let me just make it g. 129
So you also how I and J now. 130
And let's also put another condition here. 131
Get less than I read out and so that's the whole condition expression and we are incrementing both I 132
and J here. 133
So let's just run this. 134
So now it's blending dot elements and this bit I read off by an idea off just so it will just be the 135
same element. 136
Now let's just go back let's make it I equals zero. 137
Let me also remove this 138
and if you recall the last example was moving this here 139
and making this plus plus unlimited modus 140
So the opening statement has been moved here. 141
But as you can see it gives a compiler because there is nobody and said that we need a semicolon here 142
. 143
Now it's right. 144
So this is the most compact form on if you add on it it prints all done in advance. 145
So that was the last one which we have seen. 146
So that's a lot for loop. 147
And next we will look at something called and hands for loop and also in the seamless entry would also 148
look at a commendation from effective July item. 149
Ok then go ahead and try this out. 150
Play with it and that's it. 151
Happy coding ironed. 152
Thank you