Hello and welcome to this lecture.
For each groups for each loops are useful they are used to execute a block of code on each element in
an array or a collection of items.
The for each loops are quite useful for displaying each items in an array or a collection of items.
So the syntax or basically the syntax refers to how you specify or define a for each loop.
So you start with a word for each and then you specify the variable type in the array or collection
followed by the code that will be executed.
So let's have a look at how we illustrate the for each loop
to save time.
I've already written out some code here which is this piece of code here.
Okay so I'll just show you what I have done.
I previously used some code here for to illustrate a loop basically a while loop a do while loop.
So I've commented out the that piece of code here you can see it in green here.
So the code we are focusing on here is a block of code here to begin.
I created a variable type and the variable type the type is a string.
Notice of put square brackets in there the square brackets indicates that it is an array.
That means several an array basically consists of several variables stored as one.
So I've given the name of the array of variable.
I've given it the name of d All right.
So that's what I've called it and I have initialized it by initializing it.
I have added values to it.
Notice I've used the curly braces the left and the right and I have specified all these are all elements
of this array.
Here you can always tell or identify an array with square brackets anyways square brackets.
You know there's some kind of I re going on they are available so the array is called days and even
initialize it an assigned value using the equals to sign.
And these are the various objects or elements that will consist of this array because the string the
data type is a string.
Hence the text values are enclosed in quotes and each of the values are separated by comma.
Here on Line 23.
This is where r for each begins.
So with defining the for each loop this variable type the string is what will be used to collect the
items from the collection or the array.
Okay so string so the for each here and so have the for each and inside it for each parentheses are
specified a variable type or a data type which is string and of all so also declared this here day for
the for each sub declared is string chord.
D.
Okay in for the for each so declare this string.
This value code day in days so the in days is referring to this array here.
All right.
So what this is do in this string here.
It will use this day to pull the value from this day's array.
So this is what is going to be used to pick out all these values from this day's array.
So when I click on the button it will first of for display the current day and then it will go on to
list each of the rest of the days like Monday Tuesday Wednesday until it gets to Saturday and it will
then exit
and it will also display this block of code here to display the day is a start with Sunday Monday Tuesday
and so on.
OK so let's give that a test and see how it works.
Click Start.
All right.
So if I click on this button it will go through and show all the elements in that array.
So we just issued start with a Sunday and with a Saturday.
So I click on the button.
The day is a Sunday click again.
Monday Tuesday Wednesday Thursday Friday Saturday and then it exits.
Okay so that's basically what the.
For each does so it will loop through the elements and display them the elements in an array or a collection
so the values from the arrays are retreat and displayed one after the other.
Thank you so much for watching this video.
Take care and buy for them.