Summary
Summary: 3.8.0
Conditionals
are used to execute code with a boolean expression- boolean expression is either true or false
Concept Summary: 3.8.1
- Block of statements
- One or more statements enclosed in curly braces
- Boolean expression
- A mathematical or logical expression that is either true or false.
- compound Boolean expressions
- A Boolean expression with two or more conditions joined by a logical and
&&
or a logical or||
.
- A Boolean expression with two or more conditions joined by a logical and
- conditional
- Used to execute code based on a boolean expression.
- DeMorgan’s Laws
- Rules about how to distribute a negation on a complex conditional.
- logical and
- Used in compound boolean expressions that are true if both conditions are true.
- logical or
- Used in compound boolean expressions that are true if one of the conditions is true.
- negation
- turns a true statement false and a false statement true
- short circuit evaluation
- A method of evaluation used when a conditional statement can be determined without evaluating the entire statement
- Used with logical and and logical or
a && b && c && d
- If
a == false
, the rest of the statement doesn’t need to be evaluated to know the result will be false
- If
Keyword summary: 3.8.2
if
- Used to start a conditional statement
- followed by a statement/block which is executed if the expression is true
else
- execute a statement/block if the boolean expression in the
if
was false
- execute a statement/block if the boolean expression in the
else if
- Tests another condition if the first condition evaluated false
- used in conjunction with
if
Common mistakes: 3.8.4
- Using
=
instead of==
in conditional statements - putting a semicolon at the end of an if statement
- ends the if before you can do anything
- using two ifs instead of an
if
and anelse
orelse if
- Trouble with compound boolean expressions
- Not understanding
||
is an inclusive-or - Not understanding or applying negation
- De Morgan’s Laws
- Not understanding short circuit evaluation
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.