📘 CodingMarble Learn

Branching and Loops: Making Programs Decide and Repeat

Branching (if/else) lets a program choose a path using a condition; a loop repeats steps while a condition is true. With these two ideas we can find the biggest number, solve a quadratic, add the digits of a number, test for primes and compute the GCD with Euclid's algorithm.

🎬 Step-by-step story

  1. Sequence: steps run one after another. Press Run and watch the ball pass the blocks.
  2. Branch: the path depends on a test. Change n: even goes up, odd goes down.
  3. Loop: take the last digit, add it to the sum, drop the digit. Repeat until nothing is left.
  4. Prime test: look for a divisor. If one divides n exactly, n is not prime.
  5. Euclid: cut the shorter bar from the longer one again and again. When they are equal, that is the GCD.
  6. Free play: choose any method and change the numbers.

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

🤔 Common doubts, cleared

What if the condition is neither true nor false?

A condition always has one answer, true or false. So exactly one lane is taken.

When does a loop stop?

When its condition becomes false. In the digit loop that is when the number reaches 0.

Why is 1 not a prime?

A prime needs exactly two divisors. 1 has only one divisor, itself.

Why does Euclid's subtraction work?

Any number that divides both a and b also divides their difference, so the GCD stays the same while the numbers get smaller.

Do blocks always run in order?

Only in a sequence. Branches skip some blocks and loops repeat some.

Can I mix a branch inside a loop?

Yes. For example, loop over numbers and use an if to count only the even ones.

Three building blocks: sequence, branch, loop

Every algorithm is built from three blocks:

A condition is a question with the answer true or false, for example n % 2 == 0. The % sign gives the remainder after division (7 % 2 is 1).

Compound conditions, maximum and minimum

Join conditions with and (both must be true), or (at least one true) and not (flip the answer). To check that x is between 10 and 20 write x >= 10 and x <= 20.

Maximum of two numbers: if a > b: max = a else: max = b. Of three numbers: start with max = a; then if b > max: max = b; then if c > max: max = c. For the minimum, flip the sign. Check: for 7, 12, 9 the max is 12 and the min is 7.

Branching in maths: the quadratic equation

For a x² + b x + c = 0 compute the discriminant D = b² - 4ac. Then branch: if D > 0 there are two real roots x = (-b ± √D) / (2a); if D = 0 there is one root -b / (2a); if D < 0 there is no real root. Example: x² - 5x + 6 = 0 has D = 25 - 24 = 1, so roots (5 ± 1)/2 = 3 and 2.

Loops on numbers: digits of a number

Two tools: n % 10 is the last digit and n // 10 (whole-number division) drops the last digit. To add the digits of n:

sum = 0; while n > 0: sum = sum + n % 10; n = n // 10

Trace for 347: last digit 7 (sum 7, n 34), then 4 (sum 11, n 3), then 3 (sum 14, n 0). Stop. The sum is 14. The same loop can count digits, reverse a number or check a palindrome.

Prime test

A number n > 1 is prime if only 1 and n divide it. Loop d from 2 up while d × d <= n; if n % d == 0, n is not prime. If the loop ends with no divisor, n is prime. Why only up to √n? If n = p × q, one of p, q is not bigger than √n. For 29, check 2, 3, 4, 5 (5 × 5 = 25 <= 29): none divides, so 29 is prime. For 21 we meet 3 and stop.

Euclid's algorithm for the GCD

The GCD (greatest common divisor) is the biggest number that divides both. Euclid's idea: the GCD does not change if you replace the bigger number by (bigger - smaller). Keep going until the numbers are equal. Example (18, 12): (6, 12) then (6, 6). GCD = 6. A faster form uses the remainder: while b != 0: a, b = b, a % b. For (48, 18): (18, 12), (12, 6), (6, 0), so the GCD is 6.

Try it

Try it: On paper, trace the digit loop for 4829 in a table with columns n, last digit, sum. Then use the 3D digit loop and press Next step to check each row. Predict first: will the sum be bigger than 20?

Key formulas and definitions

Worked examples

1. Write the condition that is true only when a number x is between 10 and 20 (both included).

x >= 10 and x <= 20. Both parts must be true, so "and" is needed.

2. Find the maximum of 7, 12 and 9 with the start-and-compare method.

max = 7. 12 > 7 so max = 12. 9 > 12 is false so max stays 12. Answer: 12.

3. Find the sum of the digits of 347 with a loop.

sum 0. 347: last digit 7, sum 7, n 34. Then 4: sum 11, n 3. Then 3: sum 14, n 0. Stop. Sum = 14.

4. Is 29 prime?

Check d = 2, 3, 4, 5 (5×5 = 25 <= 29). 29 % 2 = 1, % 3 = 2, % 4 = 1, % 5 = 4. None is 0, so 29 is prime.

5. Find the GCD of 48 and 18 using remainders.

(48, 18) → (18, 12) → (12, 6) → (6, 0). GCD = 6.

6. Solve x² - 5x + 6 = 0 using the branching rule.

D = 25 - 24 = 1 > 0, so two roots: (5 + 1)/2 = 3 and (5 - 1)/2 = 2.

Common mistakes

Practice quiz

1. Which block repeats steps?
2. What is 347 % 10?
3. If D < 0 in a quadratic, there are:
4. What is the GCD of 18 and 12?
5. Which is prime?

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 is the difference between branching and a loop?

A branch picks one path once, using a condition. A loop runs the same steps many times while a condition stays true.

Why do we check divisors only up to the square root?

If n has a divisor bigger than its square root, it also has a matching one smaller than it, so we would already have found it.

How does Euclid's algorithm find the GCD?

It keeps replacing the bigger number by the difference (or the remainder) until the two are equal (or the remainder is 0). That value is the GCD.

Where this is taught

Russia8 классAlgorithms and programming
Russia8 классAlgorithms and programming

Learn first

Learn next

Related lessons

All Computer Science lessons