Control statements are like normal statements in any programming language but with the power to decide which statement will execute at which time. Examples of Control statements can be If-Else statements or switch statements.
In any program, a typical statement aims to execute a command or, in simple words, do something. Control Statements are similar to other statements. But they are different at a certain angle. They have the ability to determine which of the other statements should be executed and at what time it should be executed.
Control statements are intended to allow users to write scripts that determine which lines of code get evaluated and how many times they get evaluated. Control statements are classified into two types.
Types of Control Statements in JS
There are basically two types of control statements in Javascript. Namely, Conditional Statements and Iterative Statements (in simple words, Loops).
- Conditional Statements: These are the ones that make a decision. There is an expression that gets passed, and then the conditional statement decides on it. It is either YES or NO.
- Iterative Statements (Loop): This is a type of statement that keeps repeating itself till a condition is satisfied. In simple words, if we have an expression, until and unless it gets satisfied, the statement will keep repeating itself.
Examples of Control Statements
Now let us take a look at some examples and try to understand more about control statements.
Conditional Statements
This is where we decide the flow of a program's execution. Based on the outcome, Conditional Statements determine the next step. The outcome of a conditional assertion can be True, or it can be false. If the condition comes out to be true, the programme goes to the next step. On the other hand, if the condition comes out to be false, the programme goes out to a different. Unlike Loop statements, these statements get executed only once.
Below we can see the different types of Conditional Statements.
If Statment
As the name suggests, we use the If Statement if we want to check for a specific condition. When we are using the IF condition, the inner code block gets executed only when the given condition is satisfied.
Syntax:
if (condition_is_given_here) {
// If the condition is met, the code block will get executed.
}
If-Else Statement
The If-Else Statement is an extended version of the If statement. We use this one when we want to check a condition we want and another one if the evaluation turns out otherwise.
Syntax:
if (condition_is_given_here) {
// If the condition is met, this code block will get executed.
}
else {
/*If the condition is not met and turns out to be false, this code block will get executed. */
}
As we can see, the first block of code gets executed when the condition in IF-ELSE is satisfied. The second block of code gets executed when the condition does not get satisfied.
Switch Statement
A switch statement is similar to an IF statement. It is used when the user needs to execute one code block execution from their own choices out of several code block execution options. It is based on the outcome of the expression given. Switch statements hold an expression that is compared to the values of the following cases. If a match is found, the code associated with that case is executed.
Syntax:
switch (expression) {
case first:
// Here, we add the code block that is to be executed
Break;
// Break creates a separation between each case
case second:
// Here, we add the code block that is to be executed
Break;
case third:
// Here, we add the code block that is to be executed
Break;
default:
// Here, we add the default code block that is to be executed. It happens if none of the above cases is executed.
}
At the beginning of the code, an expression gets checked and compared with the cases given. If the given expression matches the first case, the code block present within the case gets executed. The same is true for the second case and the third case. However, when the expression passed matches none of the cases given, the code enters the default case and executes the code that is under the default case.
Now that we've covered conditional statements, it is time to move on to the second category, iterative statements.
Iterative Statement
Looping is a strong feature in any programming language. This is for repeatedly executing a sequence of instructions while the expression passed is met. A simple example would be to print "Hello Ninjas" ten times. Now, typing the same print statement with "Hello Ninjas" ten times in a row will take time. As an influence, it will also affect the execution time. This is the place where looping comes to play.
We should know that iterative statements are divided into three types: WHILE, DO-WHILE, and FOR. Let's break each down with syntax and take a look at them.
While Statement
One of the control flow statements is the. When the condition is met, they run a code block. However, unlike the IF statement, while keeps repeating itself until the condition is met. The difference between IF and while is that IF executes code when the condition is met. Whereas while statement keeps repeating itself till the condition is met.
Syntax:
while (the_condition_is_put_here)
{
// The code block is executed when the conditions are met
}
Do-While Statement
We can call the Do-While Statement a sibling of the While Statement. Similar to a while loop, but with the addition of a condition at the end of the loop. DO-WHILE, also known as an Exit Control Loop, performs the code and then checks for the condition.
Syntax:
while
{
// Code block gets executed till the condition is satisfied
} (we_add_the_conditon_here)
The loop will get repeated if the condition at the end is met.
For Statement
Now let us take a look at the For Loop. A for loop will run a code block several times. The FOR statement is shorter and easier to debug than other loops. This is because it contains initialization, condition, and increment or decrement on a single line.
Syntax:
for (here_we_put initialize; condition; increment/decrement)
{
// Here_we_put_the_code_block_that_we_need_to_execute
}
The loop is started using initialize. It uses a declared variable. The exit condition for the loop then gets tested in the condition section. The code block inside is run when this condition returns true.
Just think that the condition returns false or fails, and then it proceeds to the increment/decrement section. This is where the variable gets allocated an updated value. The values keep getting updated till the criteria or the condition gets met.