Basics of programming the for loop.

Octavio Gonzalez-Lugo
2 min readMar 2, 2022

--

Photo by Roman Synkevych on Unsplash

Solving repetitive tasks is one of the most useful tasks that can be solved with computers. From checking if a file contains certain information to making repetitive cuts. Those are some examples that currently are solved with computers.

The core used to solve those repetitive tasks are the different programming languages that enable us to describe the way and the times a task is going to be repeated. And inside the different programming languages. different functions help us to perform the same task many times.

One of such functions is the for function, which repeats the same task a fixed number of times. The syntax of this specific function varies among programming languages. For example, in python, there are two ways to use the for loop. A one-liner, where all the code is in one line, or many lines.

In contrast, R and JavaScript use curly brackets to define the start and end of the for loop. And either a condition or an array or vector can be used to define the loop.

While C++ uses also brackets the for function takes a value as an iterator, a condition for the iterator to be met, and how to update the iterator.

All those are some examples of how to define a for loop in different programming languages. Yet, such functionality is not always present in every language. Haskell, functional programming does not contain any kind of loop. But you can use recursion to define a function that performs the same function as a for loop. Despite the differences in syntaxis, each programming language offers an option on how to perform the same task many times. And the task in hand just needs to be defined once inside the loop. Programming does not need to be complicated, just start with the syntax that you feel the most familiar with. See you in the next one.

--

--

Octavio Gonzalez-Lugo

Writing about math, natural sciences, academia and any other thing that I can think about.