Do You Want to Know More About Control Structures?

Control Structures

Ici Colomar
Adalab
4 min readDec 13, 2017

--

My colleague (Gabi Mena) and I are learning control structures, and we have thought to talk about it.

Boolean data type: They save information like true or false and these values are the only one result they can save.

All the comparative operands return a boolean, a true or a false result.

  • == equal: this operand compares two values although they don’t have the same data type. For example 7 == ‘7’ and the result is true.
  • != not equal: this operand checks if two values are differents. Example 4 + 4 != ‘7’ this example will return true.
  • === strict equal: this operand checks if two values are equals and if they have the same data type. For example 7 === ‘7’ and the result is false. We are going to use this version to compare if something is equal in data type and value.
  • !== strict not equal: this operand checks if two values are differences in value and in data type. For example 7 !== ‘4’ and the result is true because they are different.
  • < less than: it compares if the left number from the operand is less than the right number from the operand.
  • <= less than or equal: it compares if the left number from the operand is less or equal than the right number from the operator.
  • > greater than: it compares if the right number from the operator is greater than the right number from the operand.
  • >= greater than or equal: it compares if the right number from the operator is greater or equal than the right number from the operand.

These operands could combine each other like the next examples:

  • && (AND): it returns “true” ONLY if both conditions are true. For example 7===4 && ‘hello’ !== ‘goodbye’ it should return false because one of the conditions is false.
  • || (OR): it returns “true” if one of the conditions is true. For example 7===4 || ‘hello’ !== ‘goodbye’ it should returns true because one of the conditions is true.
  • ! (NOT): it returns the opposit value. For example ! true returns false.

The comparison operands executes always after the numeric operands, if we have 5 * 1–4 !== ‘3’, first of all the multiplication will executes then the subtraction and at the end the comparison.

Conditional structure.

You can use this structure to execute a code if a condition is met or not. You establish a condition and if the condition is met, the code is run, but if not the code not run.

“If this condition is true, do this; if not do that”.

The condition always is true or false, and in the condition only executes the code in block with the structure “if, else if or else”. The code never executes in two blocks because if the first block is true, it ignore the next.

It has three possible structures in JavaScript language:

Loop for.

You can increase or decrease the value of a variable using ++ or — . If you have a variable i and the value is 1, if you execute i++ the new value will be 2. This loop is used for executed the same code a number of times (do this x times). The structure has three parts:

Initialization: will be a declaration and assignation of the value. Example: var i = 1

Condition: will be a condition that must be met for the code block to run inside the for. Example: var i < 20

Upgrade: will be the operation that is performed before each iteration of the loop without counting the first. Always is i++

If you write all the loop, this is the structure:

The order of loop execution is the next:

  1. First, execute the init when the variable is 0.
  2. Then, the loop check that the condition is met and execute the code into the keys ({}), if doesn’t met will finish the loop.
  3. It execute the operation i++ of the loop and return again to step 2.

And this is all about this awesome structures. Sometimes you can feel frustrated because you don’t know how to use it, but when you practise a lot it’s like see a film from your sofa, it is very easy. :)

If you liked this, click the 👏 below to recommend it to people here on Medium. Thank you!!

--

--