Skip to content
Lesson 8

Decisions And Branching

Until now, every algorithm we have looked at runs in a straight line, top to bottom, with maybe a loop folded in. But life rarely lets you walk in one straight line. At every corner, the world asks a question and waits …

There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.

There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.

Until now, every algorithm we have looked at runs in a straight line, top to bottom, with maybe a loop folded in. But life rarely lets you walk in one straight line. At every corner, the world asks a question and waits for your answer before showing you the next street. If it rains, take the umbrella. If the bijli is gone, light the candle. If the gas pressure is low, switch the chulha to the smaller burner. Programmers call these forks if-else, or branching. Every interesting program has them. Without them, code can only do one thing, which is barely useful.

Start with the simplest fork in your week. Agar baarish ho to chhatri lay lo, warna nahi. Two paths. Same starting point, you about to leave the house. Same ending point, you arriving at school. The middle differs by one item. The if asks the world a question. The world answers haan or naheen. Your action follows the answer. A program does the same. If raining is true, pack the umbrella. If raining is false, do not. The world is the input. Truth and falsehood are the answers the input can take.

Now stand at the chowk on Ferozepur Road and watch the traffic signal. Green, the cars move. Red, the cars stop. Yellow, the careful ones slow down and the rashghat ones speed up. One signal, three colours, three branches. In code this is written as if green, then go, else if yellow, then slow, else stop. The order of the branches matters. The signal does not show two colours at once, so the program checks them one by one and takes the first one that matches. This is exactly how a Punjab traffic warden, peering at his own watch, would think it through.

A favourite Pakistani example. The kiryana shop on the corner. You take a packet of biscuits to the counter. The bill is one hundred and forty rupees. You hand the chacha a five hundred rupee note. He looks in the cash drawer. Two paths open up. If he has the right change, he counts out three hundred and sixty rupees and the deal is done. If he does not, he says, beta khulay paisay nahi hain, dekho phir aana ya kuch aur lay lo. The whole exchange is one big if-else, with the input being the state of the cash drawer and the output being one of three things, change, a delayed transaction, or another item bought. The same idea repeats a thousand times across Pakistan every day, in every gali. You have already lived in a world made of branches your whole life.

Branches can be nested. That is, inside one branch can sit another. If it is raining, then if I have my umbrella, I will walk to school, else I will wait for the van. Two questions, four possible paths. As nesting deepens, the code starts to look like the inside of a karak chai recipe with too many sub-steps. Smart programmers learn to flatten this. Instead of asking three questions deep, they re-frame the question so it can be answered in one or two layers. The discipline is the same as a teacher who can explain a complicated rule to a Class 5 student in one clean sentence rather than a tangled paragraph.

One closing thought. The branches inside code reflect the branches inside the writer's head. A coder who has thought about only two cases will write a program that handles two cases and breaks on the third. A coder who walks through her city, watching what happens at the petrol pump, the school gate, the ATM, the doctor's clinic, will write code that handles the messy real world her code will eventually meet. The lesson is, do not start with the keyboard. Start with the chowk, the dukan, the parchi, the queue. The if-else writes itself when you have actually watched the decisions get made.

Estimated time: 12 min