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
int number = 1;
while (number <= 3) {
    printf("%d\n", number);
    number++;
}
1
2
3

Control Flow: while

What’s the difference between the two?

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

Example 6 : Count to 10

//  Count to 10 — with off-by-1 error
int number;
while (number < 10) {
    printf("%d\n", number);
    number++;
}
0
1
2
3
4
5
6
7
8
9

Example 7 : Count to 10 with error

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

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

int old = 1, current = 1;
printf("1 ");
while (current < 1000) {
    printf("%d ", current);
    int next = current + old;
    old = current;
    current = next;
}
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 

Control Flow: Common Problems

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

int i;
for (i=0; i<5; i=i+1)
    printf("i: %d\n", i);
i: 0
i: 1
i: 2
i: 3
i: 4

for loop quiz

What does this print?

for (int i=1; i<=3; i=i+1)
    printf("alpha\n");
    printf("beta\n");

for loop quiz

for (int i=1; i<=3; i=i+1)
    printf("alpha\n");
    printf("beta\n");
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?

    for (int i=1; i<=25; i=i+1);
        printf("%d", i*i);

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;
for (int i=1; !done; i++) {
    printf("i=%d\n", i);
    if (i>3 && pow(2, i) == pow(i, 2))
        done = 1;        // we’re done with the loop
    printf("yup yup yup\n");
}
i=1
yup yup yup
i=2
yup yup yup
i=3
yup yup yup
i=4
yup yup yup

pow(a,b) means exponentiation, a to the b power: ab

A Bit More Control Flow

A Bit More Control Flow

for (int age=0; age<1000; age++) {
    if (age == 30)
        break;
    else if (age>=13 && age<=19) // teenagers!
        continue;
    printf("%d ", age);
}
0 1 2 3 4 5 6 7 8 9 10 11 12 20 21 22 23 24 25 26 27 28 29 

Deliberate infinite loops

Sometimes, you want a more-or-less infinite loop.

We’re looking for a whole number whose integer square root, times six, plus seven, is 133.

int n = 1;
while (1) {
    int sr = sqrt(n);
    if (7 + sr*6 == 133)
        break;
    n++;
}
printf("We found it!  n=%d\n", n);
We found it!  n=441

Another way to do that

Or, you can do it this way:

int n = 1;
for (;;) {
    int sr = sqrt(n);
    if (7 + sr*6 == 133)
        break;
    n++;
}
printf("We found it!  n=%d\n", n);
We found it!  n=441

Yet another way to do that

Better yet:

int n;
for (n=1; ; n++) {
    int sr = sqrt(n);
    if (7 + sr*6 == 133)
        break;
}
printf("We found it!  n=%d\n", n);
We found it!  n=441

The reader will understand that we’re counting up, starting at 1, with no definite upper limit.

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-02-25T13:36

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building