[go: up one dir, main page]

0% found this document useful (0 votes)
10 views6 pages

Reference Material-Conditionals and Loops

Uploaded by

Yu En Hung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Reference Material-Conditionals and Loops

Uploaded by

Yu En Hung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

COMPUTER SCIENCE-2023-24

Module 5 - Conditionals and Loops


_________________________________________________________________________
Learning Objectives
● Describe the different types of operators such as Arithmetic, comparison, Assignment and
Logical.
● To learn the use of decision making statements.
● To identify the types of Loops.
______________________________________________________________________________________
INTRODUCTION

What is an Operator?
❖ Let us take a simple expression: 4 + 5 is equal to 9. Here 4 and 5 are called
operands and ‘+’ is called the operator.

JavaScript supports the following types of


operators.
● Arithmetic Operators
● Comparison(Relational) Operators
● Logical Operators
2

● Assignment Operators

NOTE: ++ increment the variable by 1. It is the equivalent to A = A+ 1.

-- decrements (decreases) the variable A by 1. It is the equivalent to A = A- 1.

Comparison Operators
3

Decision Making
A conditional/decision-making statement evaluates a condition before the instruction/s is executed.
4

IF STATEMENT

IF- STATEMENT IF- STATEMENT Flow chart

The if statement specifies a block of code


to be executed if a condition is true:

Syntax

if (condition) {

// block of code to be executed if the


condition is true

Eg:

num=prompt(‘Enter a number’);

if(num>0){
5

document.write(‘It is a positive number’);

IF -ELSE STATEMENT IF -ELSE FLOWCHART

The else statement specifies 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

Eg:

num=prompt(‘Enter a number’);

if(num>0){

document.write(‘It is a positive
number’);

Else{
6

document.write(‘It is a Negative
number’);

Note: document.write is a function that is used to display some strings in the output
HTML web pages
JavaScript Loops (A way to repeat a set of instructions multiple times)
Loops can execute a block of code a number of times.

Different loops in Javascript are:


● While Loop
● for Loop

While Loop For Loop

The while loop loops through a A For loop repeats until a specified
block of code as long as a specified condition evaluates to false.
condition is true. Syntax
Syntax for(InitialExpression; condition;
updateexpression)
while (condition) { {
//for loop body
// code block to be executed
}
}
Flow Chart
FlowChart

You might also like