Welcome, In this lecture I'm going to show you how flexible "for" statement is. and at the end you'll learn
about the break statements. Lets get started.
Remember this "for" statement.
Now I'm going to tear it apart, in a good way of course and the break statement will naturally emerge
from its ashes.
Let's do that! Now, remove the initialization statement
It can also work this way.
Assume that the i variable is declared somewhere before and outside of the "for" statement.
Notice the separator.
It's still there right.
So Go can understand that there are still a condition and a post statement. ok lets continue.
Now I also removed the post statement.
this works too.
offcourse I needed to increase the "i" variable myself.
Otherwise the loop will never quit.
I moved it after the summation. It's because I'm trying to imitate the actual "for" loop here.
Remember Go runs the post statements after running all the statements in its block. So that's why I'm
incrementing
the "i" variable at the end of the loop here.
Notice again the separators
they are still here right.
OK let's also remove the separators.
This is known as the "for" statement with a single condition.
Now let's also remove the condition. here
this loop will never quit. It will loop forever to the infinity Its because behind the scenes the condition
is still there! it's just hidden and it equal to true. remember for loop will continue as long as its
condition is true.
right? so because the hidden condition is true this loop never quit.
By the way this loop syntax is called an infinite loop.
As you see in the examples that infinite loops are mostly used when you don't know how many times you
would repeat the statements.
For example you can keep repeating the loop until the user presses the quit key.
Or you can use it for a network server.
which runs and listens for ever.
So how can you end this for loop.
Let me show you. To exit from the loop.
I need to use a break statement. but where you type the break statement is very important because it will
break out from the loop immediately.
For example, if you call it here. it will quit from the loop after the summation and it will skip the following
incr-decr statement.
So because of this! Actually, This loop will end on its first step.
and I don't want that. I want to add the numbers as before. right?
So you need an If statement to properly quit from the loop.
Let me show you
now I'm going to type an if statement like this.
As you can see I move the break statement inside the if statement now.
This way the loop will only quit after the "i" variable becomes greater than 5. right?
Let me show you this.
In the code editor.
first I'm going to declare the sum and "i" variable's like this since I want to add the numbers starting
from 1 so I've initialized the "i" variable to 1
OK then I'm going to type an infinite for statement like this.
Just the curly braces right here.
Inside this block, I'm going to compare whether the i variable is greater than 5 or not like this.
So this way this program will only add numbers up to 5 and I'm going to type break statement here.
So it will quit from the loop only when the i variable becomes greater than 5 and then I'm going to
add the "i" variable to the sum variable like this and lastly, I'm going to increment the "i" variable
The order of the statements is very important. as I said it's like the actual for statement now.
So before each loop step begins go we'll check for the condition then it will add the "i" variable and the sum.
Then at the end it will increment "i" variable and the loop will continue until the "i" variable becomes greater
than 5.
Then it'll break out from there.
All right.
Now let's run this
as you can see it prints 15, which is just correct.
Now let the me print the numbers here.
So you can see what it's calculating. println i
dash dash
greater than arrow and sum. OK?
OK let me run into again.
So it adds the numbers
step by step right.
All right in the next lecture I'm going to show you another statement
If these examples look simple to you don't worry at all. I prepared more challenging ones as well.
You'll learn about them soon
So see you in the next lecture.
Bye Bye