Control structures in Javascript

Control structures in Javascript

For control structures, JavaScript has the typical control sentences of high level languages

Variable statement

Variables in JavaScript are not assigned a predefined type

In JavaScript the type of the variables depends on the value that contain the same in each time

Therefore it performs an automatic conversion of types

JavaScript recognizes the following types of values:

Since there are no types of a priori variables, we do not have to specify the type of variable when declaring it

The declaration of variables is done by prepending the reserved word var to the name of the variable

It is possible to assign the value when we declare

The if sentence

The if sentence has the form:

The parentheses associated that define the condition are not optional

If the condition is true, instruction 1 will be executed; otherwise it is executed if there is instructution 2

The use of the sentence else is optional, so brackets have been used in the definition

If omitted, the instruction block will only be considered when the condition is true

A block of instructions is a set of instructions bounded by curly brackets

Keys after sentencing if are not required

If omitted and the condition was true, the following instruction will be executed

Otherwise, the subsequent instruction will be executed, independent of the condition state

Thus, the omission of keys after the sentence if it will allow us to write everything on a single line finished in ;

The else if sentence

We can also use sentences if anities by sentencing else if

Sentences else if work just like a sentence if

But they will only be executed in case the condition of the sentence if was false

The switch sentence

Makes is select a group of sentence among several possible

It is an alternative to the use of sentences else if anities

Its syntax is:

The expression between parentheses of the switch must be whole

Your result will be compared with the different values of the case

If it matches one of them, the following sentence will be moved to the case with that value and consecutive instructions will continue to be executed until a sentence is found break or reach the switch lock keys

In case the result of the expression does not match any value, execution will be passed to the next statement sentence on the label default, if there were, and it will continue as a case

The values in the case can be a constant expression

There can be two case with the same value

The while sentence

The while sentence has the form

The parentheses are not optional

If the condition is met, the sentence or instruction block is executed and the process is repeated until the condition is no longer met

The do sentence

The do sentence has the form

It's very similar to the sentence while, except that the condition goes after the instruction or block of instructions

So they are executed at least once even if the condition is false

The for sentence

As for such a sentence, in JavaScript we can distinguish two variants:

  • The loop for "classic"
  • The loops for / in

The loop for "classic"

This loop has a syntax very similar to that of C / C+

In this syntax:

Initialization creates the counter variable and gives it an initial value

Condition must be fulfilled for the loop to run

Depends on the variable index

Expression updates the value of the variable index

The equivalent of this while expression is:

The loops for / in

This loop has a syntax very similar to the for-each Java

Iterates a variable var over all the properties of an object obj that is passed to it

Thus, for each value of var is is executed, the judgments of the loop

Therefore, the loop will have as many iterations as the object's properties, and in each iteration the variable will have the value of the corresponding object's property with that iteration

Its syntax is:

The break sentence

The break sentence can be placed within a loop or anities loops

When you execute the break sentence exits the loop more internal

To all effects, the break sentence acts as a jump to the sentence following the loop in which it runs

The continue sentence

The continue sentence does not leave the loop but causes the next iteration to run

In the loop while the execution of the continue causes the program flow to jump to the condition

In the for loop the continue execution causes the increment expression to run, and then continue normally with the condition

That is, running the continue prevents the rest of the loop body from running