Day 13: Loops, Control Flow, & Functions
We can use control flow to control the execution steps of our programs. We can use conditionals and boolean operations to control the flow of our programs. Conditional are expressions that evaluate to true or false, depending on the true or false value we can execute different lines of code. A typical way to make use of these conditionals is to use them inside of If/Else statements. If else statements read similarly to human language, if(something) do this: otherwise/else do something else.
Loops are useful when you need to perform the same action multiple times. We can use for loops, which let us define an iterator and a stop condition. The loop will execute a certain task until the stop condition is met. We also have while loops which are similar to a for loop, but they do not come with an iterator. For loops are better when we know how many times we want the loop to run. While loops are better when we want the loop to run until something in our code causes it to stop. If we want a cleaner syntax for iterating over arrays we can use a for-of loop.
Functions are sets of statements that perform a task or calculate a value. When we have repeated code it is useful to wrap that code inside of a function so we can use it in multiple places. This keeps our code modular and readable. When we declare functions we define what the function will do, then we call the function wherever we need it in our code. Sometimes our functions will need some external information to do their job. To keep functions flexible they are able to have parameters/arguments, which allow us to perform tasks with different pieces of data.
We are starting to learn how we can control our programs and keep our code clean. Soon we will be able to write bigger and more complex programs ✌️.