Hello peeps 👋! Welcome to this week's article where we talk about control structures. Before we get all nitty-gritty, who's your favourite teacher? No need to mention it in the comments 😜.
Now let's get greasy. Let's talk about the things revolving around control structures in programming and some extra nonsense. Generally in life (most of the times), there is always something driving how we carry out our actions and decisions, it could be a sudden urge, or good-will, or mischief or a thousand other reasons. Bottom line, our actions have a sort of urge powering it.
Thankfully, urges aren't something we have in programming neither are there a thousand reasons as to how code decides to execute 😌. But another question, why would we need to execute code multiple ways? Or have different structures for execution? Because sometimes, we want things to happen in a different manner to the default.
In programming, there are three fundamental way code executes (no matter how complex or twisted the code is):
- Selection Any other method of execution is still built on these three since they were invented over 40 years ago.
The default of every code ever ran (except if that code happens to be a human), sequential means in sequence, i.e one after the other. This is the default control of executing code, it is fashioned to be one line after the other, line 1 then line 2 then line 3 then...
This one is for repetition. For example, get me the names of students in a class till no student exists. We are doing the same action (getting students' names) over and over. Languages have multiple ways of expressing this (for loops, while loops, etc.)
We love having choices, it's a default. And we also love adding some sprinkles of choices here and there, for example, should you buy burgers or carrots (obviously burgers). Code execution sometimes come across situations where they need to make a decision based on an event or query. E.g Is the user authenticated or not, if yes, log him in, if not, throw him out of the window 😝. As we create applications and impact lives in some manner with our code, we acknowledge that not every situations can be the same and if so, we must handle them accordingly. Hence, the selection. They usually have a syntax of if something, do this, we have also have multiple selection syntax if something, do this, else, do that. And much more (depending on the language).
Now that we have established what structures are and why we need them, how do they sync with each other? Good question, due to a phenomenom known as "Transfer of Control", you have these structures switching during excution. So the code starts of sequential (line by line), then it comes across an iteration statement, the sequential execution is paused and iteration takes over and your code is now in a loop. After iterating is done, control is transferred back to sequential execution. I think that's all to talk about, if I missed anything, don't hesitate to point it out ❤️.
The first of many articles done! Thanks for giving this a read, you can suggest topics you would love to see or add to the post. All appreciated. Keep learning, keep striving and keep coding. You are awesome! Cya next time 👋