Chapter 3 - Decisions and Loops-Update
Chapter 3 - Decisions and Loops-Update
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){
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 (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
11
SECOND YEAR
12
SECOND YEAR
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while or do while loops. It
• for loop
• while loop
• do-while loop
13
SECOND YEAR
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.
code to be executed
}
14
SECOND YEAR
while (condition)
code to be executed
15
SECOND YEAR
do{
code to be executed
}while (condition);
16
SECOND YEAR
Summary
17
SECOND YEAR
References
• Jeremy McPeak and Paul Wilton, “Beginning JavaScript”, Fifth
Edition
• Jon Duckett, “Beginning HTML, XHTML, CSS, and JavaScript”
18