Web Page - http://www.cs.tau.ac.il/~efif/courses/ComputerProgramming
Precedence | Associativity | Operator |
---|---|---|
15 | left | ++, -- (post) |
14 | right | ++, -- (pre) |
13 | left | *, /, % |
12 | left | +, - |
10 | left | <, >, <=, >= |
9 | left | ==, != |
2 | right | =, +=, -=, *=, /=, %= |
if (abs (a - b) < 0.01) { … } |
| versus |
|
int a = 1, b = 2, c = 3, d = 4; if ((a == b) && (c != d)) { … } |
Precedence | Associativity | Operator |
---|---|---|
15 | left | ++, -- (post) |
14 | right | ++, -- (pre) |
13 | left | *, /, % |
12 | left | +, - |
10 | left | <, >, <=, >= |
9 | left | ==, != |
5 | left | && |
4 | left | || |
2 | right | =, +=, -=, *=, /=, %= |
expr1 && expr2 || expr3 && expr4 |
expr1 || expr2 && expr3 || expr4 |
(expr1 || (expr2 && expr3)) || expr4 |
i = j++ 5 * (fahrenheit - 32) / 9 (a < b) || (c > d) |
while (i > 0) for (i = 0; i < 10; ++i) |
i = i++; i = (j = 5) + j++; |
i = 5; i++; ++i; celsius = 5 * (fahrenheit – 32) / 9; |
a + b; 32; |
; |
for(; i < n; i++) ... |
/* * HelloWorld: A simple C program */ #include <stdio.h> int main (void) { printf ("Hello, world!\n"); return 0; } |
#include <stdio.h> int main (void) { int i; int power = 1; printf ("2 to the %d power is %d\n", i, power); for (i = 0; i < 10; ++i) { power *=2; printf ("2 to the %d power is %d\n", i, power); } return 0; } |
if (grade >= 90) printf ("Congratulations!\n"); printf ("Your grade is %d\n", grade); |
/* * calculate the absolute value of a number */ int abs (int number) { if (number < 0) number = -number; return number; } |
int i, j; int min; if (i < j) { min = i; printf ("i is less than j\n"); } if (j < i) { min = j; printf ("j is less than or equal to i\n"); } |
int min; int i, j; if (i < j) { min = i; printf ("i is less than j\n"); } else { min = j; printf ("j is less than i\n"); } |
int min; int i, j; if (i < j) min = i; else min = j; |
| versus |
|
if (month == FEB) { if (is_leap_year (year)) { if (day > 29) legal = FALSE; } else if (day > 28) legal = FALSE; } |
int i, j, k; if (j != 0) k = i / j; else printf ("Error: division by 0!\n"); |
if (grade >= 90) printf ("A\n"); else if (grade >= 80) printf ("B\n"); else if (grade >= 70) printf ("C\n"); else if (grade >= 60) printf ("D\n"); else printf ("F\n"); |
if (grade >= 90) printf ("A\n"); else if (grade >= 80) printf ("B\n"); else if (grade >= 70) printf ("C\n"); else if (grade >= 60) printf ("D\n"); else printf ("F\n"); |
int a[10]; float temperatures[10]; |
a[0] = 10; a[1] = 20; a[2] = a[0] + a[1]; |
a = 0; /* WRONG */ |
int b[10]; b = a;/* WRONG */ |
int b[10]; for(i = 0; i < 10; i++) b[i] = a[i]; |