Control structures in JavaScript. In general the execution of the program is sequential, we can change the normal execution by using the control structures.
1. simple if
Syntax: if(test expression)
{
statements;
}
First the test expression is evaluated, if it is true then the statement block will be executed otherwise not.
if-else
Syntax:
First the test expression is evaluated, if it is true then the statement block1 will be executed otherwise statement block? will be evaluated.
2. switch
It is a multiple branch statement. Its syntax is given below.
switch(expression)
First expression evaluated and selects the statements with matched case value.
Eg. switch (n)
3. for loop
If a statement wants to execute more than once.
Loop is used.
for loop is an entry controlled loop.
The syntax of for loop is given below
For(initialisation; testing; updation)
{
Body of the for loop;
}
4. while loop
If a statement wants to execute more than once.
Loop is used.
It is also an entry controlled loop The syntax is given below
Loop variable initialised
while(expression)
{
Body of the loop;
Update loop variable;
}
Here the loop variable must be initialised out side the while loop. Then the expression is evaluated if it is true then only the body of the loop will be executed and the loop variable must be updated inside the body. The body of the loop will be executed until the expression becomes false.