c > constructs > iterative constructs > iterative constructs part 2

Iterative Constructs Part 2

Here is some more information on how we use iterative constructs.

Continue and break

We can use the continue and break statements in order to control how a loop operates.

Continue is used to tell the computer to immediately jump to the next iteration of the loop. If there is any code after this statement used then that code is ignored. In the following code, if the number is even then the loop will be advanced to the next iteration instead of running the next line printf("%d is odd\n",i):


#include <stdio.h>

int main(){
    for (int i=0;i<=10;i++){
        printf("%d\n",i);
        if (i%2==0){
            continue;
        }
        // not executed if the i is even
        printf("%d is odd\n",i);
    }
    printf("The loop has finished\n");
    return 0;
}

↗ Open 📋 Copy c/constructs/iterative_constructs/continue_in_loop.c

Break is used to tell the computer to exit the loop completely. In the following code, the loop is exited when i becomes 7:


#include <stdio.h>

int main(){
    int i=0;
    while (i<=10){
        printf("i is %d\n",i);
        // exit loop if i is 7
        if (i==7){
            break;
        }
        i++;
    }
    printf("The loop has finished\n");
    return 0;
}

↗ Open 📋 Copy c/constructs/iterative_constructs/break_out_of_loop.c

Special types of loops

The following are special classifications for loops.

Infinite loops

These are loops that have the potential to run forever.


#include <stdio.h>

int main(){
    while(1){
        printf("I wonder when this is gonna be done...\n");
    }
    return 0;
}

↗ Open 📋 Copy c/constructs/iterative_constructs/infinite_loop.c

Empty loops

These are loops that do no effective work in the program. They may happen by accident if we do not take care in how we write our code (usually by putting curly braces in the wrong place).


#include <stdio.h>

int main(){
    printf("Running an empty loop...\n");
    for (int i=0;i<=1000;i++){}
    printf("The loop is now finished executing...");
    return 0;
}

↗ Open 📋 Copy c/constructs/iterative_constructs/empty_loop.c

The goto statement

Looping behaviour can be achieved by the use of C's goto statement. We place a label before a section of code and tell the computer to go to that section where the label is.

The Euclidean algorithm

The goto statement can be used to write a more readable form of the Euclidean algorithm:


#include <stdio.h>

int main(){
    int original_a=14,original_b=1234412323;
    int a=original_a,b=original_b,quot,rem;
    
    // declaring label called 'start'
    start:
    quot = b/a;
    rem = b%a;
    printf("%d = %d x %d + %d\n",b,quot,a,rem);
    
    if (rem==0){
        printf("The HCF of %d and %d is %d\n",original_a,original_b,a);
    }
    else{
        b = a;
        a = rem;
        // jumping back to the 'start' label
        goto start;
    }
    return 0;
}

↗ Open 📋 Copy c/constructs/iterative_constructs/goto_euclidean_algorithm.c

Notice that we do not declare any variables after the label. The algorithm is based on the fact that for an equation: $$b=qa+r$$ the highest common factor (HCF) of b and a is the HCF of a and r $$HCF(b,a)=HCF(a,r)$$ The algorithm is terminated when we get the HCF of a and a remainder of 0: $$HCF(a,0)=\color{royalblue}a$$ This a is the HCF of the two numbers we started with.