Transcript from the "Control Structures" Lesson
>> And before going and writing, we are going to write two projects before getting into the next topic. Let's talk a little bit about contract structures because so far we did the functions and that's all. We have if else, that's pretty simple. Switch, it says reloaded. I have four for loops, we don't have while or do while.
[00:00:22] Simplicity, we don't need a while. Probably, you are thinking yeah, but sometimes the four is, well, you will see that the four will be good enough. We don't need parentheses for Boolean conditions. Typically, you write if parentheses. Well, not here. Four parentheses, not here. No parentheses, okay? And also, we have only one type of operator for equality.
[00:00:49] So we have just = =, we don't have triple equals or other weird things. No. Just one, okay? Pretty simple. If, else is typically as you're expecting, the only strange part is that you can put more than one expression in the if condition with semicolon is the one.
[00:01:14] And if you do that, the last one is the condition, and then you can, for example, create variables. You can create variables in the if declaration. What's the point? The variable that you set here will live in the if, and also in the else, but not outside. So it's kind of a special scope that is not possible on other languages.
[00:01:45] You create a variable on the outer scope, or then if it's an inner scope, you have a different one for the if and for the else, for the true and for the false. Well, here you can create variables whose scope will be both. Because they realize that's a typical use case for example error messages or whatever objects that you wanna create variables that you wanna create, and you want that variable to exist both on the positive and the negative side of this.
[00:02:42] So in this case, if it's Saturday, you can fall through automatically to Sunday. So it's going to execute the same code. But by default, it will break. Okay, make sense? That's kind of simple. But one nice thing that we have about switch here, is that we can actually remove the whole declaration and use case with any Boolean condition from any variable.
[00:03:10] So you can use this to reduce if else, if else, if else, if else, if. So it's just a switch condition that looks pretty cool. Again to clean code that then you can ask anything and only the first one that gets true will be executed. Okay, make sense?
[00:03:34] And there is always a default if no one was actually true, then it goes directly to the end. Pretty cool, right? The for loop. First, we can use it as with the classic for, with three arguments, the initialization, then the condition, and the post loop sentence. That's pretty common to any for.
[00:04:17] Be careful :=, it's a new variable for the index, not for the actual element in the collection, that's why it's not a foreach. It's similar to a for in. You get the index, you still need to get to the property manually. If you want similar to foreach, it's the same thing range, but you receive two values.
[00:04:38] The first one is the key, the index, and then you receive the value. We will use that now, so no worries. Do we have anything else? Well, actually, no, but you can use for for while. Instead of having a while, we use the same for. The four also accepts directly a Boolean expression, and it's a Y also you have an infinite loop.
[00:05:08] It's just four with no expression. Okay, we don't have while, just for. So we use the for for all the loops. Based on the argument that you used me, you found there too, therefore, it will use one or the other. If, switch, for that's all you have.