Loops can be described as key elements in programming,
enabling the repetition of certain blocks of code while a condition is met.
Most modern programmers couldn't imagine coding without loops. However, if you
were to go back to 1972, in the earliest versions of the BASIC language, loops
as we know them today did not exist in a structured form, and programmers
relied on commands like GOTO or jump to control the program flow, including
simulating loops. This was characteristic of the first iterations of BASIC,
which was designed in 1964 by John Kemeny and Thomas Kurtz at Dartmouth
College, with the goal of being simple for beginners.
However, was the GOTO command alone sufficient?
Theoretically, the GOTO command is sufficient to simulate any loop because it
is a Turing-complete mechanism – any control structure can be expressed with
jumps and conditions. However, in practice, it was inefficient and prone to
errors. That's why loops were introduced as a response to criticism, including
Edsger Dijkstra's famous 1968 article, 'Go To Statement Considered Harmful,'
which pointed out the problems of unstructured code.
In the C programming language, the loops: for, while, and
do-while, existed from the very beginning, that is, from its creation in 1972,
when Dennis Ritchie developed it at Bell Labs. The C programming language was
designed as a successor to the B programming language, which itself was a
simplified version of BCPL, and one of the goals was to provide a higher level
of program flow control compared to its predecessors, while maintaining
closeness to machine code.
Loops have always existed in the C programming language
Loops were an integral part of that design, so there was
no moment in the history of the standard C programming language where they were
absent. Unlike BASIC, which initially relied only on GOTO, the C programming
language was designed with loops as a standard tool, inspired by higher-level
languages like ALGOL, but adapted for low-level efficiency.
for: For iterations with a known number of repetitions.
while: For conditional loops where the condition is
checked before execution.
do-while: For conditional loops where the condition is
checked after execution.
In early versions, variable declaration within the for
loop header was not allowed; variables had to be declared outside the loop.
This changed with the C99 standard, which introduced block scope. With the ANSI
C standard in 1989, known as C89/C90, loops were formally defined and have not
undergone significant changes in later standards C99, C11, C17, and C23. All
three loops have remained consistent in syntax and semantics, showing that
Ritchie's design was robust from the very start.
Why Doesn't the C Programming Language Have a 'foreach'
Loop, While C++ Has an Alternative?