1 of 19

Foundations of Technology

2 of 19

Introduction to Computer Science Principles byDesign

Unit 1: Computer Science Principles byDesign

Learning Cycle 3: Logic: If/Else, Loops, AND, OR, NOT (Microbit)

Foundations of Technology

2

3 of 19

Big Idea

In this presentation we will learn how to use If/Else, Loops, AND, OR, NOT in programing the microbit to solve engineering problems.

Foundations of Technology

3

4 of 19

What is a Logic?

  • In programming a logic refers to statements or expressions that utilize a boolean value. A true or false value is called a boolean or bool.

Foundations of Technology

4

5 of 19

Logic blocks

There are three groups of blocks under the logic:

Conditionals: If/else

The statements inside the if block

will be executed only when the value

inside the hexagon is “TRUE”

5

6 of 19

Example: Conditional

if the boolean statement is

true, the LED will appear

in the left,

Otherwise, it stays

in the center

6

7 of 19

Logic Blocks

Comparisons:

Use comparator operators

to evaluate whether the

comparison is true or false.

=, >,<, ≥, ≤,≠

7

8 of 19

Example: Comparison

The boolean statement

is false so the else is

executed

8

9 of 19

Logic Blocks

Boolean:

Using the logical operators AND, OR,

NOT to evaluate expressions (whether

TRUE or FALSE

(Boolean is the technical term for a

data type that can only be true

or false

9

10 of 19

Example: Boolean

True and True will return

a true

10

11 of 19

Truth tables for AND, OR, NOT

11

AND

TRUE

FALSE

TRUE

TRUE

FALSE

FALSE

FALSE

FALSE

OR

TRUE

FALSE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

NOT TRUE

FALSE

NOT FALSE

TRUE

12 of 19

Example: Boolean

True and False will return

a False

12

13 of 19

Example: Try this Program

Initialize the variables

score and Goal

When button A is

pressed, score increases

by 1.

When score equals Goal,

servo moves to 135 and

microbit displays smiley face,

otherwise, servo stays at 90.

13

14 of 19

Example: Try this Program

Initialize the variables

score and Goal

When button A is

pressed, score increases

by 1

When score equals Goal,

servo moves to 135 and

microbit displays smiley face,

otherwise, servo stays at 90

14

15 of 19

Loops

Repetition (iteration) of a command/statement or a group of commands/statements

15

16 of 19

Loops

Repetition can be

controlled by a boolean

or a number/index

16

17 of 19

Loops

Repetition can be

controlled by a boolean

or a number

17

18 of 19

Try this:

  • For movement, use servo (inside the PINS or SERVO)
  • For sound use the ringtone or play tone (inside MUSIC)
  • For LED display use displays in BASIC
  • Scenario: when the room is full, close the gate, sound an alarm, flash a warning
  • Estimate/count people coming in by any trigger (shake, button press, temperature, light).

18

19 of 19

Example of Possible Solution

19