Learning JavaScript part 2

Computers in the past were used mainly for calculations. But as demands grew programmers needed to use more sophisticated tools to control execution flow. First we’ll take a look control structures and in second half at loops.

Control structures


The simplest control structure is if (exp) . This statement will allow execution of first statement or group of statements in curly brackets (braces) if expression exp is  true .

More complex form of if  statement is combination of if...else which will execute statement(s) depending on expression behind if .

It is also possible to put several if  statements in a cascade:


When I was beginning to learn programming at high school, we learned Pascal then, I feared this statement because it never worked as I needed. Therefore my first programs were made using cascades of if s. Not the most elegant way of coding but brute force works until something breaks. With heavy usage of if cascades it is usually readability of mine/yours/someone else’ code.

Switch is a simple way how to execute statements according to expression in the switch  statement.

In the example 4 we have rewritten if  cascade from Example 3 to a switch  statement. We have to use default  case for the moment when expression gives us different value than 1 or 2. It’s a matter of safety.[ref]Do no harm! Unless you are programming a missile or creating new replicators[/ref]

break  statement is not mandatory in this control structure. But you’ll have to use it when you want to execute only certain set of statement per case .

break  statement is used also in loops.


for loop

Loops in general are used when we need to repeat some action more than once in a row. There are two types of loops. Ones where we know how many iterations will be done and ones without a known number of iterations. for  loop belongs to the former category. Most common syntax for this loop is following:

Parameters for the for  loop are initial state, exit condition (i. e. when the loop can be executed) and what should be done after each iteration.

 while loop

This loop belongs to the latter group where we know only exit condition. Therefore it has much simpler syntax compared to for  loop.

Certain modification to a  while  loop is  do ... while  . The only different except the keyword do  is that the latter loop will execute at least once.

Warning: Some languages, let’s not mention Pascal, work strangle with this loop. Logic states that this will work as log as exp is true. But in Pascal it works as long as !exp is true.

Extra: while  loop can be rewritten as a for  loop:

break statement

This statement is used whenever programmer needs to terminate switch statement or any loop immediately.

 continue statement

continue  statement is from my point of view an extension of break  statement because it breaks current iteration and restarts whole loop.

Example how it works:


In this post we have seen control structures  if ... else and  switch . We’ve talked about  for  and  while  loops. Those two are not the only loops you can use but  for ... in  and  for ... each  are more complicated and I think they should be explained in the post about arrays which is by coincidence next post.