Boolean Logic 01-1
Boolean values • Are TRUE and FALSE 01-2
Boolean values • Are TRUE and FALSE » Named after the English mathematician George Boole (1815-64) who developed Boolean algebra 01-3
Boolean values • Are TRUE and FALSE • They can be established in a number of ways 01-4
Boolean values • Are TRUE and FALSE • They can be established in a number of ways » Declaration 01-5
Boolean values • Are TRUE and FALSE • They can be established in a number of ways » Declaration » Comparison 01-6
Boolean values • Are TRUE and FALSE • They can be established in a number of ways » Declaration » Comparison » Boolean expression 01-7
Declaration • To declare a variable means to create it 01-8
Declaration • To declare a variable means to create it • Example: » In JavaScript declare the variable doorIsOpen and assert its value is true var doorIsOpen = true; 01-9
Comparison • Comparison operators are used to establish a relationship between objects of the same type 01-10
Comparison operators • < less than • <= less than or equal to • === equal to (strict includes operand types) • !== not equal to (strict includes operand types) • >= greater than or equal to • > greater than 01-11
Comparison operators • < less than • <= less than or equal to • === equal to (strict includes operand types) • !== not equal to (strict includes operand types) • >= greater than or equal to • > greater than » Comparison operators are infix and binary 01-12
Comparison operators • < less than • <= less than or equal to • === equal to (strict includes operand types) • !== not equal to (strict includes operand types) • >= greater than or equal to • > greater than » Comparison operators are infix and binary > The operator is between (infix) its two operands (binary) 01-13
Examples • maryAge < aliAge • year > 1582 • elephantWeight <= mouseWeight • thisYear === 2017 • thisYear !== leapYear 01-14
Boolean expression • Logical operators can be used to construct Boolean expressions from Boolean values 01-15
Boolean expression • Logical operators can be used to construct Boolean expressions from Boolean values » The operands of the operators must be Boolean 01-16
Logical operators • && and • | | or • ! not 01-17
Boolean expression example • Assume the value of year is a calendar year such as 2017 • The following expression is true if year is a leap year in the Gregorian calendar ( year % 4 === 0 // Remainder of year is 0 // on division by 4 && // AND year % 100 !==0 ) // Remainder of year is not 0 // on division by 100 | | // OR year % 400 === 0 // Remainder of year is 0 // on division by 400 01-18
Using the not operator ! ( year % 100 === 0 ) === ( year % 100 !== 0 ) 01-19
Using the not operator • The following expression is true if year is a leap year in the Gregorian calendar ( year % 4 === 0 // Remainder of year is 0 // on division by 4 && // AND ! ( year % 100 === 0 ) ) // Remainder of year is not 0 // on division by 100 | | // OR year % 400 === 0 // Remainder of year is 0 // on division by 400 01-20
Recommend
More recommend