A programmer compiles a set of instructions into a program. So at its essence, a program is simply a bunch of instructions that perform a given task. Those instructions use branching and repetition to form a kind of narrative.
For instance, imagine you need to go shopping for groceries. First, add items to the shopping-list. While scanning kitchen, if milk is less than half full, add to shopping-list; if container of popcorn is empty, add to shopping-list. Now goto the store. While not at store, drive – if impeded, press brake, else press accelerator. Now enter the store. For each item on shopping-list, add to cart.
function scanKitchen(): if(milk < milk.contents / 2) list.add(milk) if(popcorn == empty) list.add(popcorn) return list function drive(): while(!atStore) if(isBlocked) pressBrake() else pressAccelerator() function shop(list): for each item in list cart.add(item) //call these three functions in the appropriate order: list = scanKitchen() drive() shop(list)
if statements take the program on different paths depending on whether a condition is true or false. Whereas while / for statements create loops that keep certain commands repeating.
Putting objects in groups, such as the shopping-list, is another fundamental concept. An array of items is easier to keep track of as a whole, and such a list can be quickly scanned and processed.
To tell the story, you’re going to need some action commands. For instance scanKitchen is a function you call upon to look through the pantry and fridge. You simply invoke it, and it returns a list of needed items. You can then print those items onto paper — or order them online — the list is yours to do as you wish.
Different programming platforms provide various built-in commands to play with. Some programming environments offer minimal pre-made functions and some offer extensive libraries of functionality to call upon. The work that goes into a program comes from assembling the provided building-blocks. This includes finding the appropriate commands and properly invoking them, as well as placing those instructions in the right order — and of course you can adjust this order by using conditions and repetition.