Control Statements
Site: | Philippine Science High School - MC - Knowledge Hub |
Course: | SY25.CS3.Web Development |
Book: | Control Statements |
Printed by: | , Guest user |
Date: | Thursday, 7 August 2025, 9:56 AM |
1. Introduction
After completing this module, you are expected to create corresponding branch statements to control flow of program based on a given set of conditions
Recall that during your Grade 8 Computer Science, you can make use of special statements to direct the flow of your code. These special statements are called control statements. JavaScript has several control statements:
- if....else [if].
- switch
- loop
The next section will discuss each control statement.
2. Conditional Statements
if-else [if] statement is used to execute different actions for different conditions. An action can be
defined for each condition met. The code below is the basic block for conditional statements:
2.1. Relational and Logical Operators
To effectively use if-else statements, you must learn how to properly declare conditional statements. If the conditions are not stated properly, if-else statements cannot execute their intended purpose.
Comparison operators are used in conditional statements to compare different values. You may recall
from your grade 8 Computer Science subject that these operators were also used. The table below lists the
comparison operators that can be used in JavaScript statements.
In more advanced programming problems, you may need to define more than one conditional
statement for a single line of if statement. To combine multiple conditional statements, we make use of logical
operators. The table below lists the different logical operators present in JavaScript.
2.2. An example on the use of if-else
Suppose that you are tasked to determine if a number is odd or even. The code structure below
provides a solution to this problem. (Note: Khub executes the window.alert statement in the code below)
- <script>
- x = 5
- if (x % 2 == 0){
- window.alert("The number is EVEN");
- }
- else{
- window.alert("The number is ODD");
- }
- </script>
2.3. Switch Statement
If there are multiple actions for multiple conditions, the code can be written with many layers of ifelse-if. But a more elegant solution to this is the use of switch. The code below is a sample block on how to
use switch.
Switch works by evaluating the expression stated and compares the value to each of the cases specified. You can specify as many cases as you want. If the value does not match any of the cases, the code block specified under default will be executed.
Notice that there are break statements after every case. This is so that the execution of the next
cases will be skipped. This is important because when the expression is true for case A, it can also be true for
case B. Look at the code below for an example:
The code above gives an equivalent alphabetic rating for a given numerical rating. A grade of less that
70 would merit a rating of “D.” A grade of less than 60 would merit an “E.” Let’s say that you are evaluating
the rating of a grade of 59. It is less than 60 and less than 70. When there is no break in between case
statements, it can be evaluated as true for both cases and the final alphabetical rating of the expression would
be from whichever case was declared last in the code.
3. Loop
Recall during your previous programming lessons that there are tasks that are executed repetitively, such as printing numbers in a specific range and iterating through an array. Instead of coding a line for each item, loops can be used to give instructions to the program to do an action repeatedly until a condition is met.
There are different kinds of loops depending on the structure and we will discuss only one here which is the for loop other loop structures will be taken in the fourth quarter.
The code below is an example of a for loop:
The start statement defines the base case, or the starting of the loop and is executed once before the code block is executed. The condition statement defines the conditions on which to run the loop. After the code block has been executed, the increment statement is executed.
All three statements in the definition of a for loop are optional. You may opt to remove one, two, or even all three of the statements in defining a for loop. Just make sure though that a break statement is present inside the loop to avoid infinite loops and that at least one variable is changing for every cycle so that the condition for the break statement will become true.
Given below is an example of how to use for loops. The example accepts a number and returns the sum of all integers from 1 to the given number.
<script>
sum = 0; a = 4
for(i=0; i<=a; i++){
sum = sum + i;
}
window.alert(sum);
</script>
`
There are more loops that are available in JavaScript. You will learn more about loops in in the succeeding lessons. For now, the for..loop structure introduced in this lesson is enough to help you solve programming problems and implement website features using JavaScript for this quarter.
4. Summary and References
Summary
Control statements are used to direct the flow of programs. It can be used to execute different actions based on different conditions.
References
JavaScript For Loop. (n.d.). JavaScript For Loop. W3Schools Online Web Tutorials. https:// www.w3schools.com/js/js_loop_for.asp
JavaScript if else and else if. (n.d.). W3Schools Online Web Tutorials. https://www.w3schools.com/js/ js_if_else.asp
JavaScript Switch Statement. (n.d.). W3Schools Online Web Tutorials. https://www.w3schools.com/js/ js_switch.asp
JavaScript While Loop. (n.d.). W3Schools Online Web Tutorials. https://www.w3schools.com/js/ js_loop_while.asp