3 - If Structures, Variables and Assignments
3 - If Structures, Variables and Assignments
and Assignments
making decisions and memory
What is an if if’s are for decision making.
if (this ) {
Do that
Example:
if (x < 9) {
}
Flow chart for IFTTT process:
True
End
Level 2:
This or That
Decisions
“Fork in the road”
Syntax of if-else decision structure
Example:
if (this ) {
if (x < 9) {
Do that
Serial.println("x is less than 9.");
} else {
} else {
Do something else
Serial.println("x is NOT less than 9.");
} }
Flow chart for This or That process:
True
Start
x<9 “x is less than 9”
Start
x<9
if (x<9) True
“x is less than 9”
False x>9
else if (x>9)
“x is greater than 9”
False
else
“x must be 9" End
Syntax of an if-block
Start with if if
another action
} else {
}
“Simple Reflex” Example
void loop() {
turnOnHeat();
} else {
turnOffHeat();
}
Control Expressions
Relational Operators - compare values
== Equal-to
!= Not Equal-to
GIVEN:
x=5;
y=10;
RELATIONAL EXPRESSIONS:
x<y is True
x>y is False
BOOLEAN EXPRESSIONS:
x<10 && y<10 is False
x<10 || y<10 is True
!(x<10 || y<10) is False
Variables
They are not PLACEHOLDERS like in math.
type name;
type name=value;
variable
syntax error)
Assignment vs. Equal-to
= is the == is the
assignment operator equal-to operator
This slide will upset math thinkers
x=x+1;
ex:
int age=26;
abc123 123abc
my_name my name
_hello0 hello!
blahblah for
ThIsIsFiNeToUsE No-Can-Do-Compadre
a1b2c3
for_me
FOR
PRO TIP: Use meaningful variable names
If you’re keeping track of an input pin number, use inputPin instead of the literal
value.
Literals
For example:
123
4.642