Java Script conditional Boolean Logic Basis for modern

Java. Script conditional

Boolean Logic Basis for modern computer logic Works with only two values George Boole

BOOLEAN VARIABLE OR EXPRESSION Holds either true or false can pass as a literal Can be thought of as yes or no

Boolean expressions Operator Meaning == Is equal to != Is not equal > Is greater than < Is less than >= Is greater than or equal to <= Is less than or equal to Creates a Boolean value from numbers or strings

Conditionals Test if a Boolean expression is true or false Allows a program to make a choice

IF … THEN … ELSE if (MUST BE LOWERCASE) no else: do nothing if not true else: one or the other else if: series of choices

More complicated IF example Let’s assign letter grades to students! 90 - 100 – A 80 - 89 –B 70 - 79 –C 60 - 69 –D <60 –F

Decision Tree grade < 60 F grade < 70 grade < 80 D grade < 90 C B A

In Java. Script if (num < 60) { grade = "F"; } else if (num < 70) { grade = "D"; } else if (num < 80) { grade = "C"; } else if (num < 90) { grade = "B"; } else { grade = "A"; }

In Java. Script if (num < 60) { grade = "F"; } else if (num < 70) { grade = "D"; } else if (num < 80) { grade = "C"; } else if (num < 90) { grade = "B"; } else { grade = "A"; } Java. Script statement(s)

Combining Boolean expressions

Operations on Booleans AND Are all of them true? && Is it a big dog? (size==“big” && pet==“dog”) Number between 5 and 10 (num>=5 && num<=10) OR Are any of them true? || Is it either blue or black? (color==“blue” || color==“black”) Number less than 10 or greater than 20 (num<10 || num>20)
- Slides: 12