You are on page 1of 19

Qrio Level 1

Session 7: If Statement
Daily Life Examples of
Conditional Statements
If Statement!
It’s a very commonly used and important
command for any condition we want!

The if statement checks for a condition and


executes the following statement or set
of statements if the condition is 'true'.
How is its syntax!?
If the condition is
applied, then the
If(condition) { operations written
between { } will be
//operations to apply also applied!

} Example: if the
PushButton is
pressed, turn on the
LED!
In the conditions,
numbers can be used!

Random Example:
Be Careful!
Do not use single equal (=) sign alone!
The equal sign will assign x to a value,
which will make the condition always
TRUE!!
These comparison operations can be used as condition in the
If Statement
Let’s make an Example!
What if I want to have a
PushButton and a LED, in
condition that the LED won’t
turn On Unless the
PushButton is pressed
and it only turns Off
when it’s released?
If button is pressed If we get 5v from ButtonPin
turn on the LED put 5v on LEDpin
else else
turn off the LED put 0v on LEDpin

If we read 1 from ButtonPin


write 1 on LEDpin
else
write 0 on LEDpin
Let’s consider
at first that we
connect the
LED to pin 9
and the
PushButton to Attention!
pin 8. const: is used when we want
a variable that will remain
the same whatever happens.
int: is used for a variable that
can be varied throughout the
upload time.
The
Code
The Circuit
When pushButtton
is pressed  LED On
When pushButtton
is released LED Off
Why do we need to insert a resistor
for the pushbutton switch?
The idea is to avoid an open
circuit on the digital input.
When the pushbutton is
open, the resistor pulls the
input to the connected supply
rail.
When the pushbutton is
closed, the input is at the
ground supply rail.
Another task you can make is
‘Blinking LEDs with PushButton’!
1. The LEDs will start blinking once the
button is pressed
2. The LEDs will stop
blinking once the
button is released
Let’s Challenge!
Make a code of a LED and a
PushButton.
Whenever the button is pressed, the
LED will blink each time faster than
before!
Code:
•Good Job•
See You Next Week!

You might also like