📘 CodingMarble Learn

Programming Basics: Sequence, Selection, Loops and Functions

A program is a set of exact instructions a computer follows. Every program is built from three structures: sequence (steps in order), selection (if/else choices) and iteration (loops). Variables store values. Functions group code into reusable, named blocks, which makes programs modular and easier to test, debug and maintain.

🎬 Step-by-step story

  1. A program is a list of exact steps. Watch each move() line light up as the robot takes one step.
  2. A variable is a box with a name. score starts at 0, then 5 is added, then 5 more.
  3. An if lets the program choose. At each tile it asks: wall ahead? Only when the answer is yes does it stop.
  4. A loop repeats lines. Two lines of code make four moves, while a counter goes 1, 2, 3, 4.
  5. A function is code you name once and reuse. Then a bug: the robot stops one tile short. Fix the number and test again.
  6. Your turn. Set the loop count and the wall check. Predict first, then run.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why can't the computer just guess what I mean?

A computer follows instructions exactly, in order. If a step is missing or in the wrong place, the robot does the wrong thing.

How can score = score + 5 be true? A number can't equal itself plus 5.

In code = means "store". The right side is worked out first using the old value, then the answer goes into the box.

Does an if run both branches?

No. The condition is True or False, and only one branch runs. Watch the robot: it moves until the condition becomes True, then only stop runs.

What is the counter i for in a loop?

It remembers how many times the loop has run, so the loop knows when to stop. It shows as i = 1, 2, 3, 4 on the robot.

Why use a function if I can just copy the code?

One definition can be used many times. If you find a bug, you fix it once and every call is fixed.

The program ran with no error message, so why is it wrong?

That is a logic error: the code is valid but the plan is wrong, like a loop that runs 4 times instead of 5.

What is a program?

An algorithm is a step-by-step plan to solve a problem. A program is that plan written in a language a computer understands, such as Python, Scratch, Java or C. The computer does exactly what the program says, nothing more.

Most programs follow input → process → output: take data in, work on it, give a result. Before coding, plan with pseudocode (plain-English steps) or a flowchart.

Sequence means instructions run in order, from top to bottom. Changing the order can change the result.

Variables and data types

A variable is a named storage place for a value. score = 0 is an assignment: it puts 0 in the box called score. score = score + 5 reads the old value, adds 5, and stores the new value.

Common data types:

A constant is a value that does not change while the program runs, like the number of days in a week. Good names (total_marks, not x) make code easy to read.

Selection and iteration

Selection (if / else)

A program checks a condition that is either True or False, and runs one branch.

if marks >= 33:
    print("Pass")
else:
    print("Try again")

Conditions use comparison operators (==, !=, <, >, <=, >=) and logical operators (and, or, not).

Iteration (loops)

Functions and modular design

A function (also called a subprogram, procedure or method) is a named block of code. You define it once and call it whenever you need it.

def area(length, width):
    return length * width

print(area(4, 3))   # 12

Modular design means breaking a big problem into small parts (decomposition), each written and tested as its own function. Benefits: less repeated code, easier testing, teamwork, and one fix updates every use.

Testing, debugging and code maintenance

Three kinds of errors:

Testing uses normal data, boundary data (on the edge, like exactly 33 marks) and invalid data (like -5 marks). A trace table records each variable's value line by line, to find where things go wrong.

Maintainable code has meaningful names, comments explaining why, consistent indentation, small functions, and no copy-pasted blocks. Programmers also use version control to keep a history of changes.

Try it: unplugged robot

Ask a friend to be the robot. Write instructions on paper using only: step, turn left, turn right, repeat n and if wall: turn. Guide them from the door to a chair. Did the robot do exactly what you wrote? Fix any bugs and try again. Then open the 3D free play and predict where the robot stops before you press anything.

Key formulas and definitions

Worked examples

1. x = 4, then x = x * 2, then x = x - 3. What is x?

Line by line: x = 4 → x = 8 → x = 5. So x = 5.

2. What does this print? for i in range(1, 4): print(i * 10)

i takes 1, 2, 3, so it prints 10, 20, 30.

3. age = 15. What does this print? if age >= 18: print("Adult") else: print("Minor")

15 >= 18 is False, so the else branch runs: Minor.

4. Write a function that returns the bigger of two numbers.

def bigger(a, b): if a > b: return a else: return b

5. Trace: total = 0; for n in range(1, 4): total = total + n. What is total at the end?

n = 1 → total 1; n = 2 → total 3; n = 3 → total 6. Final total = 6.

6. A program should print numbers 1 to 5 but uses for i in range(1, 5). What error is this and how do you fix it?

It is a logic (off-by-one) error; it prints only 1 to 4. Fix: range(1, 6).

Common mistakes

Practice quiz

1. Running instructions one after another in order is called:
2. Which structure makes a choice?
3. After x = 3 and x = x + 4, x is:
4. A program runs but gives the wrong answer. This is a:
5. A named, reusable block of code is a:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What are the basics of programming?

Variables to store data, and three structures to control the steps: sequence, selection (if/else) and iteration (loops). Functions then help you organise and reuse code.

Which language should I learn first?

The ideas are the same in every language. Scratch is great for young learners; Python is popular because its code is short and readable.

What is debugging?

Finding and fixing mistakes (bugs) in a program, often by testing with different inputs and tracing variable values line by line.

Where this is taught

Canada (Ontario)Grade 8C. Algebra
Canada (Ontario)Grade 8A. STEM Skills and Connections
Canada (Ontario)Grade 9C. Algebra
Canada (Ontario)Grade 10C. Programming
Canada (Ontario)Grade 11C. Exploration of Opportunities
Canada (Ontario)Grade 11A. Programming Concepts and Skills
Canada (Ontario)Grade 11B. Software Development
Canada (Ontario)Grade 11A. Programming Concepts and Skills
Canada (Ontario)Grade 11B. Software Development
Canada (Ontario)Grade 11B. Computer Technology Skills
Canada (Ontario)Grade 12D. Interacting with School-Age Children and Adolescents
Canada (Ontario)Grade 12A. Programming Concepts and Skills
Canada (Ontario)Grade 12B. Software Development
Canada (Ontario)Grade 12A. Programming Concepts and Skills
Canada (Ontario)Grade 12B. Computer Technology Skills
NetherlandsVWO 2 (onderbouw)Using digital technology
NetherlandsHAVO 4 (bovenbouw, 2e fase)Programming
NetherlandsVWO 5Programming
PolandSzkoła podstawowa, klasa VIIProgramming and solving problems with computers
PolandSzkoła podstawowa, klasa VIIIProgramming and solving problems with computers
PolandLiceum ogólnokształcące, klasa IIProgramming and solving problems with computers
RomaniaClasa a VIII-aAlgorithms
RomaniaClasa a X-aProgramming language basics
Spain2º ESOComputational thinking, programming and robotics
Spain3º ESOComputational thinking, programming and robotics
Spain1º BachilleratoComputer systems and programming
England (GCSE, A level)Year 9Computer science
England (GCSE, A level)Year 103.2 Programming
England (GCSE, A level)Year 134.1 Fundamentals of programming (A-level)
USA (Common Core, NGSS, AP)Grade 8Algorithms and Programming
USA (Common Core, NGSS, AP)Grade 9Algorithms and Programming
USA (Common Core, NGSS, AP)Grade 10Big Idea 3: Algorithms and Programming
USA (Common Core, NGSS, AP)Grade 11Using Objects and Methods
USA (Common Core, NGSS, AP)Grade 11Data Collections
USA (Common Core, NGSS, AP)Grade 11Algorithms and Programming
Japan中学3年Technology: information technology
Japan高校(専門学科)1〜3年Programming for Information Systems
South Korea중학교 2학년Algorithms and programming
South Korea중학교 3학년Programming
South Korea고등학교 2학년Algorithms and programming
South Korea고등학교 2학년Symbols and logic
South Korea고등학교 2학년Software for creation
South Korea고등학교 3학년Programming
Russia8 классAlgorithms and programming
Russia8 классAlgorithms and programming
Russia9 классAlgorithms and programming

Learn first

Learn next

Related lessons

All Computer Science lessons