انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents

The counter: a box that keeps a number

Section 2: VariablesLesson 2.1 · ⏱ 10 min

A variable is a box with a name that keeps a number. The app remembers it and changes it. Here its name is count.

When the app opens count = 0, and the two buttons add to it or take from it, then show it
When the app opens count = 0, and the two buttons add to it or take from it, then show it
We tapped +1 three times, then -1 once: 2 appeared
We tapped +1 three times, then -1 once: 2 appeared
  • set count to 0 when the app opens: counting starts from zero.
  • change count by 1 adds one, and by -1 takes one away.
  • After the change we show the number in Label1, otherwise we won't see it.

📌 Another example: a counter that steps by 5, and a zero button

+5 adds five, and “Zero” takes the counter back to the start.

The design
The design
The blocks
The blocks
In the lab: the counter
In the lab: the counter

Test yourself

1. count = 0. We tapped +1 five times and -1 twice. What appears?
✍️ Practise
The challenge: the counter.
✍️ Practise here in the lessonor open the lab in a new page ↗
✍️ Practise
Practice: plus and minus.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A counter that adds 2 with every tap.

👀 Show the solution
The design
The design
The blocks
The blocks
In the lab
In the lab

Result: after 3 taps: 6

Medium

A countdown counter: it starts at 10 and takes away 1.

👀 Show the solution
The design
The design
The blocks
The blocks
In the lab
In the lab

Result: after 3 taps: 7

Hard

When the counter reaches 10 or more the screen turns green.

👀 Show the solution
The design
The design
The blocks
The blocks
In the lab
In the lab

Result: at 10 the screen turns green.

Finished the lesson? Mark it so your progress counts.