if/else
: conditional execution
switch
: conditional execution
while
: repeated execution
do
…while
: repeated execution
for
: repeated execution
while (expression) { statement; … statement; } | do { statement; … statement; } while (expression); | for (expr1; expr2; expr3) { statement; … statement; } |
while (condition) statement; while (condition) { statement; … statement; }
% ./a.out 1 2 3
int number = 1; while (number <= 3) { printf("%d\n", number); number++; }
1 2 3
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
// 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
int number = 1; printf("1\n"); while (number < 10) { printf("%d\n", number); number++; }
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; }
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
int i=1, j; while (i<5) { printf("%d\n", i); j++; }
i<5
.
j
is incremented, not i
.
for (initialize; condition; increment) { statement; … statement; }
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
What does this print?
for (int i=1; i<=3; i=i+1) printf("alpha\n"); printf("beta\n");
for (int i=1; i<=3; i=i+1) printf("alpha\n"); printf("beta\n");
alpha alpha alpha beta
for
loop
is only the first printf
, just like an if
statement.
{ }
.
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; }
for
loop,
but this is evil, without a big comment.
for
-loop is like a contract between the code writer
and the code reader. Don’t be sneaky!
for
loop should only be
used for loops that iterate a fixed number of times.
while
loop to express loops
that may execute an undetermined number of times.
for (int i=1; i<=25; i=i+1); printf("%d", i*i);
What does this print?
for (int x=1; x<=12; x++) { for (int y=1; y<=5; y++) printf("%d", x); printf("\n"); }
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
for (int i=0; i<5; i++) { printf("%d", i); i += 1; }
% ./a.out 0 2 4
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
break
— a keyword that causes the program
to continue execution after the enclosing loop
continue
— a keyword causing execution to
move to the next iteration of the loop
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
continue
skipped the printing for teenagers, but kept the loop going.
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
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
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.