Loops and Iteration in Programming: Repeating Processes, For-Loops, and Array Traversal

Loops and Iteration

The standard approach in most programming languages for repeating a process a certain

number of times, such as moving sequentially through an array to perform the same operations

on each item, involves a loop. In pseudocode, this would typically take the general form

For i = 1,...,N,

    do something

and in programming languages like C and Java this would be written as the for-loop

for( i = 0 ; i < N ; i++ ) {

// do something

    }

in which a counter i keep tracks of doing “the something” N times. For example, we could

compute the sum of all 20 items in an array a using

for( i = 0, sum = 0 ; i < 20 ; i++ ) {

        sum += a[i];

    }

We say that there is iteration over the index i. The general for-loop structure is

    for( INITIALIZATION ; CONDITION ; UPDATE ) {

            REPEATED PROCESS

        }

in which any of the four parts are optional. One way to write this out explicitly is

    INITIALIZATION

if ( not CONDITION ) go to LOOP FINISHED

    LOOP START

    REPEATED PROCESS

    UPDATE

    if ( CONDITION ) go to LOOP START

LOOP FINISHED

In these notes, we will regularly make use of this basic loop structure when operating on data

stored in arrays, but it is important to remember that different programming languages use

different syntax, and there are numerous variations that check the condition to terminate the

repetition at different points.



#Loops #Iteration #ForLoop #Programming #Coding #Pseudocode #CProgramming #Java #ArrayProcessing #LoopStructure #Algorithm #DSA #ControlFlow

Comments

Popular posts from this blog

What is GPT-4? - How Does GPT-4 Work? - Key Features of GPT-4 - Applications of GPT-4 - The Future of GPT-4 and Beyond.

The de-Broglie wavelength associated with a particle of mass m and energy E is h/2mE. The dimensional formula for Planck's constant is :