- 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
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.
if a > b then
m = a
else
m = b
end if
if x < 0 then
x = −x
end ifif a > b: … else: … and if x < 0: …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 solutionHide solution
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.
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 solutionHide solution
(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”.
- 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
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 solutionHide solution
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.
| A | B | A and B | A or B | not A |
|---|---|---|---|---|
| true | true | true | true | false |
| true | false | false | true | false |
| false | true | false | true | true |
| false | false | false | false | true |
- %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
Check the condition for the years 2024, 2026, 1900 and 2000.
Show solutionHide solution
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.
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 solutionHide solution
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.
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
= 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.
- 1Name it
Call the unknown initial value x.
- 2Express
After each command express the variables through x (a trace table with expressions instead of numbers).
- 3Equate
Set the expression of the result equal to the given value and solve the equation.
- 4Check
Substitute each root into the algorithm: are the branch conditions and restrictions (division by zero, natural numbers) satisfied?
- 5Answer the question asked
The question may ask for the sum, the product or the largest of the roots — read it again.
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 solutionHide solution
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.
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 solutionHide solution
“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.
year = 2100. Write the leap-year rule as a compound condition: print 366 for a leap year, otherwise 365.
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.