JS Control statements
JS Control statements
This type of conditional statement is applied when the conditional statements allow you to take one
action if a condition is true and another if it isn't. This is where the else statement comes in. By
placing the else statement after the if statement, it is linked to the if statement so that the
statement(s) it governs is evaluated if the condition in the parentheses of the if statement turns out
to be false. This saves writing out the condition again. In this way, either the statements the if
statement governs will be evaluated, or the statements the else statement governs will be
evaluated. After all a condition in JavaScript can only evaluate to true or false. Here are a
couple of examples to demonstrate how you would write this:
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.
Example:
Example
if (4 < 3)
alert("4 is less than 3");
else
alert("4 is greater than 3");
Since 4 is greater than 3, the alert after the if statement is ignored; therefore, the alert() function after the else statement
is evaluated. The else statement can also be used with a function block so you could write:
Example
if (4 < 3) {
alert("The if statement's statement block was evaluated");
alert("because 4 is less than 3");
}
else {
alert("The else statement's statement block was evaluated")
alert("because 4 is greater than 3")
}
SWITCH
This type of conditional statement is applied 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.
Example
<html>
<head>
<title>Switch Statement Demo</title>
<script language="javascript" type="text/javascript">
<!--
switch (1+1){
case "a":
alert(1);
case 2:
alert(2);
case true:
alert(3);
}
//-->
</script>
</head>
<body>
<h1>Switch Statement Demo</h1>
</body>
</html>
2. Iterative Statement (loop statements)
This type of control statement is a powerful tool which executes 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.
Types of Iterative statements
while,
do-while,
for.
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.