Control structures

Control structures

Control structures are a set of statements that are used to control the flow of execution of a program in JavaScript. These structures allow the program to make decisions and execute different sets of instructions based on certain conditions. In JavaScript, there are three main types of control structures:

1. Conditional Statements :

Conditional statements are used to execute different sets of instructions based on whether a condition is true or false. The most commonly used conditional statements in JavaScript are:

  • if statement: It is used to execute a set of statements if a condition is true.

if (condition) {

//Statements to be executed if the

 condition is true

}

  • if…else statement: It is used to execute a set of statements if a condition is true, and another set of statements if the condition is false.   

if (condition) {

//Statements to be executed if the

 condition is true

}

else {

//Statements to be executed if the

 condition is false

}

  • if…else if…else statement: It is used to execute different sets of statements based on different conditions.

if (condition1) {

//Statements to be executed if condition1 is true

}

else if (condition2) {

//Statements to be executed if condition2

 is true

}

else {

//Statements to be executed if all

 conditions are false

}

  • switch statement: It is used to execute different sets of statements based on different values of an expression.

switch(expression){

case value1: //Statements to be executed

 if expression matches value1

break ;

case value2: //Statements to be executed

 if expression matches value2

break ;

. . .

default : //Statements to be executed if

 expression does not match any case

}

2. Looping Statements :

Looping statements are used to execute a set of statements repeatedly until a certain condition is met. The most commonly used looping statements in JavaScript are:

  • while loop: It is used to execute a set of statements if a condition is true.

while(condition){

//Statements to be executed as long as

 the condition is true

}

  • do…while loop :It is used to execute a set of statements at least once and then repeatedly as long as a condition is true.

do {

//Statements to be executed at least once

while (condition) ;

  • for loop :It is used to execute a set of statements a specific number of times.

for( initialization; condition;

 increment/decrement){

//Statements to be executed until the

 condition is true

}

  • for…in loop: It is used to loop through the properties of an object.

for(variable in object){

//Statements to be executed for each

 property of the object

}

  • for…of loop: It is used to loop through the elements of an iterable object like an array or a string.

for (variable of iterable) {

//Statements to be executed for each

 element of the iterable object

}

3. Jump Statements :

Jump statements are used to transfer the control of a program to a different part of the program. The most commonly used jump statements in JavaScript are:

  • break statement: It is used to terminate a loop or a switch statement.

for (variable of iterable) {

//Statements to be executed for each

 element of the iterable object

}

  • continue statement:It is used to skip the current iteration of a loop and continue with the next iteration.

for (i=0 ; i<10 ; i++) {

if (i%2==0) {

continue ;

click here to go back