CurriculumGrades 6-8Computer Science

Programming with variables, conditionals, and functions

Aligned to CSTA-AP — CSTA K-12 Computer Science Standards.

What this lesson teaches

Variables store information your program can use and change, like a score or a name. Conditionals (if/else) let a program make decisions, doing one thing when something is true and another when it is false. Loops repeat work, and functions bundle steps into a reusable command. These same structures appear in every programming language, including Python.

Worked example

In a guessing game, a variable holds the secret number. A conditional checks: if the guess equals the number, print You win, else print Try again. A loop lets the player keep guessing until they get it.

Practice questions

  1. What does a variable do in a program?
  2. Write an if/else rule for a program that says Pass if a score is 60 or higher, else Retry.
  3. Why is it useful to put repeated steps into a function?

Watch the lesson

Every lesson comes with a video taught in English and Spanish — the same video the QR code in the printed workbook opens.

▶ Watch this lesson

En español

Programación con variables, condicionales y funciones

Las variables guardan información que tu programa puede usar y modificar, como un puntaje o un nombre. Los condicionales (if/else) permiten que un programa tome decisiones, haciendo una cosa cuando algo es verdadero y otra cuando es falso. Los ciclos repiten trabajo, y las funciones agrupan pasos en un comando reutilizable. Estas mismas estructuras aparecen en todos los lenguajes de programación, incluyendo Python.

Ejemplo: En un juego de adivinanzas, una variable guarda el número secreto. Un condicional verifica: if el número adivinado es igual al número secreto, imprime Ganaste, else imprime Inténtalo de nuevo. Un ciclo deja que el jugador siga adivinando hasta que lo logre.

More Algorithms & Programming lessons

Computational thinking: decomposition and abstractionCSTA-AP

Teach this lesson today

An AI tutor that guides with questions instead of just giving answers — all K–12 core subjects, every child in the family.

Start free — no card required