Day 13: Loops, Control Flow, & Functions

Control Flow

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.

If else statement.

Loops

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.

For of loop.

Functions

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.

Function example.

Closing

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 ✌️.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store