Tutorial #4 Summer 2005
If int main() { if (30) printf("this will ___ be printed"); } return 0;
If int main() { if (0) printf("this will ___ be printed"); } return 0;
If int main() { if (-24) printf("this will ___ be printed"); } return 0;
If int main() { int digit; … if ((digit == 0) || ((digit > 3) && (digit != 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”);
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”); }
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”); }
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”); }
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”); }
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”); }
if … else if … else #include <stdio.h> int main() { int a = 2, b = 1; if (2 < a < 3) printf(“2 < a < 3”); }
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; }
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;
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;
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;
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; }
Operators… #include <stdio.h> int main() { int a; … if (a++) printf("a was not zero"); else printf("a is not zero"); return 0; }
Operators… #include <stdio.h> int main() { int x, y; scanf(“%d %d”, &x, &y); if ((x = y) != 0) ... return 0; }
Operators… #include <stdio.h> int main() { int x = 2, y, z; return 0; }
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; }
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; }
?: char ch; int x; … if (ch >= ‘a’ && ch <= ‘z’) x = 8; else
?: char ch; int x; … x = ((ch >= ‘a’ && ch <= ‘z’) ? 8 : 7);
?: char ch; int x; … (ch >= ‘a’ && ch <= ‘z’) ? x = 8 : x = 7;
while x = -2; while (x < 0) { printf("x is still negative :(\n"); x = x + 1; } printf("x is no longer negative.\n");
while #include <stdio.h> int main() { char ch = ‘a’; while (ch <= ‘z’) printf(“%d: %c\n”, ch, ch); ch++; }
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");
While Loop #include <stdio.h> int main() { int n = 3; while (n) printf(“%d\n”, n--); return 0; }
While Loop #include <stdio.h> int main() { int n = -3; while (n) printf(“%d\n”, n++); return 0; }
While Loop #include <stdio.h> int main() { char ch; ch = getchar(); while (ch == ‘ ‘ || ch == ‘\n’) return 0; }
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 ; }
for #include <stdio.h> int main() { char ch; for (ch = ‘a’; ch <= ‘z’; ++ch) printf(“%d: %c\n”, ch, ch); }
For Loop #include <stdio.h> int main() { int i; } for (i = 0; i < 60; i += 10) printf(“%d \n”, i); return 0; }
For Loop #include <stdio.h> int main() { int i; } for (i = 0; i < 60; i += 10) printf(“%d \n”, i); return 0; } ;
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;