Introduction to Control Statement in JavaScript
In this article, we will be understanding what Control Statements are in JavaScript, how to use these statements and syntax for each. But before we move into the control statements part, let’s clear our understanding of the JavaScript Programming Language.
JavaScript, widely known as JS, is a high-level, light-weight and object-oriented programming language. JS has interpreted programming language, which means when executed, JS code does not comply but it is translated by a translator. This translator translates the JS code and loads the result into a web page on a browser. A JS Engine is a simple computer program that does the translation work, it is a Standard Interpreter. JS Engine efficiently harnesses the advantage of Just-In-Time compilation for better performance.
A normal statement in any program is intended to execute a command or say, do something. Control Statements are kind of same statements, but with power to decide which of the other statements should be executed and when. A very basic example, for anyone who understands programming, IF-ELSE is one of the control statement. The power these control statements have is based on an expression that does the job of evaluation.
Type of Control Statement in JavaScript
Every programming language, basically, has two types of control statements as follows:
- Conditional Statements: based on an expression passed, a conditional statement makes a decision, which results in either YES or NO.
- Iterative Statements (Loop): Until and unless the expression or the condition given is satisfied, these statements repeat themselves.
Examples
let’s understand these statements along with examples:
1. Conditional Statements
This is where the flow of the execution in a program is decided. Conditional Statements decide the next step based of the result. Conditional statement results in either True or False. Whatever the condition is passed, if that is true, then the program moves to the next step and if the condition is False, then the program moves to another step. These statements are executed only once, unlike Loop statements.
Following are the different types of Conditional Statements:
IF
when you want to check for a specific condition. With the IF condition, the inner code block is executed if the condition provided is satisfied.
Syntax:
if (condition) {
//code block to be executed if condition is satisfied
}
IF-ELSE
an extended version of IF. When you want to check a specific condition and two
Syntax:
if (condition)
{
// code to be executed of condition is true
}
else {
// code to be executed of condition is false
}
As you can see, when the condition is satisfied in IF-ELSE, the first block of code will be executed and if the condition isn’t satisfied, the second block of code will be executed.
SWITCH
A switch statement is similar to IF and is of use when you need to execute one code out of the multiple code block execution possibilities, based on the result of the expression passed. Switch statements carry an expression, which is compared with values of the following cases and once a match is found, code associated with that case executes.
Syntax:
switch (expression) {
case a:
//code block to be executed
Break;
case b:
//code block to be executed
Break;
case n:
//code block to be executed
Break;
default:
//default code to be executed if none of the above case is executed
}
The above code contains an expression at the very beginning, which is check and compared with the cases included. If the expression passed matches with the case a, the code block inside the case is executed. The same applies for case b and n, and when the expression passed matches with none of the cases mentioned, it code enters default case and the code under default case is executed.
Now, that we have understood the conditional statements, let’s learn about the second type, i.e. Iterative Statements.
2. Iterative Statement
Looping, for any programming language, is a powerful tool in order to execute a set of instructions, repeatedly, while the expression passed is satisfied. A very basic example can be, to print “Hello World” for 10 times. Now, writing the same print statement with “Hello world“ for 10 straight times will be time-consuming and will impact the execution time. And this is where looping comes handy. There are three Iterative statements: WHILE, DO-WHILE and FOR. Let’s understand each with syntax.
WHILE
one of the control flow statement, which executes a code block when the condition is satisfied. But unlike IF, while keeps repeating itself until the condition is satisfied. Difference between IF and while can be, IF executes code ‘if’ the condition is satisfied while the while keeps repeating itself until the condition is satisfied.
Syntax:
while (condition)
{
//code block to be executed when condition is satisfied
}
DO-WHILE
Similar to a while loop, with a twist that keeps a condition at the end of the loop. Also known as Exit Control Loop, DO-WHILE executes the code and then checks for the condition.
Syntax:
while
{
//code block to be executed when condition is satisfied
} (condition)
If the condition at the end is satisfied, the loop will repeat.
FOR
a for loop will execute a code block for a number of times. Compared to other loops, FOR is shorter and easy to debug as it contains initialization, condition and increment or decrement in a single line.
Syntax:
for (initialize; condition; increment/decrement)
{
//code block to be executed
}
With initialize, it starts the loop, here a declared variable is used. Then the exit condition for the loop is checked in condition part. When this condition returns true, the code block inside is executed. When, in case, if the condition returns false or fails, it goes to increment/decrement part and the variable is assigned an updated value. Values are updated until the condition is satisfied.
Conclusion
JavaScript has two types of control statements. Conditional and Iterative (Looping) with their particular uses. We learned about conditional statements like IF, IF-ELSE, and SWITCH, along with their respective syntaxes. And for Iterative Statements, we learned about WHILE, DO-WHILE and FOR along with syntaxes. A condition is passed and checked until satisfied.
Recommended Articles
This is a guide to Control Statement in JavaScript. Here we discuss the introduction and types of control statements in javascript which includes conditional statements and iterative statements. You can also go through our other suggested articles to learn more –
39 Online Courses | 24 Hands-on Projects | 230+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses