CS156

CS156: Intro to C, Part I

Spring 2018

Loops

See this page as a slide show

CS156 Loops: Loops

Control Flow

Three types of loops

while (expression) {
    statement;
    …
    statement;
}
do {
    statement;
    …
    statement;
} while (expression);
for (expr1; expr2; expr3) {
    statement;
    …
    statement;
}

Control Flow: while

while (condition)
        statement;
 
while (condition) {
        statement;
        …
        statement;
}

Control Flow: while

% ./a.out
1
2
3
#include <stdio.h>
int main() {
        int number = 1;
        while (number <= 3) {
                printf("%d\n", number);
                number = number + 1;
        }
        return 0;
}

Control Flow: while

What’s the difference between the two?

int number = 0;
while (number < 3) {
    number = number + 1;
    printf("%d\n", number);
}
1
2
3
int number = 1;
while (number <= 3) {
    printf("%d\n", number);
    number = number + 1;
}
1
2
3

Example 6 : Count to 10

//  Count to 10 — with off-by-1 error
#include <stdio.h>
int main() {
        int number;
        while (number < 10) {
                printf("%d\n", number);
                number = number + 1;
        }
}

Example 7 : Count to 10 with error

#include <stdio.h>
int main() {
    int number = 1;
    printf("1\n");
    while (number < 10) {
        printf("%d\n", number);
        number = number + 1;
    }
}

While loop example

Reverse a number

    #include <stdio.h>
    int main() {
        int value=0;
        while (value <= 0) {
            printf("Enter number: ");
            scanf("%d", &value);
            if (value <= 0)
                printf("Must be positive\n");
        }
        while (value != 0) {
            int r_digit = value % 10;
            printf("%d", r_digit);
            value /= 10;
        }
        printf("\n");
        return 0;
    }

Example 6 : Fibonacci Numbers

#include <stdio.h>
int main() {
    int old = 1, current = 1;
    printf("1 ");
    while (current < 100) {
        printf("%d ", current);
        int next = current + old;
        old = current;
        current = next;
    }
    return 0;
}
1 1 2 3 5 8 13 21 34 55 89 

Control Flow: Common Problems

#include <stdio.h>
int main() {
    int i=1, j;
    while (i<5) {
        printf("%d\n", i);
        j++;
    }
}

Control Flow: for Loops

for (initialize; condition; increment) {
        statement;
        …
        statement;
}

for loop example

#include <stdio.h>
int main() {
    int i;
    for (i=0; i<5; i=i+1)
        printf("i: %d\n", i);
    return 0;
}
i: 0
i: 1
i: 2
i: 3
i: 4

for loop quiz

What does this print?

#include <stdio.h>
int main() {
    for (int i=1; i<=3; i=i+1)
        printf("alpha\n");
        printf("beta\n");
    return 0;
}

for loop quiz

#include <stdio.h>
int main() {
    for (int i=1; i<=3; i=i+1)
        printf("alpha\n");
        printf("beta\n");
    return 0;
}
alpha
alpha
alpha
beta

Equivalency of for and while

The for and while loops are equivalent in what they do:

        for (initialize; condition; increment) {
                statement;
                statement;
        }

is the same as:

        initialize;
        while (condition) {
                statement;
                statement;
                increment;
        }

Equivalency of for and while

What is wrong with this?

        #include <stdio.h>
        int main() {
                for (int i=1; i<=25; i=i+1);
                        printf("%d", i*i);
                return 0;
        }

Loops inside Loops

What does this print?

for (int x=1; x<=12; x++) {
    for (int y=1; y<=5; y++)
        printf("%d", x);
    printf("\n");
}

Loops inside Loops

for (int x=1; x<=12; x++) {
    for (int y=1; y<=5; y++)
        printf("%d", x);
    printf("\n");
}
11111
22222
33333
44444
55555
66666
77777
88888
99999
1010101010
1111111111
1212121212

Careful!

for (int i=0; i<5; i++) {
    printf("%d", i);
    i += 1;
}
% ./a.out
0
2
4

Loop Flags

        int done = 0;
        while (!done) {
            // insert code here
            if (x && !y || z)    // example test
                done = 1;        // we’re done with the loop
            // more code here
        }

A Bit More Control Flow

A Bit More Control Flow

while (1) {
    printf("Enter a number: ");
    scanf("%d", &control);
    if (control == 0)
        break;
    else if (control == 1)
        continue;
    printf("Your number was: %d\n", control);
}

Another way to do it:

for (;;) {
    printf("Enter a number: ");
    scanf("%d", &control);
    if (control == 0)
        break;
    else if (control == 1)
        continue;
    printf("Your number was: %d\n", control);
}

Modified: 2017-12-19T11:19

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building