Lesson 3: Control Structures – If Statements, Loops, and Logic

Lesson 3: Control Structures – Circuits and Coding 101


📌 Introduction

Welcome back to Lesson 3 of Circuits and Coding 101! Today, we’ll make our programs smarter by introducing control structures.

So far, we've covered variables, constants, and basic input/output. Now, it's time to add logic and decision-making to our programs.


🎯 What You’ll Learn Today

  • Conditional Statements – Using if and else to make decisions
  • Logical Operators – Adding conditions to improve program logic
  • Loops – Automating repetition with for, while, and do-while
  • Hands-on exercises – Light-activated LED and LED blink pattern


🔄 What Are Control Structures?

Definition: Control structures let your program make decisions and execute code based on conditions.


Why are they important?

They make programs dynamic and interactive.

They allow responses to inputs, like turning on lights when it's dark.


✅ Conditional Statements


What are conditionals?

Conditionals execute code only if a certain condition is met.


Basic If-Else Syntax:

🛠️ Hands-On Exercise 1: Light-Activated LED🛠️

Let’s use a photoresistor to control an LED based on light levels.


🔧 Steps for Breadboard Setup:

  • Connect a photoresistor to pin A0 using a voltage divider.
  • Connect an LED to pin 13 with a resistor.

💻 Code:


🔁 Loops

What are loops?

Loops let a program repeat a block of code multiple times.


📌 Types of Loops:


  • for – Runs a block of code a specific number of times.
  • while – Repeats as long as a condition is true.
  • do-while – Runs at least once, then checks the condition.

💻 Examples:

For Loop: Print numbers 0-9

While Loop: Keep LED on while button is pressed

💡 Hands-On Exercise 2: LED Blink Pattern with Loops

Let’s make an LED blink in a custom pattern using a for loop.


🔧 Steps for Breadboard Setup:

  1. Connect an LED to pin 13 with a resistor.


💻 Code:

🚨 Common Pitfalls

  • Forgetting to set pin modes in setup().
  • Using = instead of == in conditions.
  • Miswiring components on the breadboard.


🏠 Homework Assignment

Write a program that:

  • Uses a photoresistor to control an LED.
  • Adds a button to toggle between blinking and staying on.


✅ Wrap-Up & Next Steps

What You Learned Today:

  • How to use conditional statements like if-else.
  • How to create loops to repeat tasks efficiently.
  • Built circuits to control LEDs based on input.


🎉 Great job! Next, we'll explore combining inputs and outputs for even more complex projects.


🔔 See you next time, and happy coding!