Professional Documents
Culture Documents
Booleans and Comparison Operators: Word Soup: Conditionals, Booleans, Expressions, Statements
Booleans and Comparison Operators: Word Soup: Conditionals, Booleans, Expressions, Statements
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.
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.
Compares two values to see if the number on the left is less than the number on the right.
Compares two values to see if the number on the left is greaterthan or equal to the number on the
right.
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.
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.
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.
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
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.