Welcome to this lecture about for loops.
Before starting.
We need to explain what is a loop a loop is a block of code that iterate a list of commands as long
as the loop control condition is true so how it looks like here is the standard for loop.
It takes first item from the list and assign its value to variable arc.
Then a list of commands between the keyboard to do and done are executed and after this one loop is
finished.
Into variable Eric is assigned next item from the list and so on to understand it fully.
We can look at this example.
We are iterating through a list which contains three items.
Mercury Venus and act.
Firstly to variable planet is assigned variable Mercury.
Then we will perform commands between keyboards due and done which in our case means printing value
of variable planet which is now Mercury then into variable planet is assigned to Venus and we will print
it also.
And then finally available act is assigned into variable planet and Venus printed.
There are no other items to loop over so the loop control condition is false and we are continuing with
the script code after a keyboard.
Done very often you can see looping over a value of a variable.
Here you can see we have assigned into two variable plummets string.
Mercury Venus at what will be the result if we loop over it.
Result will be the same as in previous slide.
So the question is why why the for loop prints each planet instead of printing just one thing.
Because internal field separator definitely separate by space for loop doesn't see only one string thanks
to internal field separator it sees space strings.
Mercury Venus and act but if we in our example used quotation marks with the variable planets we would
basically tell strictly follow created string planet and don't separate for that case.
The for loop would iterate only one time and we would see printed Mercury Venus act as a one string
on one line Well let's see another example with internal field separator into variable planets.
We are assigning Mercury Venus ahead separated by comma for a loop.
In that case would iterate only one time and we would see printed only one string as internal field
separator doesn't contain comma symbol.
There is nothing to separate in our variable s internal field separator default separates.
Also by New Line we can assign into variable planets Mercury Venus and separated by New Line.
And in that case would internal field separator separate it our string to three strings and as output
of four loops we would see printed each planet on different line.
We can in four loops also use wildcards.
Here for example in the list are all text files and for each text file we are printing them using photo
loops.
We can integrate any number of times here.
For example we are iterating through a list of 1 to 10 range.
So 10 times iterating For loops are also hugely used for looping through all arguments provided as we
can see in this example.
We have also possibility to use for loops in C style.
Here we are using double parenthesis and inside them.
We are setting default value so we are starting here with variable ie equals to 1.
Then we have loop control condition so we will iterate through this for loop when the value of variable
IE is less than or equal to 10 and the last part is incrementing.
That means at the end of each iteration we are increasing value of variable ie by 1.
Of course we can also decrement and not only by 1.
It's up to us of what operation we will do here but most commonly in this type of for loops is spared
from increment operation by 1.
So dad was a lot of information in very short time kind of exhausting but don't worry.
Best way to remember the usage would be to seed on some example in next lecture.