This course has already ended.

Control structures

Control Structures can be considered as the building blocks of computer programs. They are commands that enable a program to “take decisions”, following one path or another. A program is usually not limited to a linear sequence of instructions since during its process it may bifurcate, repeat code or bypass sections. Control Structures are the blocks that analyze variables and choose directions in which to go based on given parameters.

The basic Control Structures in programming languages are:

  • Conditionals (or Selection): used to execute one or more statements if a condition is met.

  • Loops (or Iteration): used to repeat a statement a certain number of times or while a condition is fulfilled.

Conditionals

Conditionals are at the very core of programming. The idea behind them is that they allow you to control the flow of the code that is executed based on different conditions in the program (e.g. an input taken from the user, or the internal state of the machine the program is running on). Here we will explore the Conditionals Control Structures of “if” statements, “else if” statements and “else” statements.

If statements

If statements execute one or more statements when a condition is met. If the testing of that condition returns a boolean value of true, the statement gets executed. But if it’s false (the condition is not met), then nothing happens.

const number = 1
if(number < 10)  {
  console.log("Condition met!") //"Condition met!"
  console.log("Less than 10") //"Less than 10"
}
//In one line conditional statements, you can also leave out the curly braces
if(number < 10)
  console.log("Less than 10") //"Less than 10"
//You can also write one line conditional statements in single lines
if(number < 10) console.log("Less than 10") //"Less than 10"
if(number > 10) console.log("Larger than 10") //Nothing happens

Else if and else statements

If else Control Structure allows a program to follow alternative paths of execution. Use the else statement to specify a block of code to be executed if all conditions are false.

if (condition1) {
//  block of code to be executed if condition1 is true
} else if (condition2) {
//  block of code to be executed if the condition1 is false and condition2 is true
} else {
//  block of code to be executed if the condition1 is false and condition2 is false
}

Falsy and truthy values

In javascript, a truthy value is a value that is considered true when encountered in a Boolean context. A falsy value is a value that is considered false when encountered in a Boolean context.

An empty string ( ‘’ ), the number 0 , null , NaN (Not a Number) , a boolean false , and undefined variables are all “falsy”. Everything else is “truthy”. Gotchas to watch out for: the strings “0” and “false” are both considered truthy!

function logTruthy(value) {
  if(value) console.log("Truthy value")
}
logTruthy(1) //"Truthy value"
logTruthy("false") //"Truthy value"
logTruthy(true) //"Truthy value"
logTruthy(0) //Nothing happens
logTruthy(undefined) //Nothing happens
logTruthy(NaN) //Nothing happens
logTruthy(false) //Nothing happens

Conditionals

A+ presents the exercise submission form here.

Loops

Loop statements are nothing more than the automation of multi-step processes by organizing sequences of actions, and grouping the parts that need to be repeated. Also a central part of programming, iteration (or Looping) gives computers much of their power. They can repeat a sequence of steps as often as necessary, and appropriate repetitions of simple steps can solve complex problems.

In general terms, there are two types of “Looping techniques”:

  • “While Loops” and “Repeat Loops”: are based on the onset and verification of a logical condition. The condition is tested at the start or the end of the loop construct.

  • “For Loops”: are the ones that execute for a prescribed number of times, as controlled by a counter or an index.

while

The while statement executes a given statement as long as a given expression is true. For example, the code block below will increase the variable c to 10:

while (c < 10) {
  c += 1;
  // …
}

This control loop also recognizes the break and continue keywords. The break keyword causes the immediate termination of the loop, allowing for the loop to terminate from anywhere within the while block.

The continue keyword finishes the current iteration of the while block or statement, and checks the condition to see, if it is true. If it is true, the loop commences again.

do … while

The do … while statement executes a given statement as long as a given expression is true - however, unlike the while statement, this control structure will always execute the statement or block at least once. For example, the code block below will increase the variable c to 10:

do {
  c += 1;
} while (c < 10);

As with while, break and continue are both recognized and operate in the same manner.

for

Loops through a block of code a number of times. The syntax is: for (<initial expression>;<condition>;<increment statement>). Initial expression is executed once, condition is checked before the beginning of each loop, increment statement executes at the end of each loop.

for (let i=0; i< 3; i++) {
  // loops until i would no longer be less than 3
}

There are also other versions of for-loop, such as for/in which loops through the properties of an object, and for/of which loops through the values of an iterable object (array, for instance).

While

A+ presents the exercise submission form here.

Array travel

Note: the submit button may not be visible unless you scroll downwards

A+ presents the exercise submission form here.

Repeat note

A+ presents the exercise submission form here.

Posting submission...