0% found this document useful (0 votes)
17 views18 pages

Chapter 3 - Decisions and Loops-Update

Uploaded by

jenesias2919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

Chapter 3 - Decisions and Loops-Update

Uploaded by

jenesias2919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SECOND YEAR

IT- 22025
Web Development
Technologies I
Chapter – 3
Decisions and Loops

1
SECOND YEAR

Learning Outcomes
• Compare number and string values
• Make decisions with the if, else, and switch statements
• Repeat code for as long as a condition is true

2
SECOND YEAR

Overview
• Decision Making
• Looping

3
SECOND YEAR

Conditional Statements
In JavaScript we have the following conditional statements:

• if statement - use this statement if you want to execute some code only if a specified condition is true

• if...else statement - use this statement if you want to execute some code if the condition is true and
another code if the condition is false

• switch statement - use this statement if you want to select one of many blocks of code to be executed

4
SECOND YEAR

JavaScript if statements

if statements allow code to be executed when the condition specified is met; if the
condition is true then the code in the curly braces is executed. Here is the syntax
for an if statement:

if(condition){

//code to be executed if condition is true

5
SECOND YEAR

Example -1
<!DOCTYPE html> if (degCent < 0) {
<html lang="en"> document.write("That's below the freezing point of
<head> water");
<title>Chapter 3, Example 1</title> }
</head>
<body> if (degCent == 100)
<script> document.write("That's the boiling point of water");
var degFahren = parseInt(prompt("Enter </script>
the degrees Fahrenheit", 32), 10); </body>
var degCent = 5/9 * (degFahren - 32); </html>

document.write(degFahren + "\xB0
Fahrenheit is " + degCent +
"\xB0 centigrade<br />");

6
SECOND YEAR

Example-2
<html lang="en"> if ( myAge >= 80 || myAge <= 10 ) {
document.write("myAge is 80 or above OR 10 or below<br />");
<head>
}
<title>Chapter 3, Example 2</title>
if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) ) {
</head> document.write("myAge is between 30 and 39 or myAge is between 80 and 89");
<body> }
</script>
<script> </body>
</html>
var myAge = parseInt( prompt("Enter your age", 30), 10 );

if (myAge >= 0 && myAge <= 10) {


document.write("myAge is between 0 and 10<br />");
}

if ( !(myAge >= 0 && myAge <= 10) ) {


document.write("myAge is NOT between 0 and 10<br />");
} 7
SECOND YEAR

JavaScript If...else Statement


When you have two possible situations and you want to react
differently for each, you can use an if...else statement. This
means: “ If the conditions specified are met, run the first block of
code; otherwise run the second block. ”
The syntax is as follows:

if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is false
8
}
SECOND YEAR

JavaScript Switch
The JavaScript switch statement is used to execute one code from multiple expressions. Here is the syntax
for a switch statement:
switch (expression)
{
case option1:
code to be executed if expression is what is written in option1
break;
case option2:
code to be executed if expression is what is written in option2
break;
case option3:
code to be executed if expression is what is written in option3
break;
default:
code to be executed if expression is different from option1, option2, 9
SECOND YEAR

Switch Statement

10
SECOND YEAR

Using the Switch Statement

11
SECOND YEAR

Using the Switch Statement


Default:

12
SECOND YEAR

JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while or do while loops. It

makes the code compact. It is mostly used in array.

• for loop

• while loop

• do-while loop

13
SECOND YEAR

JavaScript For loop

The JavaScript for loop iterates the elements for the fixed number of times. It
should be used if number of iteration is known. The syntax of for loop is given
below.

for (initialization; condition; increment)

code to be executed

}
14
SECOND YEAR

JavaScript while loop


The JavaScript while loop iterates the elements for the infinite number of times. It should be used if
number of iteration is not known. The syntax of while loop is given below.

while (condition)

code to be executed

15
SECOND YEAR

JavaScript do while loop


The JavaScript do while loop iterates the elements for the infinite number of times like while loop.
But, code is executed at least once whether condition is true or false. The syntax of do while loop is
given below.

do{

code to be executed

}while (condition);

16
SECOND YEAR

Summary

• Comparing number and string values


• Making decisions with the if, else, and switch statements
• Repeating code for as long as a condition is true

17
SECOND YEAR

References
• Jeremy McPeak and Paul Wilton, “Beginning JavaScript”, Fifth
Edition
• Jon Duckett, “Beginning HTML, XHTML, CSS, and JavaScript”

18

You might also like