Welcome, In this lecture, I'm going to talk about the "for" statement. In the first step
You're going to learn about the four statements which allows us to repeat a block of statements. loops
statement in
Go is very simple.
There is only one loop statement in Go unlike other languages.
There are no "while" or "until" statements in Go.
However you can create them using only the "for" statement. In the second step
you are gonna learn how to use a loop inside another loop and the last step you're going to learn
about the four range clause.
All right!
Let's start with the first step.
OK.
Now I'm going to talk about the basics of the "for" statement
Let's first take a look at why you may need loops.
Now I'm gonna show you an example code for that.
ok in this very simple program I am going to find out the the sum of five numbers.
OK so let's first declare sum variable here. var some int
Now lets add the numbers beginning from 1 to 5 like this.
All right ready.
lets print them
ok, let me run it
now, it prints 15. correct!
Now imagine that these numbers goes to 1000.
It would be a waste of time to add these numbers by manually typing that as I said at the beginning of
the section that we humans are very bad at doing those sort of things right.
So that's why we need loops. Let's refactor this code to a loop.
now, let me type look at numbers between 1 and 1000 like thi. for short declare.
Start with 1 and loop to 1000 . increments to
i variable. OK!
then inside the loop, I'm going to assign the current i variable to sum variable using an assignment
operation like this.
All right, done!
Let me run it.
As you can see the computer has very quickly added the numbers without getting tired at all.
That's what they do. I think now it's clear why we need the loops.
If you have a programming background already I know this was a very simple example. but I needed to do
this for also beginners.
Ok now I'm going to explain each part of this "for" statements.
This is the initialization statement. This init statement is just a simple statement as you've seen it
before.
This way I can use a short declaration here.
As you can see here it's usually used for declaring variables. Any declared variables here can only be used
inside these for loop. so they won't be visible outside of the for statement. Scoping rules still apply
this statement will only be run once. Alright! that's all about it. Let's continue. This part is the condition expression
of the for loop.
It should be a bool expression only. As long as this condition yields true.
the loop will continue.
For example here, as long as the i variable is less than or equal to 5, this loop will continue. before the
loop begins.
Go will first check this condition and if it yields true the loop will start and after the first step of
the loop ends go will check it again. if it yields true
the loop will continue.
when It becomes false.
the loop will end
OK.
This is the post statement of the for loop. like the init statement,
this is a simple statement.
So that's why I can use an inc-dec statement here.
This statement will be run after each step of the loop.
So Go will first run the statements inside the for statements block and after that it will run this
statement.
I've just used an inc - dec statement here for incrementing the "i" variable so i variable will keep incrementing
until the loop ends.
these highlighted parts are used for separating the parts of the for statements.
their meaning is clear.
so lets continue.
This is the block of the for loop. as you can see the i variable is available here.
It's within the scope of the for loop right? Any statements inside this block will be repeated until
the i variable becomes greater than 5 and that's all
OK.
Let me summarize
it first
short declares the i variable and assigns one to it in the initialization only once. then in its condition
expression.
It checks whether the i variable is less than or equal to 5. at the beginning it's zero.
so the loop starts then Go runs the for statements block which in turn adds the i variable to the sum
variable
then it executes the post statement which in turn increments the i variable and it repeats this as long
as the i variable becomes greater than 5.
By the way in Go, all these parts can be removed except the block. In the next lecture
I'm going to show you how to do that.
That's all for now.
Thank you for watching.
See in the next lecture. Bye Bye!