Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial #4 Summer 2005.

Similar presentations


Presentation on theme: "Tutorial #4 Summer 2005."— Presentation transcript:

1 Tutorial #4 Summer 2005

2 If int main() { if (30) printf("this will ___ be printed"); }
return 0;

3 If int main() { if (0) printf("this will ___ be printed"); } return 0;

4 If int main() { if (-24) printf("this will ___ be printed"); }
return 0;

5 If int main() { int digit; …
if ((digit == 0) || ((digit > 3) && (digit != 7))) …. }

6 if … else if … else #include <stdio.h> int main() { char c;
if ((c = getchar() != ‘\n’) { if ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c<= ‘Z’) printf(“A letter\n”); } else printf(“not a letter”); printf(“End of line”);

7 if … else if … else #include <stdio.h> int main() { char c;
if ((c = getchar() != ‘\n’) if ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c<= ‘Z’) printf(“A letter\n”); else printf(“not a letter”); printf(“End of line”); }

8 if … else if … else #include <stdio.h> int main() { char c;
if ((c = getchar() != ‘\n’) if ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c<= ‘Z’) printf(“A letter\n”); else printf(“not a letter”); }

9 if … else if … else #include <stdio.h> int main() { char c;
if ((c = getchar() != ‘\n’) if ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c<= ‘Z’) printf(“A letter\n”); else printf(“End of line”); }

10 if … else if … else #include <stdio.h> int main() { char c;
if ((c = getchar() != ‘\n’) if ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c<= ‘Z’) printf(“A letter\n”); else; else printf(“End of line”); }

11 if … else if … else #include <stdio.h> int main() {
int a = 2, b = 1; if (a > 2 || b < 3) printf(“1”); else printf(“2”); if (a > 2) printf(“1”); else if (b < 3) else printf(“2”); }

12 if … else if … else #include <stdio.h> int main() {
int a = 2, b = 1; if (2 < a < 3) printf(“2 < a < 3”); }

13 if … else if … else #include <stdio.h> int main() { int color;
scanf(“%d”, &color); if (color == 1) printf(“red\n”); else if (color == 2) printf(“yellow\n”); else if (color == 3) printf(“blue\n”); return 0; }

14 Switch #include <stdio.h> int main() case 2: {
int color; scanf(“%d”, &color); switch (color) case 1: printf(“red\n”); break; case 2: printf(“yellow\n”); break; case 3: printf(“blue\n”); } return 0;

15 Switch #include <stdio.h> case 2: int main() printf(“yellow\n”);
{ int color; scanf(“%d”, &color); switch (color) case 1: printf(“red\n”); break; case 2: printf(“yellow\n”); break; case 3: printf(“blue\n”); default: printf(“No color\n”); } return 0;

16 Switch #include <stdio.h> int main() { int color;
scanf(“%d”, &color); switch (color) case 1: printf(“red\n”); case 2: printf(“yellow\n”); case 3: printf(“blue\n”); } return 0;

17 Operators… #include <stdio.h> #include <stdio.h>
int main() { int a, b; scanf(“%d%d”, &a, &b); if (a == 0 || b/a > 1) ... return 0; } #include <stdio.h> int main() { int a, b; scanf(“%d%d”, &a, &b); if (b/a > 1 || a == 0) ... return 0; }

18 Operators… #include <stdio.h> int main() { int a; … if (a++)
printf("a was not zero"); else printf("a is not zero"); return 0; }

19 Operators… #include <stdio.h> int main() { int x, y;
scanf(“%d %d”, &x, &y); if ((x = y) != 0) ... return 0; }

20 Operators… #include <stdio.h> int main() { int x = 2, y, z;
return 0; }

21 Operators… int x = 2, y = 1, z = 0; x = x && y || z;
#include <stdio.h> int main() { int x = 2, y = 1, z = 0; x = x && y || z; printf(“%d\n”, x); printf(“%d\n”, x || !y && z); x = y = 1; z = x++ -1 printf(“%d\n”, z); z += -x y; printf(“%d\n”, x); z = x / ++x; return 0; }

22 scanf double age, height; int res;
printf("enter your age and height"); res = scanf("%lf %lf", &age, &height); if (res != 2) { printf(“scanf failed”); return -1; }

23 ?: char ch; int x; … if (ch >= ‘a’ && ch <= ‘z’) x = 8; else

24 ?: char ch; int x; x = ((ch >= ‘a’ && ch <= ‘z’) ? 8 : 7);

25 ?: char ch; int x; (ch >= ‘a’ && ch <= ‘z’) ? x = 8 : x = 7;

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

27 while #include <stdio.h> int main() { char ch = ‘a’;
while (ch <= ‘z’) printf(“%d: %c\n”, ch, ch); ch++; }

28 While loops double age; printf("enter your age: ");
scanf("%lf", &age); while (age < 18) { printf("Invalid age, please try again: "); } printf("Welcome to C-Stuff.com, the"); printf("#1 adult site on the web!\n");

29 While Loop #include <stdio.h> int main() { int n = 3; while (n)
printf(“%d\n”, n--); return 0; }

30 While Loop #include <stdio.h> int main() { int n = -3; while (n)
printf(“%d\n”, n++); return 0; }

31 While Loop #include <stdio.h> int main() { char ch;
ch = getchar(); while (ch == ‘ ‘ || ch == ‘\n’) return 0; }

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

33 for #include <stdio.h> int main() { char ch;
for (ch = ‘a’; ch <= ‘z’; ++ch) printf(“%d: %c\n”, ch, ch); }

34 For Loop #include <stdio.h> int main() { int i; }
for (i = 0; i < 60; i += 10) printf(“%d \n”, i); return 0; }

35 For Loop #include <stdio.h> int main() { int i; }
for (i = 0; i < 60; i += 10) printf(“%d \n”, i); return 0; } ;

36 For Loop #include <stdio.h> #include <stdio.h> int main()
{ int count = 1; for ( ; count <= 5; ++count) printf(“%d \n”, count); return 0; } #include <stdio.h> int main() { int count = 1; while (count <= 5) printf(“%d \n”, count); ++count; } return 0;


Download ppt "Tutorial #4 Summer 2005."

Similar presentations


Ads by Google