Web Page - http://www.cs.tau.ac.il/~efif/courses/ComputerProgramming
Precedence | Associativity | Operator |
---|---|---|
15 | left | expr>++, expr>-- (post) |
14 | right | ++expr>, --expr> (pre) !<, ~, +, -, *, & (unary) |
13 | left | *, /, % |
12 | left | +, - |
11 | left | <<, >> |
10 | left | <, >, <=, >= |
9 | left | ==, != |
8 | left | & (binary) |
7 | left | ^ (binary) |
6 | left | | (binary) |
5 | left | && |
4 | left | || |
3 | left | expr ? true_expr : false_expr |
2 | right | =, +=, -=, *=, /=, %=, <<=, >>=, <=, ^=, |= |
1 | left | , |
/* * z = max (a, b); */ if (a > b) z = a; else z = b; |
/* * z = max (a, b); */ z = (a > b) ? a : b; |
z = (a > b) ? a++ : b--; |
switch (expression) {
case const-expr: statements
case const-expr: statements
…
default: statements
}
switch (grade/10) { case 10: case 9: printf ("A\n"); break; case 8: printf ("B\n"); break; case 7: printf ("C\n"); break; case 6: printf ("D\n"); break; default: printf ("F\n"); break; } |
case 8: printf ("B\n"); break; |
switch (grade/10) { case 10: case 9: printf ("A\n"); case 8: printf ("B\n"); case 7: printf ("C\n"); case 6: printf ("D\n"); default: printf ("F\n"); } |
switch (c) { case 'a': case 'A': a_count++; break; case 'b': case 'B': b_count++; break; } |
/* compute n factorial */ int i = 0; int factorial = 1; while (i < n) { i++; factorial *= i; } |
/* print powers of 2 that are less than 1000 */ int i = 1; while (i < 1000) { printf ("%d\n", i); i *= 2; } |
while ((c = getchar()) != EOF) { if (c == ' ') blank_count++; else if (c >= '0' && c <= '9') digit_count++; else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') letter_count++; else if (c == '\n') newline_count++; } |
while (fahr <= upper) { celsius = 5 * (fahr - 32) / 9; printf ("%d\t%d\n", fahr, celsius); fahr = fahr + step; } |
expr1;
while (expr2) {
statement
expr3;
}
for (i = 0; i < 10; i = i + 1) printf ("i is %d\n", i); |
for (i = 0; i < 10; i = i + 1) printf ("i is %d\n", i); |
int i = 0; while (i < 10) { printf ("i is %d\n", i); i = i + 1; } |
/* compute n factorial */ int i = 0; int factorial = 1; while (i < n) { i++; factorial *= i; } |
/* compute n factorial */ int factorial = 1; for (i = 1; i <= n; ++i) { factorial *= i; } |
/* print powers of 2 that are less than 1000 */ int i = 1; while (i < 1000) { printf ("%d\n", i); i *= 2; } |
/* print powers of 2 that are less than 1000 */ int i; for (i = 1; i < 1000, i *= 2) { printf ("%d\n", i); } |
do { printf ("Input a positive integer: "); scanf ("%d", &n); if (error = (n <= 0)) printf ("\nERROR: Try again!\n"); } while (error); |
while (1) { scanf ("%f", &x); if (x < 0.0) break; printf ("%f\n", sqrt(x)); } |
#include <stdio.h> #include <math.h> int main (void) { int i, j; printf ("%d\n", 2); for (i = 3; i <= 100; ++i) { for (j = 2; j < i; ++j) { if (i % j == 0) break; if (j > sqrt(i)) { printf (%d\n", i); break; } } } return 0; } |
#include <stdio.h> int main (void) { int i, j; printf (" "); for (j = 1; j <= 10; ++j) printf (" %3d", j); printf ("\n"); for (i = 1; i <= 10; ++i) { printf ("%2d", i); for (j = 1; j <= 10; ++j) printf (" %3d", i*j); printf ("\n"); } return 0; } |
printf (" "); for (j = 1; j <= 10; ++j) printf (" %3d", j); printf ("\n"); |
for (i = 1; i <= 10; ++i) { printf ("%2d", i); for (j = 1; j <= 10; ++j) printf (" %3d", i*j); printf ("\n"); } |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
2 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
3 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 |
… |