0% found this document useful (0 votes)
5 views

JS Control statements

The document provides an introduction to JavaScript control statements, which are essential for creating scripts that can make decisions and execute code based on conditions. It outlines two main types of control statements: conditional statements (IF, IF-ELSE, SWITCH) and iterative statements (while, do-while, for). Each type is explained with syntax and examples to illustrate their usage in programming.

Uploaded by

nalumansitheresa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

JS Control statements

The document provides an introduction to JavaScript control statements, which are essential for creating scripts that can make decisions and execute code based on conditions. It outlines two main types of control statements: conditional statements (IF, IF-ELSE, SWITCH) and iterative statements (while, do-while, for). Each type is explained with syntax and examples to illustrate their usage in programming.

Uploaded by

nalumansitheresa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction Website Design

Course Code : DCIS1204


Course Level : Semester Two Year One
JAVASCRIPT CONTROL STATEMENTS
JS Control Statements
Control statements are designed to allow you to create scripts that can decide which lines of code
are evaluated, or how many times to evaluate them. There are two different types of control
statements: conditional statements and Iterative Statements (also called loop statements).
In other words control statements have the power to decide which of the other statements should be
executed and when.

Types of control statements


There are two types of Control Statements
1. conditional statements
2. Iterative statements(loop statements)
1. Conditional Statements
Conditional statements are used to make decisions. In real life, we make all sorts of decisions based
on criteria such as "am I being offered enough money to take this job?" If the answer is "yes," then
the result is "take the job." If the answer is "no," then "don't take the job." In JavaScript, you need to
make decisions about which sections of code to evaluate. For example, if you asked a user for input
so you could perform a calculation, you will want to carry out the calculation if the input is
numeric, but not if it isn't.
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.
Types of Conditional Statements
 IF
This type of conditional statement is applied 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
}
NOTE:
It is useful to use curly braces even when the if statement has only one statement associated with
it. Then if you later need to add other statements, you won't encounter errors caused by forgetting to
add the curly braces.
 IF-ELSE

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.

You might also like