You are on page 1of 3

Booleans and Comparison Operators

Word Soup: Conditionals, Booleans, expressions, statements


As you saw in the video there are a bunch of terms that generally are all talking about the same thing.
Historical Notes on Booleans
Boolean, Boolean values, Boolean expressions:
Named after mathematician George Boole
A Boolean value is simply a computer science-y term that means a true/false value. He invented a corner of mathematics that is now
A Boolean expression is a statement that evaluates to a Boolean value (a single true/false). named after him called "Boolean Algebra"
Boolean Algebra is math that operates using only
Condition, Conditionals, Conditional Statements:
true/false values.
"Conditional" is simply a generic term for code that alters program flow based on true/false values (like an This is important work for computer science
if statement) because true/false maps very easily to binary.
Examples: Condition, Conditionals, Conditional statements, conditional execution

Comparison Operators

A common type of condition to check is a comparison of two values. Here are 6 common comparison operators. Each compares a value on the
left with a value on the right and returns a Boolean value -- true or false. Most of these do what you would expect.

Why these symbols: ==, !=, <=, and >=?


1. We use == because the single equal sign = is the assignment operator. We need something different to indicate we want to compare two values
instead of assign one to the other.

Common mistake: writing something like if (age = 18) instead of if (age == 18) . We'll make sure we get this down later.

2. We use != , <= , and >= because they only require ASCII symbols. Historically the mathematical symbols ≠ , ≤ and ≥ were hard or impossible
to produce on some systems. The ! is universally read as "not".

Reference: Examples
Below are a bunch of examples of how you might see comparisons in code. Review them if you like or continue on and come back if you need reference.

Compares two values - numbers, strings, or other booleans - and returns trueif they are equal, otherwise false.

"Hello" == "hello" returns false -- because the strings are are capitalized differently.
A string is data that is meant to be read as English (i.e. not as a number)
"3" == 3 returns true-- because == tries to be forgiving. It notices one of the inputs is a number and
tries to convert the string "3" into a number before making the comparison.
(2+1) == 3 returns true-- because the arithmetic expression evaluates to 3. x == 7 returns true-- when the
variable x has the value 7.

Compares two values - numbers, strings, or other booleans - and returns true if they are not equal
,
otherwise false .

"Hello" != "hello" returns true-- because the strings are slightly different.
"3" != 3 returns false -- because the computer recognises you are comparing a string and a number. It tries
to convert the string "3" into a number before comparing it with 3.
(2+1) != 3 returns false-- because the arithmetic expression evaluates to 3.
x != 7 returns true
-- when the variable x is any value other7than

Compares two values to see if the number on the left is greater than the number on the right.

4 > 3 returns true


3 > 7 returns false
age > 17 returns true -- when the value of the variable "age" is strictly greater than 17, otherwise false.

Compares two values to see if the number on the left is less than the number on the right.

4 < 3 returns false


3 < 7 returns true
age < 17 returns true -- when the value of the variable "age" is strictly less than 17, otherwise false.
Compares two values to see if the number on the left is less than or equal to the number on the right.

3 <= 4 returns true


4 <= 3 returns false
age <= 18 returns true -- when the value of the variable "age" is 18 or less.

Compares two values to see if the number on the left is greaterthan or equal to the number on the
right.

3 >= 4 returns false


4 >= 3 returns true
age >= 18 returns true -- when the value of the variable "age" is 18 or greater.

If Statements
Understanding Program Flow
Programs are said to have a "flow of execution". You start by executing a line of code, then the next, then the next, and so on.

A flow chart is a common visual used to represent the various paths of execution
that your program might take. Many people use them to help plan programs.

1. This flow chart depicts a program executing one line after another until it
gets to a point where it needs to make a decision.

2. In order to determine which path to take you state some condition. It


should be a Boolean expression - something that evaluates to true or
false. Here we have a simple comparsion of two values: the person's
age and the number 18.

3. The program does one thing if the condition is true, and something else if
the condition is false.

4. The program can continue a single thread of execution after the condition
as well.

How If-statements work


if statements are the lines of code
you use to change the flow of a
program while it's running. You can
write code that determines which
lines of code should run next.

At the right is a diagram showing


the elements of a basic if statement
in JavaScript.

There are two basic parts to an if-


statement.

1. A condition to be evaluated (A Boolean expression that evaluates to true or false)


2. Code that should run if the expression was true - enclosed in curly braces
If-Else Statements
How If-Else Statements work
With an if-else statement you are giving an either-or command:

either the lines of code inside the if will execute or the lines inside the else will
execute. Those are the options.

You saw in the video how to add an else clause to an if-statement -- hit the little +
symbol on the tail of the if statement.

Inside the curly braces for the else clause you put lines of code that you want to run if
the Boolean condition from the if statement is false.

Some important notes about the else clause:

The else must come immediately after the closing curly brace of an if statement
The else also has its own set of opening and closing curly braces to encapsulate
lines of code

Considering our flow chart from before, until now we haven't had a way to make the program do something different if the
condition was false. With an if-else statement we do.

We can now write a program that "branches" at a particular point, running one of two possible sections of code.

A Worked Example

1. Lines of code execute


sequentially as usual.
Prompt the user to
enter their age.

2. The if statement and


Boolean expression are
also the same as
before. The expression
evaluates to either
true or false .

3. With an if-else
statement you are
guaranteeing that
exactly one of these
two sections of code
will execute. If the condition is true (age is 18 or greater) then the lines of code inside the if-statement's curly braces are executed. If the condition is false it
jumps to the else clause and executes any lines of code it finds between the else clause's curly braces.

4. Finally the program picks up normal execution directly after the if-else block. At this point in the program, we know that either the code in the if-block or the else
block has executed.

If you are interested in licensing Code.org materials for commercial purposes, contact us.

You might also like