Skip to content
Lesson 4

Algorithms The Step By Step

An algorithm is an ordered list of steps that, if followed, takes you from a starting situation to a finished one. The first lesson in this track established the idea on a recipe. Now we make it sharper. A real algorith…

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.

An algorithm is an ordered list of steps that, if followed, takes you from a starting situation to a finished one. The first lesson in this track established the idea on a recipe. Now we make it sharper. A real algorithm in the wild has three things, not just one. It has steps. It has order. And, often, it has branches. A branch is a step that says, if this, then this, otherwise that. Without branches you only have a march. With branches you have a thinking machine.

Worked example one. Boil chai for two cups. Plain steps version. Take the patila. Add one and a half cups water. Add half cup milk. Add three teaspoons sugar. Add two teaspoons chai patti. Place on stove on medium flame. Wait until the chai rises, then lower flame. Wait one more minute. Strain into two cups. Done. This is a march. No branches. No decisions. It works only when the world cooperates.

Worked example one, with branches. Real chai, real kitchen. Take the patila. If there is no milk, the algorithm should not crash silently, it should send you to the kiryana. So step one becomes, check if there is milk. If yes, continue. If no, walk to the corner kiryana, buy a packet of Olpers, come back. Now add water and milk. If someone in the family has shugar, ask before adding sugar. If yes for sugar, three teaspoons. If no, zero teaspoons. Place on stove. If the chai patti is the strong kind from a tea shop, use one and a half teaspoons. If it is the weak supermarket kind, use three. Wait until the chai rises. If it rises faster than usual, the flame is too high. Lower it. If it does not rise after four minutes, the flame is too low. Raise it. Strain. Serve. The chai is the same chai. The algorithm now survives a kitchen where things go wrong.

Worked example two. Take a Lahore Metrobus from your gali to school. Step one, leave home at seven five. If it is raining, leave at six fifty five and carry the umbrella. Step two, walk to the metro station. If the lift is broken, take stairs. Step three, tap the metro card at the gate. If the card is empty, top it up at the booth, then tap. Step four, board the bus going north. If the bus is full, wait for the next, do not push. Step five, get off at the third stop. Step six, walk three minutes to school. Step seven, arrive by seven forty. Look at the algorithm. It has six branches. Each branch is a real failure your day might face. Without those branches, the algorithm only works on a perfect day, and a perfect day is rare.

Worked example three. Send Easypaisa to your khala in Multan. Step one, pick up phone. Step two, dial star seven eight six hash. Step three, choose option for send money. Step four, enter your khala's mobile number. Step five, enter the amount, say five thousand rupees. Step six, enter your four digit MPIN. Step seven, the system asks for confirmation. If everything is correct, press one to confirm. If anything is wrong, press two to cancel. Step eight, wait for the SMS confirmation. If the SMS arrives within thirty seconds with the right amount, the algorithm is done. If no SMS arrives in two minutes, the algorithm has not finished. You must call the helpline. Notice that the last branch is what protects the family money. A program that says, send and assume it worked, is the program that loses money. A program that says, send and verify, is the program a Pakistani family can trust.

Why this matters. Pakistan is moving fast into a world where small algorithms run quietly behind everything. The bus you take. The money you send. The school admission you fill. Each of those is an algorithm written by someone. If the algorithm has missing branches, real families lose real money and real chances. The country needs young people who write algorithms with their eyes open, who name the things that go wrong because they have lived with the things that go wrong. You already have that knowledge. Writing it down as steps is just the next move.

Estimated time: 14 min