Skipping

Of the two C language keyword looping statements, for is the most traditional and probably the most popular. It’s also the most frustrating for beginners because of its many parts. But eventually, a comfort level arises with using the for loop, which is sad because it’s more powerful than a simple counter.

A while back, I wrote a post about how to setup a basic for loop, one that repeats a given number of times. That’s really all you need to know about a loop, and in many simple programming languages, a basic loop-like statement simply repeats a given chunk of code.

The for loop is more powerful than a simple repeat-this-chunk-of-code type of loop. In fact, the C language for keyword is on loan to just about every other programming language because it’s so flexible.

Part of the flexibility is that you can set specific starting and ending values for the loop, and also determine a skip value. So if you need to work through only even numbers, you craft a for statement like this:

for(x=2;x<=100;x+=2)

The loop starts at value 2, then increments by 2 all the way up to 100. The x+=2 calculation is short for x=x+2, which keeps adding 2 to the value of variable x, all the way from 2 to 100.

Another powerful aspect of the for loop is that its conditions can vary. To demonstrate how that can work, the following code contains a nested for loop. The inner loop counts from 1 to 100 in increments from 1 to 10. The outer loop sets those increments.

#include <stdio.h>

int main()
{
    int skip,x;

    for(skip=1;skip<=10;skip++)
    {
        printf("Skip %d: ",skip);
        for(x=skip;x<=100;x+=skip)
        {
            printf(" %3d",x);
        }
        putchar('\n');
    }

    return(0);
}

Here's the output:

Skip 1:    1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100
Skip 2:    2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98 100
Skip 3:    3   6   9  12  15  18  21  24  27  30  33  36  39  42  45  48  51  54  57  60  63  66  69  72  75  78  81  84  87  90  93  96  99
Skip 4:    4   8  12  16  20  24  28  32  36  40  44  48  52  56  60  64  68  72  76  80  84  88  92  96 100
Skip 5:    5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95 100
Skip 6:    6  12  18  24  30  36  42  48  54  60  66  72  78  84  90  96
Skip 7:    7  14  21  28  35  42  49  56  63  70  77  84  91  98
Skip 8:    8  16  24  32  40  48  56  64  72  80  88  96
Skip 9:    9  18  27  36  45  54  63  72  81  90  99
Skip 10:   10  20  30  40  50  60  70  80  90 100

The outer loop value skip is used twice in the inner loop at Line 10. First, it sets the starting number for the loop. Second, it sets the increment. (If the starting value for the loop were set to 1, then the output wouldn't look as pretty.)

The sample code's output isn't really anything special. It does, however, demonstrate my point, which is that a for loop need not default to the traditional and expected x++ as the loop counter.

Leave a Reply