Tutorial #5 Summer 2005
while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");
For Loop #include int main() { int secs; for (secs = 2; secs > 0; secs--) printf(“%d seconds!\n”, secs); printf(“The End!”); return 0 ; }
While Loop #include int main() { char ch; ch = getchar(); while (ch == ‘ ‘ || ch == ‘\n’) ch = getchar(); return 0; }
Do While Loop #include int main() { char ch; do { ch = getchar(); } while (ch == ‘ ‘ || ch == ‘\n’) ; return 0; }
While loops double age; printf("enter your age: "); scanf("%lf", &age); while (age < 18) { printf("Invalid age, please try again: "); scanf("%lf", &age); } printf("Welcome to C-Stuff.com, the"); printf("#1 adult site on the web!\n");
break #include int main( void ) { int count; char ch; while ((ch = getchar()) != ‘\n’) { if (ch == ‘\t’) break; putchar(ch); } printf(“After the loop”); return 0 ; }
break #include int main( void ) { int i,j; char ch; for(i = 0; i < 9; ++i) { for (j = 0; j < i; ++j) { scanf(“%c”, &ch); if (ch == ‘ ‘) break; } printf(“After the j loop”); } printf(“After the i loop”); return 0 ; }
break #include int main( void ) { int count; char ch; while ((ch = getchar()) != ‘\n’ && ch != ‘\t’) { putchar(ch); } return 0 ; }
Continue #include int main( void ) { int count = 0; char ch; while (count < 10) { ch = getchar(); if (ch == ‘\n’) continue; putchar(ch); count++; } return 0 ; }
Continue #include int main( void ) { int count = 0; char ch; while (count < 10) { ch = getchar(); if (ch != ‘\n’) { putchar(ch); count++; } return 0 ; }
Continue #include int main( void ) { int count; char ch; for (count = 0; count < 10; ++count) { ch = getchar(); if (ch == ‘\n’) continue; putchar(ch); } return 0 ; }
Continue #include int main( void ) { int count; char ch; for (count = 0; count < 10; ++count) { ch = getchar(); if (ch != ‘\n’) putchar(ch); } return 0 ; }
Continue #include int main( void ) { int i; for (i = 0; i < 100; i += 2) if (i % 3 == 0 || i % 7 == 0) continue; else printf("%d ", i); return 0 ; }
Continue #include int main( void ) { int i; for (i = 0; i < 100; i += 2) if (i % 3 != 0 && i % 7 != 0) printf("%d ", i); return 0 ; }
enum #include int main( void ) { enum month {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; enum color {GREEN = 1, RED, BLUE = 7, YELLOW}; enum animals {MONKEY, FISH, DOG, CAT, HORSE, NUM_ANIMALS}; return 0 ; }
enum #include enum day {SUN, MON, TUE, WED, THU, FRI, SAT}; int main( void ) { int cnt; for (cnt = SUN) ; cnt <= SAT); cnt++) { switch (cnt) { case SUN: printf("Sunday"); break; case MON: printf("Monday"); break; case Tue: printf("Tuesday"); break; case WED: printf("Wednesday"); break; case THU: printf("Thursday"); break; case FRI: printf("Friday"); break; case SAT: printf("Saturday"); break; default: printf("ERROR!"); } return 0; }
Arrays #include #define N 10 int main() { int arr[N]; for (i = 0; i < N; ++i) arr[i] = i; return 0; }
Arrays #include #define N 10 int main() { double b[] = {1.5, 3.0, 5.7}; char c[] = {`a`, `b`, `c`, `d`, `e`}; int a[10] = {1, 5, 7}; … return 0; }
Arrays #include #define N 10 int main() { int arr[N] = {0}; … return 0; }
Arrays #include #define N 5 int main() { char arr[N]; int i; for (i = 0; i < N; ++i) scanf(“%c”, &a[i]);... return 0; }
Arrays – Find the errors #include int main() { int arr[2] = {1,2,3,4}; … return 0 ; }
Arrays – Find the errors #include int main() { int n = 3; int arr[n] = {1,2,3}; int i; for (i = 1; i <= n; ++i) printf(“%d\n”, arr[i++]); return 0 ; }