The secret of loops in programming

Nov 11, 2022 6:50 am

The Secret Of Loops In Programming

image


I'll let you in on a secret about programming: there's actually only 1 kind of loop. That's right, for loops, while loops, and do while loops are all different forms of the same thing.


For example, this for loop:


for (var i = 0; i < 5; i++) { }


is equivalent to this code:


var i = 0;

while (i < 5) {

i++;

}


And this do while loop:


var i = 0;

do {

i++;

} while (i < 5);


is equivalent to this code:


var i = 0;

i++;

while (i < 5) {

i++;

}


How They Work Under The Hood

By the time your code compiles down to machine code, loops don't exist in the same way. As machine code, your program is just a list of instructions. One of these instructions is called jump, and it tells the processor to go to a specific location in memory to read instructions.


The jump instruction is a type of branching instruction. Branching instructions tell the processor to jump to a specific location, if a condition is true.


Control-flow statements, like if, for, or while, are typically implemented as branching instructions.


Likewise, switch statements are basically just if/if else statements. Both compile to conditionals and branching instructions.


What About Functions?

In machine code, functions don't exist in the same way, either. Functions are just a subset of the list of instructions that make up your program. The processor doesn't know they exist. When we call functions, we are actually doing a special kind of jump.


This jump is called a call instruction. It tells the processor to store the current instruction address before jumping, so that the function can jump back to, or "return" to this address when it's done.


What's The Point Of Knowing This?

Programming is often more about making it easier to write correct programs, than just telling the computer what to do. Modern languages have features that abstract these branching instructions away.


Languages with lots of features like this are called high-level languages, because they let you focus on the problem from a higher level of logic, instead of having to focus on branching and jump instructions.


In general, most programming you do will be in high-level languages. But if you plan to write software for embedded systems, where resources are constrained, you'll need to write code in a low-level language.


One language you might be familiar with is C. C is technically a high-level language, compared to the instructions of most processors. However, when compared to a language like JavaScript, which has automatic memory management and higher-order functions, C is low-level.


Conclusion

There's more to programming than meets the eye. You can become a better programmer when you understand how your code relates to what actions the computer actually performs.


I hope you enjoyed this article. Don't forget to join the Coding for Zoomers Discord server:


Join Discord


See you next Monday,

Tobe Osakwe

Comments