Page 135 - CITS - Computer Software Application -TT
P. 135
COMPUTER SOFTWARE APPLICATION - CITS
Control Flow
JavaScript control statement is used to control the execution of a program based on a specific condition. If the
condition meets then a particular block of action will be executed otherwise it will execute another block of action
that satisfies that particular condition.
Types of Control Statements in JavaScript
• Conditional Statement: These statements are used for decision-making, a decision
• n is made by the conditional statement based on an expression that is passed. Either YES or NO.
• Iterative Statement: This is a statement that iterates repeatedly until a condition is met. Simply said, if we
have an expression, the statement will keep repeating itself until and unless it is satisfied.
There are several methods that can be used to perform control statements in JavaScript:
Table of Content
• If Statement
• Using If-Else Statement
• Using Switch Statement
• Using the Ternary Operator (Conditional Operator)
• Using For loop
Approach 1: If Statement
In this approach, we are using an if statement to check a specific condition, the code block gets executed when
the given condition is satisfied.
Syntax:
if (condition_is_given_here ) {
// If the condition is met,
//the code will get executed.
}
Example: In this example, we are using an if statement to check our given condition.
• Javascript
const num = 5;
if (num > 0) {
console.log(“The number is positive.”);
};
Output
The number is positive.
Approach 2: Using If-Else Statement
The if-else statement will perform some action for a specific condition. If the condition meets then a particular code
of action will be executed otherwise it will execute another code of action that satisfies that particular condition.
Syntax:
if (condition1) {
// Executes when condition1 is true
if (condition2) {
// Executes when condition2 is true
}
}
122
CITS : IT&ITES - Computer Software Application - Lesson 37 - 46