Skip to main content

Statements

if statement

Basic syntax of if statement is

if (expression) {
statement;
}

Example

var x = 4;
if (x) {
console.log("true");
}

The given expression is evaluated to a Boolean. If the expression is true, the statement is executed. If not, loop will exit.

else statement

One of the drawbacks of if statement is that there is no statement for false condition. So, this problem can be solved by using else statement.

Basic syntax of else statement is

if (expression) {
statement;
} else {
statement;
}

Example

var x;
if (x) {
console.log("true");
} else {
console.log("false");
}

In this code a variable is declared. If the value of variable persists, if the condition is true and if statement is executed. If false, else statement is executed.

else if statement

More advanced logic can be implemented using nested else if statement.

Basic syntax of else if statement is

if (expression1) {
statement;
} else if (expression2) {
statement;
} else if (expression3) {
statement;
} else {
statement;
}

Example

var x = 3;
var y = 2;
if (x > 3 && y == 2) {
console.log("test condition 1");
} else if (x < 3 && y == 2) {
console.log("test condition 2");
} else {
console.log("test condition 3");
}

In else if statement we can chain many else if statements. By using and operator, if first condition is satisfied print given condition, if not check second condition and if satisfied print the second condition. Otherwise print the last condition.

switch Statement

switch statement is used when there are many codes to be executed and to avoid many else if statements chained together.

Basic working of a switch statement is:

  • The value of expression is compared with all conditions in each case.
  • If there is a match following statement of following case is executed.
  • If there is no match default statement is executed.

Basic syntax of switch statement is

switch(expression) {
case condition 1 :
statement;
break;
case condition 2 :
statement;
break;
case condition 3 :
statement;
break;
default : statement;
}
Note

break statement helps to exit from the switch statement. If break is omitted, the interpreter will continue by executing every statement until next break is reached.

Example

var grade = "D";
switch (grade) {
case "A":
console.log("Good job.");
break;
case "B":
console.log("Pretty good.");
break;
case "C":
console.log("You passed!");
break;
case "D":
console.log("Not so good.");
break;
case "F":
console.log("Back to the books.");
break;
default:
console.log("Grade Error!");
}

Using switch statement a grade calculator is made. Current grade is D, so this grade is compared with every case. If any of them matches following statement is executed and exit from the loop.

while Loop

while loop executes a statement or code of block repeatedly until the expression becomes false or a break encounter.

Basic syntax of while loop is

while (expression) {
statement;
}

Example

var x = 1;
while (x <= 10) {
console.log(x);
x++;
}

In while loop condition is checked first, then the statement is executed and at last count is incremented. In this example while loop prints from one to ten. When ten is printed loop is exited.

do-while Loop

do-while loop is similar to the while loop except that the condition check happens at the end of the loop.

Basic syntax of do-while loop is

do {
statement;
} while (expression);
Note

Semicolon is used at the end of the do-while loop.

Example

var x = 1;
do {
console.log(x);
x++;
} while (x <= 10);

This is the same example that we had done in while loop example. We can see that the condition is checked at the end of the loop.

for Loop

for loop is more compact loop because loop initialization, test statement, and iteration statement all are in one line.

Basic syntax of for loop is

for (initialization; test condition; iteration statement) {
loop statement;
}

The cycle of for loop is

  • First initialization statement is executed
  • Then condition is checked, if satisfied** loop statement is executed.
  • Then iteration statement is executed and then test condition is checked.
  • This loop continues until test condition becomes false.
  • Then the loop will terminate.

Example

for (var i = 1; i <= 10; i++) {
console.log(i);
}

In this example the loop will print from 1 to 10.

Danger

While using while and do-while loop follow correct steps and use break if necessary, because due to logic error sometimes there will be no way for conditional expression to become false. This causes the loop to run infinitely.

Danger

While using for loop, a common problem we accidently do is putting semicolon after initializing of for loop. So, the output will be a single value which means loop is executed only one time.

for (var i = 1; i <= 10; i++);
{
console.log(i);
}

In this example the output will be 11.