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
#include <stdio.h> int main() { int number = 1; while (number <= 3) { printf("%d\n", number); number = number + 1; } return 0; }
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
// 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; } }
#include <stdio.h> int main() { int number = 1; printf("1\n"); while (number < 10) { printf("%d\n", number); number = number + 1; } }
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; }
#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
#include <stdio.h> int main() { 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; }
#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
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; }
#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
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 should be avoided.
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.
#include <stdio.h> int main() { for (int i=1; i<=25; i=i+1); printf("%d", i*i); return 0; }
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; while (!done) { // insert code here if (x && !y || z) // example test done = 1; // we’re done with the loop // more code here }
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
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); }
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: 2016-07-26T18:39 User: Guest Check: HTML CSSEdit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |
![]() |