if Structures, Variables
and Assignments
making decisions and memory
What is an if if’s are for decision making.
structure? “conditional” expressions -
choosing a course of action based
on a potentially changing condition
Simple
if this then that
decisions
Syntax
if (this ) {
Do that
Example:
if (x < 9) {
Serial.println(“x is less than 9.”);
}
Flow chart for IFTTT process:
True
Start “x is less than 9”
x<9
False
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”
One of these two
False actions occurs,
but not both
“x is NOT less than 9” End
Level 3:
Multiple
Mutually Exclusive
Execution Paths
decisions
Syntax - multiple execution paths
Example:
if (this ) {
if (x < 9) {
Do that
Serial.println("x is less than 9.");
} else if ( this ) {
} else if (x > 9) {
Do something
Serial.println("x is greater than 9.");
} else {
} else {
Do something else
Serial.println("x must be 9");
}
}
Flow chart for multi-mutex process:
True
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
May have multiple else if’s otherwise if
May finish with an else if all else fails
Think of them as “cases”: If this is the case, then do that
Syntax
if ( conditional expression ) {
action / code to perform and execute
} else if ( another conditional expression ) {
another action
} else {
action to perform if all other cases are false
}
“Simple Reflex” Example
void loop() {
int temperature=getTemp(); // let’s imagine that we get the temp in deg f
if( temperature < 70 ) {
turnOnHeat();
} else {
turnOffHeat();
}
Control Expressions
Relational Operators - compare values
== Equal-to
!= Not Equal-to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Boolean or Logic operators
&& AND Returns True if both sides are True,
otherwise False
|| OR Returns False if both sides are
False, otherwise True
! NOT Returns the opposite value, true or
false
Boolean operators examples
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.
They are CONTAINERS like for lunch.
What is a variable?
A variable is a container. You can put one value in it at a time.
The value must be of the type that the variable holds.
Think sandwich vs. soup lunch containers:
A variable is a named memory location. You utilize and
access computer memory using variables in code.
Variables and 1. create/declare the variable by
type and name
Assignments 2. assign a value using
(in a nutshell) assignment operator =
3. use the variable name
In code, when you want the
anywhere you want its current
program to remember
value
something, you must assign the
value to a variable
Variable Declaration
syntax:
type name;
type name=value;
ex: This line creates a
variable called
int sensorValue;
“sensorValue” of
int myValue=analogRead(A0); type int
int age=26;
Assignment Operator
Looks like equal sign =
Do not call it “equals”
On its left MUST be a variable.
On its right MUST be: a literal value, a variable, or an expression.
If you see a single
= then the thing
on its left IS a (otherwise you’ll get a
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;
What does this line do?
Actually it does two things:
1. x+1 is evaluated and results in the value of the variable x plus 1
2. the resulting value is assigned to x
therefore, following SEQUENCE, if the value of x was 5 BEFORE this
line, its value is 6 after this line.
Using variables as values
Put the variable name where the value would go.
ex:
int age=26;
Serial.println(age); // this will print the value 26
type kind used for range
Variable Type int integer counting -32,768 to 32,767
float decimal dec. values
This is the “kind” of data that can be
stored in a variable.
Some types are compatible and some
are not - sometimes data is lost in a
type conversion.
Variable Naming Rules
1. You may use ONLY letters, numbers, and the underscore _
1. You may NOT begin with a number
1. You may NOT use any reserved words
* remember, they are case sensitive
Valid Names Invalid
Names
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 storing somebody’s age, use age instead of x.
If you’re storing the temperature, use temp instead of t.
If you’re keeping track of an input pin number, use inputPin instead of the literal
value.
Literals
literals are any actual values you see written in code.
For example:
123
4.642
“Hello” name is NOT a literal because that would
be a variable. It doesn’t have quotes
name
around it so it’s not a string literal.