You are on page 1of 5

Name: Date: / /2021

ICT-CBP Worksheet : Javascript

Conditional Statements
In JavaScript we have the following conditional statements:

• Use if to specify a block of code to be executed, if a specified condition is true


• Use else to specify a block of code to be executed, if the same condition is false
• Use else if to specify a new condition to test, if the first condition is false
• Use switch to specify many alternative blocks of code to be executed

The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.

Syntax

if (condition) {
// block of code to be executed if the condition is true
}

Example: filename: ifProg#4


<html>
<body>
<p>Example of If Statement:
<br><br>Displays "I Love You!" if the answer is Yes.</p>
<script>
var ans=" "
ans=prompt("Do you love me?","YES/NO")
if (ans=="YES"||ans=="yes"||ans=="Yes") {
document.write("I LOVE YOU!");
}
</script>
</body>
</html>
The if…else Statement
Use the else statement to specify a block of code to be executed if the
condition is false.

if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

Example: filename: ifElseProg#5

<!DOCTYPE html>
<html>
<body>
<p>Example of If...else Statement:
<br><br>Displays the answer to the question, "Do you love me?</p>
<script>
var ans=" "
ans=prompt("Do you love me?","YES/NO")

if (ans=="YES"||ans=="yes"||ans=="Yes") {
document.write("I LOVE YOU!");
}
else
{ document.write("Let's be Friend!")
}
</script>

</body>
</html>
The else if Statement
Use the else if statement to specify a new condition if the first condition is
false.

Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}

Example: filename: IfElseifElseProg#6

<!DOCTYPE html>
<html>
<body>
<p>Example of If...else Statement:
<br><br>Displays the answer to the question, "Do you love me?</p>
<script>
var ans=" "
ans=prompt("Do you love me?","YES/NO/MAYBE")
if (ans=="YES"||ans=="yes"||ans=="Yes") {
document.write("I LOVE YOU!");
}
else if (ans=="NO"||ans=="No"||ans=="no")
{ document.write("Let's be Friend!")
}
else{
document.write("I will think about it. I need more time.")
}
</script>
</body>
</html>
Performance Task Activities #2:
PT#1 Conditional Statement Filename: gradePT#1.html
• Use HTML, CSS and JavaScript
• Create a program that will perform the following tasks in sequence:
a. Declare the variables (5pts)
Ex. var studentName=” “

b. Get the student names & each subject grades from the user (10pts)
Ex. studentName=prompt(“Enter student name: “,”Student Name”)

c. Get the average of the grades (10pts)


Ex. ave= (g1+g2+g3+g4+g5+g6+g7+g8)/8

d. Determine the grades remark (10pts)

Remark:
ave>=95 “OUTSTANDING”
ave>=90 and ave<95 “EXCELLENT ”
ave>=85 and <90 “VERY GOOD”
ave>=80 and <85 “GOOD”
ave>=75 and <80 “SATISFACTORY”
ave<75 “FAIL”

e. Display the results (10pts)


Ex. document.write(“<br> Student Name”,studentName)

Example Output:
FIRST QUARTER GRADE

Student Name: Anya Claudya Racab

English = 90
Filipino =95
Math =90
Science =89
A.P. =89
T.L.E. =93
E.S.P. =90
MAPEH =95

Averge: 91
Remark: EXELLENT
f. Copy and paste here the source code (5 pts.)

You might also like