Modulus Operator include stdio h main int a
- Slides: 16
Modulus Operator #include <stdio. h> main() { int a, b, q, r; printf(" a b a/b a%%bn"); while (scanf("%d%d", &a, &b) != EOF) { q = a / b; /* quotient */ r = a % b; /* remainder */ printf("%4 d %5 d %5 dn", a, b, q, r); } } a. out < in. dat a 10 3 -10 10 -10 b 3 10 3 -3 -3 a/b 3 0 -3 -3 3 (in. dat contains a list of the first two columns of numbers a and b) a%b 1 It is always true that 3 -1 a = q*b + r 1 with |r|<|b|, b 0. -1
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; z = x || y; /* y gets 0 */ /* z gets 1 */
Exercise, Do it now What are printed? #include <stdio. h> main() { int x, y, z, u, v; int j 1, j 2, j 3, j 4, j 5; x = -1; y = 3; z = 0; u = 100; v = 7; j 1 j 2 j 3 j 4 j 5 = = = v/u - x % y; y % v % y; x && y || u < v; x <= !y || z; !!x == x; printf("%d %d %dn", j 1, j 2, j 3, j 4, j 5); } 10110
The For Loop • For( ; ; ) is a statement for looping. • The general form is for(expr 1; expr 2; expr 3) action where expr 1 is evaluated first and once only. expr 2 is checked as a condition [like in while(expr)]. If expr 2 is true, action is performed. Then expr 3 is evaluated. Repeat expr 2/action/expr 3 until expr 2 becomes false.
For Loop Initialization test update for( expr 1 ; expr 2 ; expr 3 ) { action }
For Loop Example sum = 0; for(i = 1; i <= 4; i += 1){ sum += i; printf("i=%d, sum=%dn", i, sum); } prints i=1, i=2, i=3, i=4, sum=1 sum=3 sum=6 sum=10
For Loop Example sum = 0; for(i = 1; i <= 4; ++i) sum += i; printf("i=%d, sum=%dn", i, sum); prints i=5, sum=10 • ++ is called increment operator. ++i is the same as i = i + 1, or i += 1. • ++ applies only to integral type variables.
Equivalence of while and for For(expr 1; expr 2; expr 3) action Is equivalent to expr 1; while (expr 2) { action expr 3; }
Equivalence of for and while(expr) action Is equivalent to for( ; expr; action } ) {
Infinite Loop (looping forever) • With for loop we can write for( ; ; ) action • If expr 2 (test) is missing, the condition is regarded as true. • With while, we can say while ( 1 ) action
Comma Operator ", " • The expression expr 1, expr 2, . . . , exprn can be used in places where only one expression is logically allowed. The expr 1, expr 2, 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 %dn", i, j) prints 0 1 2 5 4 3
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 3. 7. 4 and 3. 7. 5, on page 105.
Reading/Home Working • Read Chapter 3, page 99 to 110. • Work on Problems – Section 3. 6, page 101102, 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.
- #include stdio.h void main()
- Include stdio.h
- #include string.h
- Sum0
- #include stdio.h #include conio.h #include stdlib.h
- Int main
- Int main
- H int
- #include stdio.h int main(void)
- Public void drawsquare(int x, int y, int len)
- Interface calculator public int add(int a int b)
- Public int divide(int a int b)
- Int max(int x int y)