Modulus Operator #include main() { int a, b, q, r; printf(" a b a/b a%b\n"); while (scanf("%d%d", &a, &b) != EOF) { q = a / b; /* quotient */ r = a % b; /* remainder */ printf("%4d %5d %5d %5d\n", a, b, q, r); } a.out < in.dat a b a/b a%b (in.dat contains a list of the first two columns of numbers a and b) It is always true that a = q*b + r with |r|<|b|, b 0.
Relational/Logical Expressions Any values can be used in logical expressions. Zero (0) is interpreted as false; any nonzero values (such as 1, -1, 5) are interpreted as true. The results of relational or logical operations are always 0 or 1. Examples: x = 5; y = !x; /* y gets 0 */ z = x || y; /* z gets 1 */
Exercise, Do it now What are printed? #include main() { int x, y, z, u, v; int j1, j2, j3, j4, j5; x = -1; y = 3; z = 0; u = 100; v = 7; j1 = v/u - x % y; j2 = y % v % y; j3 = x && y || u < v; j4 = x <= !y || z; j5 = !!x == x; printf("%d %d %d %d %d\n", j1, j2, j3, j4, j5); } 10110
The For Loop For( ; ; ) is a statement for looping. The general form is for(expr1; expr2; expr3) action where expr1 is evaluated first and once only. expr2 is checked as a condition [like in while(expr)]. If expr2 is true, action is performed. Then expr3 is evaluated. Repeat expr2/action/expr3 until expr2 becomes false.
For Loop for( ; ; ) { } expr2expr3 expr1 action Initialization test update
For Loop Example sum = 0; for(i = 1; i <= 4; i += 1){ sum += i; printf("i=%d, sum=%d\n", i, sum); } prints i=1, sum=1 i=2, sum=3 i=3, sum=6 i=4, sum=10
For Loop Example sum = 0; for(i = 1; i <= 4; ++i) sum += i; printf("i=%d, sum=%d\n", i, sum); prints i=5, sum=10 ++ is called increment operator. ++i is the same as i = i + 1, or i += applies only to integral type variables.
Equivalence of while and for For(expr1; expr2; expr3) action Is equivalent to expr1; while (expr2) { action expr3; }
Equivalence of for and while while(expr) action Is equivalent to for( ; expr; ) { action }
Infinite Loop (looping forever) With for loop we can write for( ; ; ) action If expr2 (test) is missing, the condition is regarded as true. With while, we can say while ( 1 ) action
Comma Operator "," The expression expr1, expr2,..., exprn can be used in places where only one expression is logically allowed. The expr1, expr2, and so on are evaluated in turn, the value of the whole expression is the value of exprn. Example for(i=0,j=5; i<j; ++i,--j) printf("%d %d\n", i, j) prints
Increment ( ++ ) Operator When we execute ++x (prefix) two things happen: –The value of the variable x is increased by 1. –The value of the expression ++x is equal to the new value (original value plus 1). When we execute x++ (postfix) two things happen: –The value of the variable x is increased by 1. Same as ++x. –The value of the expression x++ is equal to the old value (original value of x).
Decrement ( -- ) Operator When we execute --x two things happen: The value of the variable x is decreased by 1. The value of the expression --x is equal to the new value (original value minus 1). When we execute x-- two things happen: The value of the variable x is decreased by 1. Same as --x. The value of the expression x-- is equal to the old value (original value of x).
++/--, Examples int x, y; x = 4; y = --x; /* y gets 3 */ x = 1; y = 4; z = --y == 3 && !(x++ <=1); /* -- y is 3, -- y==3 is true, x++ increases x by 1, the value of the expression x++ is 1 (not 2), (x++ <= 1) is true. !(x++ <= 1) is false. True && false is false. So z gets 0. */ These are examples and 3.7.5, on page 105.
Reading/Home Working Read Chapter 3, page 99 to 110. Work on Problems –Section 3.6, page , exercise 1, 3, 5. –Section 3.7, page 106, exercise 1, 3, 5. Check your answers in the back of the textbook. Do not hand in.
Lab 1, Due Monday, 17 Aug 98 Work on Problems –Page 67-68, Programming exercise 2.3, 2.5, 2.9. –Page 111, Programming exercise 3.1 Follow the instructions given by our Teaching Assistant Alice Heng.