Quick actions

cmd+k|ctrl+k

Navigation

Languages

c_loops

Snippet info

Language

C

Visibility

public

Author

tony

Created

2018-08-17T01:03:01Z

Updated

2018-08-17T02:20:08Z

#include <stdio.h>

int main(void) {
    printf("For Loop\n");
    
    int a, b;

    for (a = 10; a <= 20; a++) {
       printf("value of a: %d\n", a);
    }
    
    printf("\nNested For Loop\n");

    for (a = 10; a <= 15; a++) {
        for (b = 100; b <= 120; b = b + 15) {
           printf("value of a: %d b: %d product: %d\n", a, b, a*b);
        }
    }

    /* we need to fix a bug in this program */
    printf("\nWhile Loop\n");
    while (a < 20) {
        printf("value of a: %d\n", a);
        a++;
    }

    printf("\nWhile Loop over a string\n");
    /* strings are null terminated */
    char greeting[] = "Hello, world!";
    int i = 0;
    while (greeting[i]) {
        printf("value of a: %c\n", greeting[i]);
        i++;
    }

    a = 100;
    
    printf("\nWhile Loop\n");
    while (a < 20) {
        printf("[while] value of a: %d\n", a);
        a++;
    }

    printf("\nDo Loop\n");
    do {
        printf("[do] value of a: %d\n", a);
        a++;
    } while (a < 20);


    return 0;
}
INFO