Skip to content
Educora
IntermediateGrades 6–722 min24 / 59

Branching algorithms

Full and incomplete branching, nested and compound conditions (and, or, not), their flowcharts and pseudocode; tracing DİM flowcharts with several diamonds and finding the initial value from the result.

Check yourself
In this lesson you will learn
  • Tell full and incomplete branching apart and write them as flowcharts and pseudocode
  • Evaluate nested conditions and compound conditions built with “and”, “or”, “not”
  • Trace a DİM flowchart with several diamonds for given values
  • Find the initial values from the result and check the branch conditions

A metro turnstile asks the same question every time: is there enough money on the card? If there is, the gate opens and the fare is deducted; if not, the screen says “Insufficient balance”. The turnstile’s algorithm is not linear: one of two different paths is chosen depending on the answer to a condition. Such algorithms are called branching algorithms. DİM algorithm tasks often give flowcharts with several diamonds, and in every exam of 2025–2026 two or three Python tasks were directly on the topic “Conditional statement” — the key to both is in this lesson.

Full and incomplete branching

Definition
Branching algorithm

An algorithm in which one of two different sequences of commands is carried out depending on whether a condition is true or false. In a flowchart the branching is made by a diamond: its “Yes” and “No” exits are the two branches, which later join again.

  • Full branching — both branches contain actions: “if the condition is true, do A, otherwise do B”.
  • Incomplete branching — only one branch contains actions: “if the condition is true, do A”; when it is false the algorithm simply goes on.
Full branchingIncomplete branchinga > bYesNom = am = bx < 0YesNox = −x
Left: the larger of two numbers; right: the absolute value of a number
Text
if a > b then
    m = a
else
    m = b
end if

if x < 0 then
    x = −x
end if
The same two branchings in pseudocode. In Python: if a > b: … else: … and if x < 0: …
Example 1. Tracing full and incomplete branching

Using the flowcharts above, find:
1) m for the pairs a, b: (7, 3), (−2, 5), (4, 4).
2) the final value of x for x = −6 and x = 9.
3) a full branching that prints “even” or “odd” with the condition “n % 2 = 0” — for n = 14, 7, 0, −3.

Show solution
1) 7 > 3 — Yes → m = 7. −2 > 5 — No → m = 5. 4 > 4 — No (4 is not greater than 4) → m = b = 4; the answer is still right for equal numbers.
2) −6 < 0 — Yes → x = −(−6) = 6. 9 < 0 — No → nothing changes, x = 9. This algorithm finds the absolute value.
3) 14 % 2 = 0 → even; 7 % 2 = 1 → odd; 0 % 2 = 0 → even (0 is an even number!); −3 % 2 = 1 (in Python the remainder of division by 2 is always 0 or 1) → odd.

Nested conditions

A branch may contain another diamond — this is nested branching. With it you can separate not two but three, four or more cases. For example, to find the largest of three numbers you first compare a and b, and then compare the winner with c. The same job can be done more simply with two incomplete branchings in a row: m = a; if b > m then m = b; if c > m then m = c. This “candidate” method is very useful in the written tasks too.

Example 2. The largest of three numbers

Trace the “candidate” algorithm (m = a; if b > m then m = b; if c > m then m = c) for three inputs: (3, 9, 5), (8, 2, 8), (−1, −4, −7).

Show solution
(3, 9, 5): m = 3 → 9 > 3 Yes, m = 9 → 5 > 9 No → 9.
(8, 2, 8): m = 8 → 2 > 8 No → 8 > 8 No → 8 (a tie causes no problem).
(−1, −4, −7): m = −1 → −4 > −1 No → −7 > −1 No → −1. It works for negative numbers because the first candidate is a, not “0”.
D = b² − 4·a·c
where:
  • Ddiscriminant: D > 0 — two roots, D = 0 — one root, D < 0 — no real roots
  • a, b, ccoefficients of a·x² + b·x + c = 0 (a ≠ 0)

Two diamonds separate the three cases: first D > 0, then D = 0

Starta, b, cD = b² − 4·a·cD > 0x₁ = (−b + √D)/(2a)x₂ = (−b − √D)/(2a)x₁, x₂YesD = 0Nox = −b/(2a)xYes“No real roots”NoEnd
Nested branching: the roots of a quadratic equation
Example 3. Tracing the quadratic-equation flowchart

Trace the flowchart for three inputs: 1) a = 1, b = −5, c = 6; 2) a = 1, b = 4, c = 4; 3) a = 2, b = 1, c = 3.

Show solution
1) D = 25 − 24 = 1 > 0 → x₁ = (5 + 1)/2 = 3, x₂ = (5 − 1)/2 = 2.
2) D = 16 − 16 = 0 → D > 0 No, D = 0 Yes → x = −4/2 = −2.
3) D = 1 − 24 = −23 → “No” twice → “No real roots”.
Each input took a different path through the flowchart — all three paths have been checked.

In the quadratic-equation flowchart the “No” branch leads to the next diamond — this is a ladder structure: the conditions are checked in turn, the branch of the first true condition runs, and the rest are skipped. In Python it is written with if … elif … else. The order of the conditions matters a lot: if “score ≥ 50” is checked before “score ≥ 90”, a student with 95 points falls into the first branch and never gets “excellent”. Rule: check the narrowest condition first.

Compound conditions: and, or, not

When simple conditions are joined by logical operations, you get a compound condition. “A and B” is true only when both conditions are true; “A or B” is true when at least one is true; “not A” is the opposite of A. In Python they are written and, or, not, and in database queries AND, OR, NOT. Order of operations: first “not”, then “and”, and last “or”; when in doubt, use brackets.

ABA and BA or Bnot A
truetruetruetruefalse
truefalsefalsetruefalse
falsetruefalsetruetrue
falsefalsefalsefalsetrue
Truth table of the logical operations
(year % 4 = 0 and year % 100 ≠ 0) or year % 400 = 0
where:
  • %remainder of division; “year % 4 = 0” means the year is divisible by 4
  • ≠not equal

The leap-year (366-day) rule of the Gregorian calendar — a classic compound condition

Example 4. Leap years

Check the condition for the years 2024, 2026, 1900 and 2000.

Show solution
2024: divisible by 4 (true) and not by 100 (true) → the bracket is true → leap year.
2026: not divisible by 4 → the bracket is false; not divisible by 400 either → common year.
1900: divisible by 4 but also by 100 → the bracket is false; 1900 % 400 = 300 → common year.
2000: the bracket is false (divisible by 100), but 2000 % 400 = 0 → the “or” is true → leap year.

DİM tasks: a flowchart with several diamonds

In such a task the flowchart looks like a tree, but for the given input only one path through it is taken. In each diamond evaluate the condition with the current values, write the answer (“Yes” or “No”) and follow only that arrow — ignore the other branches. Most mistakes happen with strict and non-strict inequalities: 60 > 60 is false, while 60 ≥ 60 is true.

Starta = 24b = 40a + b > 60a·2 = ba > bYesNoa = b / 5a = a − ba = b − a / 4a = a·2NoYesNoYesaEnd
A DİM-style flowchart with several diamonds
Example 5. What is a after the algorithm runs?

1) Using the flowchart, find the value of a that is output.
A) 16 B) 48 C) 34 D) 8 E) 30
2) What would the answer be for the initial values a = 20, b = 40?

Show solution
1) 24 + 40 = 64 > 60 — Yes, right branch.
24·2 = 48 = 40? — No → a = 40 − 24 / 4 = 40 − 6 = 34. Answer C. Note: the diamond a > b is not checked at all on this path.
2) 20 + 40 = 60 > 60? — No (strict inequality!), left branch.
20 > 40? — No → a = 40 / 5 = 8. A student who reads it as “60 ≥ 60” goes right, gets 20·2 = 40 → a = 40 and is wrong.
Python
a = 24
b = 40
if a + b > 60:
    if a * 2 == b:
        a = a * 2
    else:
        a = b - a / 4
else:
    if a > b:
        a = a - b
    else:
        a = b / 5
print(a)
▸ Expected output
34.0
The same flowchart in Python. The = of a diamond is written == in Python. The result of / is always a decimal number, so 34.0 is printed. Exam programs read the values with a = int(input()).

The reverse task: the initial value from the result

DİM sometimes asks about an algorithm in reverse: the result is known and the initial value of a is given as “?”. In a linear algorithm we call the initial value x, express every step through x and get an equation. In a branching algorithm a separate equation is solved for each branch, and then we check that the root really falls into that branch.

  1. 1
    Name it

    Call the unknown initial value x.

  2. 2
    Express

    After each command express the variables through x (a trace table with expressions instead of numbers).

  3. 3
    Equate

    Set the expression of the result equal to the given value and solve the equation.

  4. 4
    Check

    Substitute each root into the algorithm: are the branch conditions and restrictions (division by zero, natural numbers) satisfied?

  5. 5
    Answer the question asked

    The question may ask for the sum, the product or the largest of the roots — read it again.

Example 6. Sum of the initial values of a linear algorithm

Algorithm: a = ?; b = a + 4; a = a · b; a = a − 2 · b. After execution a = 7. Find the sum of the possible initial values of a.
A) 8 B) −2 C) 2 D) −15 E) 3

Show solution
a = x → b = x + 4 → a = x(x + 4) = x² + 4x → a = x² + 4x − 2(x + 4) = x² + 2x − 8.
x² + 2x − 8 = 7 → x² + 2x − 15 = 0 → x = 3 or x = −5.
Check: x = 3: b = 7, a = 21, a = 21 − 14 = 7 ✓; x = −5: b = −1, a = 5, a = 5 + 2 = 7 ✓.
Sum: 3 + (−5) = −2, answer B.
Example 7. The reverse task with branching

Algorithm: a = ?; if a > 10 then b = a − 10, otherwise b = a + 4; output b.
1) The output is 12. Find the sum of the initial values of a.
2) The output is 20. What can a be?

Show solution
1) “Yes” branch: a − 10 = 12 → a = 22; 22 > 10 ✓.
“No” branch: a + 4 = 12 → a = 8; 8 > 10 is false, so a really goes to the “No” branch ✓.
Sum: 22 + 8 = 30.
2) “Yes”: a = 30, 30 > 10 ✓. “No”: a = 16, but 16 > 10 is true — such an a would go to the “Yes” branch and give 6 ✗.
Answer: only a = 30. Without the check, 16 would wrongly be added.

Branching gives an algorithm the power to “choose”. The next step is repeating the same commands, which is the topic of the lesson «Loop algorithms and trace tables». A loop’s condition is checked with the same kind of diamond as in this lesson, so reading diamonds fluently will be needed there too.

Exercise

year = 2100. Write the leap-year rule as a compound condition: print 366 for a leap year, otherwise 365.

Exercise · Python
year = 2100
# print 366 for a leap year, otherwise 365
▸ Expected output
365

Key points

  • In a branching algorithm one of two paths is chosen depending on the answer to a condition; in a flowchart this is done by a diamond.
  • Full branching has actions in both branches, incomplete branching in only one.
  • “A and B” is true when both are true, “A or B” when at least one is true; “not (a > b)” ⇔ a ≤ b.
  • In a DİM flowchart only one path is taken for a given input: check each diamond with the current values and watch strict inequalities.
  • In a reverse task call the initial value x, build an equation, check each root against the branch conditions; find the sum of roots with Vieta’s theorem.

Check yourself

12 questions. Every correct answer earns XP.

1 / 12
What is branching called when no action is performed if the condition is false?