In this medium we will be looking at control structures.
Goal is a simple language so we will cover almost all of them.
Control structures are the rules of our program.
They analyze variables and decide the direction that the program will follow go is a simple language
and like others it only has three control structures.
Let's check them the first one is called for or for loop.
It's a control step name that will execute some common code repeatedly until some conditions met.
Let's see an example.
Let's say we want to print the first 10 numbers so we can do it like this print 0 print one print 2
and so on right.
But it would be very smart.
So in this cases when I want to use a for loop to use it the way to to express this loop is writing
for initially and then those three sections the first one is the initialization.
So let's say for E equals to zero.
This will be our initial state the next stage will be the condition it reads while.
It's less than let's say 10 and the after class which is gonna be increase e by one eye plus plus is
the same the eye equals to eye plus one before shorter we will write it.
Plus plus.
So in this case what we're saying is I start with the value of 0 you will increase by 1 in every iteration
of this loop.
Its value until this condition is not met which is I is smaller than 10.
So what are we going to do inside this loop is to print the value of ie.
So there is gonna iterate I starting from zero and then in a smaller number than 10 which is nine.
So to better understand this let me save it and executed.
So what we see now in that value.
I start with zero then gold one two three.
Blah blah blah.
Until nine.
So this is the main use case of the for loop.
It basically iterate until some condition is met.
Doing something else.
In this case implementing the value of pi.
Now the next control structure is the if statement if it's a completely lost control structure were
some condition is met once from some condition is met he will execute some sunblock.
So let's expand this example by doing OK.
Let's say we just want to print the numbers bigger than 5.
Right.
So we know I is gonna start from zero will end in nine but we just want to print the volume.
When the condition AI is bigger than 5 is met.
So to do it you just have to write.
If the condition which is AI bigger than 5 in brackets right.
What's gonna happen here is I'm just going to be printing AI when it's bigger than 5.
But the loops and when it's smaller than 10 so we should print 6 7 8 and 9.
Let's verify that works.
Yeah.
And it does.
Right now I can do the upside.
Let's say when I is smaller than fi printed and now I have from 0 to 4.
Now there's what I can do is to define two conditions.
So if the condition is met I will print the number in this case but it is not met.
I can do something else and that's where the LS keyword will help me.
So I'm gonna say here is if I use this more than 5 printed or otherwise just print let's say some text
like I can't print why.
So let's run this and see what happens from 0 to 4.
It prints I but after that on every duration that if statement result these two falls so it falls into
the else statement which is I can't print I read.
Now let's go to a more useful case.
Let's say you want to print the even numbers instead of doing this.
I'm gonna use this operator which is the model's story.
Is everything okay.
So this is operator is gonna return zero when the number is even.
So let's say this is just bringing the numbers that even numbers.
Okay.
Now a better way to express if conditions are not better but another way to express conditionals that
can be easier to read when our conditions grow too much.
It's called switch.
So let's see what switch does.
It's pretty similar to Eve but again when our conditions grow it's way easier to read.
So let's go to some other example let's define our price function which is.
Take an item which is a string and it's gonna return its price.
Price is of course a number.
So it's an end it's a typo.
Okay.
So let's say I have a list of items where I want to return the price I can use an if statement.
But let's use a switch statement to see how it will look like.
So it is going to be used with a switch keyword is common in very other languages.
So let's switch the item and see if it's an apple for example with our return let's say 10 Apple cost
if it's an orange price will be why.
If it's a carrot let's say is cheaper.
So it's fine.
So if you can see here what it is doing is if this condition is met then returned its value.
Else if this condition is met return is value.
And so on.
Now let's call the function to see if this really works.
So let's do price Apple right.
OK.
And now it's failing that it is finding failing because if not none of these conditions is met I should
return something but I'm not returning anything.
So there's two ways to think that I can do here.
I can return some default value which is going to be zero.
And this is gonna work.
So if I run this I have the price.
So let me print it.
So you can see it on the screen and it's fine.
The price of Apple is 10.
The price of carrot should be five.
But the price of a laptop is not defined right.
So if you return zero.
Okay that's fine.
Now there is another way I can do this which is using the default keyword and the default Cuba is basically
if not the condition before his math regional 0 is the same effect.
There is probably an hour and expressive way to say it.
Now let's say you have two items which has the same price.
Let's say apple costs 10.
And also tomatoes has price.
So I can change these by tomatoes.
And it sure says 10 as well.
Okay.
So what was learned on this episode is that we have three control statement for Eve and switches and
they are super useful for controlling our program and makes the decision we want.
So that's it for today.