Repetition in imperative languages

Metadata
aliases: []
shorthands: {}
created: 2022-07-24 21:59:14
modified: 2022-07-24 22:09:06

In imperative languages, new values may be associated with the same name through command repetition.

Example

In order to find the sum of the N elements of array A, we don't write

SUM1 := A[1] ;
SUM2 := SUM1 + A[2] ;
SUM3 := SUM2 + A[3] ;
    ....

Instead of creating a new variable for each element, we can write a loop, by reusing two names, one for the sum and one for counting iterations:

I := 0 ;
SUM := 0 ;
WHILE I < N DO
BEGIN
    I := I + 1 ;
    SUM := SUM + A[I] ;
END

See also