Professional Documents
Culture Documents
Conditional Statements
In JavaScript we have the following conditional statements:
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
}
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
}
<!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
}
<!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”)
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”
Example Output:
FIRST QUARTER GRADE
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.)