Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views11 pages

Web Development Lesson 11 Notes

Uploaded by

HEAVENLY MANSION
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)
4 views11 pages

Web Development Lesson 11 Notes

Uploaded by

HEAVENLY MANSION
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/ 11

Diploma in Web Development

Conditional Logic
2

Contents
3 Working with JavaScript operators

6 Implementing conditional logic

11 Practical demonstration – source code


3

Lesson outcomes

In this lesson, we explore the use of operators in JavaScript. Operators are an excellent way to
perform arithmetic computation. It’s also a core part of implementing conditional logic in our code.
So come prepared with your thinking hats and prepare yourself for some basic algebra.

Working with JavaScript operators


JavaScript operators
JavaScript operators are used to assign and compare values, perform arithmetic operations, and
implement conditional logic. Below are the following operators in JavaScript.

 Arithmetic operators - used to perform arithmetic between variables and/or values.


 Assignment operators - used to assign values to JavaScript variables.
 Comparison operators - used in logical statements to determine equality or difference
between variables or values.
 Logical operators - used to test if one or more conditions are met. These are typically used
with boolean (logical) values.

Arithmetic operators

Operator Description

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus

++ Increment

-- Decrement

Examples of Arithmetic Operators


4

Operator Example Output

Addition z = x+y; 15
alert(z);

Subtraction z = y-x; 5
alert(z);

Multiplication z = (y-x)*2; 10
alert(z);

Division z = (x+y)/2; 7.5


alert(z)

Modulus alert (y%3); 1


alert ((x+y)%4); 3

Increment counter1++; 7
counterResult1=counter1
alert(counterResult1);
Decrement counter2--; 8
counterResult2=counter2
alert(counterResult2);

Assignment operators

Operator Example

= x=y

+= x += y

*= x *= y

-= x -= y

/= x /= y

%= x %= y
5

Comparison operators

Operator Example

== equal to

=== equal value and data type

!= not equal

!== not equal value and not equal data type

> greater than

< less than

>= greater than or equal to

<= less than or equal to

Logical operators

Operator Example

&& AND

|| OR

! NOT
6

Implementing conditional logic


Pseudocode
Pseudocode is a text-base design tool that helps software developers with writing informal code based
on logic.

Pseudocode example
IF it’s RAINING,

CARRY an UMBRELLA

IF it‘s SUNNY,

GO for a SWIM

IF it‘s SNOWING,

GRAB a SHOVEL

ELSE GO ABOUT OUR DAY

Conditional logic
Very often you may want to perform different actions for different decisions. To achieve this we ca use
conditional logic.

Conditional statements
 Use if to specify a block of code to be executed, only if a specified condition is true.
 Use else to specify the block of code to be executed, if the same condition is false.
 Use else if to test a new condition, if the first condition is neither true or false.
 Use switch statement to specify many alternative blocks of code to be executed.
7

if statement
8

else statement
9

else if statement
10

switch statement
11

Practical demonstration – source code


To run the following code, ensure that you ae using js.fiddle online editor.

Grade scale template

Symbol Mark range

A 90-100

B 80-89

C 70-79

D 60-69

F 0-59

1. var mark = prompt("Enter your mark to receive your grade");


2.
3. if(mark >=90)
4. {
5. alert("Congratulations! You obtained an A!");
6. }
7. else if (mark>=80)
8. {
9. alert("Well done! You obtained a B!");
10. }
11. else if (mark>=70)
12. {
13. alert("Not bad! You obtained a C");
14. }
15. else if (mark>=60)
16. {
17. alert("Hmm... You obtained a D. Better luck next time");
18. }
19. else if (mark>=0&&mark<=59)
20. {
21. alert("C'mon! You can do better than obtaining an F");
22. }
23. else
24. {
25. alert("You've entered an invalid mark");
26. }

You might also like