In this article we will learn about control statements in JavaScript. We will look at the syntax and description for each control statement.
Introduction
Statements that are used to control the flow of execution in a script are known as control statements. Control statements are used along with compound statements (a set of statements enclosed in braces).
Local variables are not allowed in a control construct (control statement + single/compound statement). Even though a variable is declared within a control construct, it is treated as a global variable.
Control Expressions
The expressions upon which the flow of control depends are known as control expressions. These include primitive values, relational expressions and compound expressions. The result of evaluating a control expression is always a Boolean value either true or false.
Selection Statements
Statements which are used to execute a set of statements based on a condition are known as selection statements. In JavaScript selection statements are: if, if-else and switch.
Syntax for an if statement is shown below:
if(expression)
{
statement(s);
}
Syntax for an if-else statement is shown below:
if(expression)
{
statement(s);
}
else
{
statement(s);
}
Syntax of a switch statement is given below:
switch(expression)
{
case label1:
statement(s);
break;
case label2:
statement(s);
break;
...
default:
statement(s);
}
The expression and labels in a switch statement can be either numbers, strings or Boolean values.
Loop Statements
Statements which are used to execute a set of statements repeatedly based on a condition are known as loop statements or iteration statements. Loop statements supported by JavaScript are while, do-while, for and for-in.
Syntax of while loop is given below:
while(condition)
{
statement(s);
}
Syntax for do-while loop is given below:
do
{
statement(s);
}
while(condition);
In a do-while statement condition is evaluated after the body of the loop is executed. So, the difference between while and do-while is, unlike in while loop, the body of the loop is guaranteed to be executed at least once in a do-while loop.
Syntax of a for loop is given below:
for(initialization; condition; increment/decrement expression)
{
statement(s);
}
JavaScript provides another loop statement to work with objects which is known as a for-in loop. It’s syntax is given below:
for(identifier in object)
{
statement(s);
}
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply