A loop allows to repeatedly execute a sequence of instructions. From the CPU’s view a loop simply means resetting the program counter (PC) which always points to the currently executed instruction.

       ; Example in some pseudo assembly language
       ; showing an unconditional loop (infinite loop)
       ; ---------------------------------------------
0000h: ; here is the code
0001h: ; within the loop
0002h: jmp 0000h

In the last line the jump instruction resets the program counter, so it will be pointing to the first instruction within the loop. If you grew up in the 80’s and enjoyed owning a home computer you might remember an equivalent unconditional loop in BASIC.

10 PRINT "Good ole times! Where did they go?"
20 GOTO 10

And here is the proof that C isn’t any better than BASIC.

start: printf( "Oh my God!\n" );
       goto start;

In most cases we may want to create conditional loops that repeat code only a certain number of times. In assembly language the unconditional jmp instruction would need to be replaced by a conditional jump, such as jnz (jump if not zero).

Higher programming languages such as BASIC, C, Java, … provide a rich set of keywords to conveniently create different kinds of loops. In Java, for example, there are four different ways of creating a loop, actually there are five depending on what your definition of a loop is. Here are the most basic loops in Java.

// Example 1: while loop (top-controlled)
while( expression ) {
    // Code to be repeated while expression is true

// Example 2: do...while loop (bottom-controlled)
do {
    // Code to be repeated while expression is true
} while( expression );

// Example 3: for loop
for( int i = 0; i < 10; i++ ) {
    // Code to be repeated while i < 10, whereas i starts at 0
    // and is incremented after each repetition

The bottom-controlled do…while loop is being used whenever the code within the loop should be executed at least one time. This can be useful if you want to repeat user input until he or she has entered a valid value, for example. The for loop should only be used if you know the exact number of repetitions in advance. In other words, the exact number of repetitions should become clear to the reader of the code just by looking at the for(…) line. Never write-access your counter variable within the for loop.

The fourth type of loop in Java is called a for…each loop, although the word each does not appear as a keyword. It was introduced to make iterations over data collections (e.g. arrays) more convenient. Here is an example: If a is an array of int-values, n gets the value of each element successively.

// Example 4: for ... each loop
for( int n : a ) {
    System.out.println( n );

Yet another way of repeating code is Recursion. It means that a function (or method in OOP languages) invokes itself.

 * Example 5: Recursion

public static void m1( int n ) {
    System.out.println( n );
    if( n >= 10 )
        m1( n );

public static void main( String[] args ) {
    m1( 0 );

The example above displays the numbers from 1 to 10 (inclusively) on the screen. Recursion is close to the way our brains are thinking (“repeat the same method until a certain condition is reached”). However, it is the least efficient and most dangerous way to create a loop. Here’s why (briefly explained):

  • Each time a method is invoked, your computer has to “remember” (store) the place where to return to after the method has been executed. Local variables and other data related to the method also need to be stored.
  • Accessing memory is very time consuming, so Recursion always is a lot higher in execution time than conventional loops are.
  • The place where this data is stored is called Stack, a limited amount of memory which can overflow. In most cases it is hard (if not impossible) to predict when this overflow will occur, because it depends on many factors (e.g. the stack size of the machine that is running your program, the data types and number of local variables, …)

So you may want to avoid Recursions in your programs.

Well, that’s all for this posting.
See you later,

— Andre M. Maier

PS: Did you know that goto is a reserved keyword in Java, which is neither supported nor working?


About bitjunkie

Teacher, Lecturer, and BITJUNKIE ...
This entry was posted in Programming Essentials, Uncategorized and tagged , , , . Bookmark the permalink.