Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial #5 Summer 2005. while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");

Similar presentations


Presentation on theme: "Tutorial #5 Summer 2005. while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");"— Presentation transcript:

1 Tutorial #5 Summer 2005

2 while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");

3 For Loop #include int main() { int secs; for (secs = 2; secs > 0; secs--) printf(“%d seconds!\n”, secs); printf(“The End!”); return 0 ; }

4 While Loop #include int main() { char ch; ch = getchar(); while (ch == ‘ ‘ || ch == ‘\n’) ch = getchar(); return 0; }

5 Do While Loop #include int main() { char ch; do { ch = getchar(); } while (ch == ‘ ‘ || ch == ‘\n’) ; return 0; }

6 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");

7 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 ; }

8 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 ; }

9 break #include int main( void ) { int count; char ch; while ((ch = getchar()) != ‘\n’ && ch != ‘\t’) { putchar(ch); } return 0 ; }

10 Continue #include int main( void ) { int count = 0; char ch; while (count < 10) { ch = getchar(); if (ch == ‘\n’) continue; putchar(ch); count++; } return 0 ; }

11 Continue #include int main( void ) { int count = 0; char ch; while (count < 10) { ch = getchar(); if (ch != ‘\n’) { putchar(ch); count++; } return 0 ; }

12 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 ; }

13 Continue #include int main( void ) { int count; char ch; for (count = 0; count < 10; ++count) { ch = getchar(); if (ch != ‘\n’) putchar(ch); } return 0 ; }

14 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 ; }

15 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 ; }

16 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 ; }

17 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; }

18 Arrays #include #define N 10 int main() { int arr[N]; for (i = 0; i < N; ++i) arr[i] = i; return 0; }

19 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; }

20 Arrays #include #define N 10 int main() { int arr[N] = {0}; … return 0; }

21 Arrays #include #define N 5 int main() { char arr[N]; int i; for (i = 0; i < N; ++i) scanf(“%c”, &a[i]);... return 0; }

22 Arrays – Find the errors #include int main() { int arr[2] = {1,2,3,4}; … return 0 ; }

23 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 ; }


Download ppt "Tutorial #5 Summer 2005. while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");"

Similar presentations


Ads by Google