1 of 25

2 of 25

Intro to if Statements

Using Pushbuttons in Tinkercad Circuits

3 of 25

Today’s Task

  • Learn about different data types and when to use them
  • Learn to use if/else statements in coding to determine outputs
  • Activity:
    • a) LED circuit with single push button input. Pushing the button turns the light on. Otherwise, the light is off. Program using if/else statements
    • Bonus: Add a second LED. When LED1 is on, LED2 should be off. When LED1 is off, LED2 should be on.

4 of 25

What is a data type?

5 of 25

What is a data type?

A data type in programming stores a certain kind of value or information

6 of 25

What are examples of data types?

  • int : whole integer values

  • double : decimal values

  • char : single letter values

  • String : multiple char values

  • Boolean bool : true or false values

7 of 25

Let’s switch gears into:

if/else statements

8 of 25

What is an if statement?

9 of 25

What is an if statement?

An if statement will only run the code inside it when a certain condition is true

10 of 25

if statement

Let’s look at a few if statements. Assume int val = 5;

Will these statements be true or false?

  1. if (val == 5)

2. if (val != 4)

3. if (val < 3)

4. if (val > 5)

5. if (val >= 5)

11 of 25

if statement

Let’s look at a few if statements. Assume int val = 5;

Will these statements be true or false?

  1. if (val == 5)

true

2. if (val != 4)

3. if (val < 3)

4. if (val > 5)

5. if (val >= 5)

12 of 25

if statement

Let’s look at a few if statements. Assume int val = 5;

Will these statements be true or false?

  • if (val == 5)

true

2. if (val != 4)

true

3. if (val < 3)

false

4. if (val > 5)

false

5. if (val >= 5)

true

13 of 25

What is an else statement?

14 of 25

What is an else statement?

An else statement will run the code inside it when the previous if statement isn’t true

You can’t have an else statement without an if

15 of 25

else statement

Let’s look at a few if/else statements.

What is the condition for the else?

  1. if (val == 5)

---

else

---

2. if (val > 5)

---

else

---

16 of 25

else statement

Let’s look at a few if/else statements.

What is the condition for the else?

  1. if (val == 5)

---

else // val != 5

---

2. if (val > 5)

---

else // val <= 5

---

17 of 25

Let’s build our circuit!

18 of 25

Start from one pushbutton circuit

19 of 25

Declare variables BEFORE setup()

So far, we have only written code within the brackets of setup() and loop()�

Now, we are declaring our variables at the beginning of the program before both functions so they can be used throughout the whole program.

20 of 25

Declare pinMode()

21 of 25

If la is true, turn on led1

* The LED depends on boolean value la

22 of 25

If la is not true, turn off led1

23 of 25

If the button is pressed, set la to false

*boolean value la depends on whether or not the button is pressed

24 of 25

If the button is not pressed, set la to true

*boolean value la depends on whether or not the button is pressed

25 of 25

Run your code!