Assignment Operators Topics Increment and Decrement Operators Assignment

  • Slides: 31
Download presentation
Assignment Operators Topics • • • Increment and Decrement Operators Assignment Operators Debugging Tips

Assignment Operators Topics • • • Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) <math. h> math library functions Reading • Sections 5 A. 5 L 13 1

Increment and Decrement Operators • The increment operator ++ • The decrement operator -

Increment and Decrement Operators • The increment operator ++ • The decrement operator - • Precedence: lower than (), but higher than * / and % • Associativity: right to left • Increment and decrement operators can only be applied to variables, NOT to constants or expressions L 13 2

Precedence of Operators in this Lect. () ++ -* / % + < <=

Precedence of Operators in this Lect. () ++ -* / % + < <= > >= == != = += -= *= /= %= L 13 left to right to left to right left to right to left 3

Increment Operator • If we want to add one to a variable, we can

Increment Operator • If we want to add one to a variable, we can say: count = count + 1 ; • Programs often contain statements that increment variables, so to save on typing, C provides these shortcuts: count++ ; OR ++count ; Both do the same thing. They change the value of count by adding one to it. L 13 4

Post-Increment Operator • The position of the ++ determines when the value is incremented.

Post-Increment Operator • The position of the ++ determines when the value is incremented. If the ++ is after the variable, then the incrementing is done last (a postincrement). int amount, count ; count = 3 ; amount = 2 * count++ ; • amount gets the value of 2 * 3, which is 6, and then 1 gets added to count. • So, after executing the last line, amount is 6 and count is 4. L 13 5

Pre-Increment Operator • If the ++ is before the variable, then the incrementing is

Pre-Increment Operator • If the ++ is before the variable, then the incrementing is done first (a preincrement). int amount, count ; count = 3 ; amount = 2 * ++count ; • 1 gets added to count first, then amount gets the value of 2 * 4, which is 8. • So, after executing the last line, amount is 8 and count is 4. L 13 6

Code Example Using ++ #include <stdio. h> int main ( ) { int i

Code Example Using ++ #include <stdio. h> int main ( ) { int i = 1 ; /* count from 1 to 10 */ while ( i < 11 ) { printf (“%d ”, i) ; i++ ; /* same as ++i */ } return 0 ; } L 13 7

Same result as above, more concise # include <stdio. h> int main ( )

Same result as above, more concise # include <stdio. h> int main ( ) { int counter = 0 ; while ( ++counter <= 10 ) { printf (“%d ”, counter ) ; } return 0; /* initialization */ /* condition & inc. */ } L 13 8

A while loop with ++i i=0; while ( + + i < 11 )

A while loop with ++i i=0; while ( + + i < 11 ) { printf(“%d ”, i ); } Output is: 1 2 3 4 5 6 7 8 9 10 L 13 9

A while loop with i++ i=0; while ( i ++ < 11 ) {

A while loop with i++ i=0; while ( i ++ < 11 ) { printf(“%d ”, i ); } Output is: 1 2 3 4 5 6 7 8 9 10 11 L 13 10

The Increment expression in the for The following are all equivalent in the incrementing

The Increment expression in the for The following are all equivalent in the incrementing portion of the for structure: counter = counter + 1 counter += 1 ++counter++ Incrementing occurs after the body of the for is executed, so counter++ seems more appropriate. for ( counter = 1 ; counter < 10 ; counter++ ) L 13 11

Decrement Operator • If we want to subtract one from a variable, we can

Decrement Operator • If we want to subtract one from a variable, we can say: count = count - 1 ; • Programs often contain statements that decrement variables, so to save on typing, C provides these shortcuts: count-- ; OR --count ; Both do the same thing. They change the value of count by subtracting one from it. L 13 12

Post-Decrement Operator • The position of the -- determines when the value is decremented.

Post-Decrement Operator • The position of the -- determines when the value is decremented. If the -- is after the variable, then the decrementing is done last (a postdecrement). int amount, count ; count = 3 ; amount = 2 * count-- ; • amount gets the value of 2 * 3, which is 6, and then 1 gets subtracted from count. • So, after executing the last line, amount is 6 and count is 2. L 13 13

Pre-Decrement Operator • If the -- is before the variable, then the decrementing is

Pre-Decrement Operator • If the -- is before the variable, then the decrementing is done first (a predecrement). int amount, count ; count = 3 ; amount = 2 * --count ; • 1 gets subtracted from count first, then amount gets the value of 2 * 2, which is 4. • So, after executing the last line, amount is 4 and count is 2. L 13 14

A Hand Trace Example int answer, value = 4 ; Code Value Answer 4

A Hand Trace Example int answer, value = 4 ; Code Value Answer 4 garbage value = value + 1 ; value++ ; ++value ; answer = 2 * value++ ; answer = ++value / 2 ; value - - ; - -value ; answer = - -value * 2 ; answer = value - - / 3 ; L 13 15

Practice Given int a = 1, b = 2, c = 3 ; What

Practice Given int a = 1, b = 2, c = 3 ; What is the value of this expression? ++a * b - c - What are the new values of a, b, and c? L 13 16

More Practice Given int a = 1, b = 2, c = 3, d

More Practice Given int a = 1, b = 2, c = 3, d = 4 ; What is the value of this expression? ++b / c + a * d++ What are the new values of a, b, c, and d? L 13 17

Assignment Operators = += -= Statement a=a+2; a=a-3; a=a*2; a=a/4; a=a%2; b=b+(c+2); d=d*(e-5); L

Assignment Operators = += -= Statement a=a+2; a=a-3; a=a*2; a=a/4; a=a%2; b=b+(c+2); d=d*(e-5); L 13 *= /= %= Equivalent Statement a += 2 ; a -= 3 ; a *= 2 ; a /= 4 ; a %= 2 ; b += c + 2 ; d *= e - 5 ; 18

Practice with Assignment Operators int i = 1, j = 2, k = 3,

Practice with Assignment Operators int i = 1, j = 2, k = 3, m = 4 ; Expression i += j + k Value j *= k = m + 5 k -= m /= j * 2 L 13 19

Code Example Using /= and ++ Counting the Digits in an Integer #include <stdio.

Code Example Using /= and ++ Counting the Digits in an Integer #include <stdio. h> int main ( ) { int num, temp, digits = 0 ; temp = num = 4327 ; while ( temp > 0 ) { printf (“ %d n ”, temp) ; temp /= 10 ; digits++ ; } printf (“There are %d digits in %d. n”, digits, num) ; return 0 ; } L 13 20

Output of the code example: num 4327 digits 0 1 2 3 4 There

Output of the code example: num 4327 digits 0 1 2 3 4 There are 4 printf 4327 432 43 4 digits in temp 4327 432 43 4 0 4327 “Boxes Indicate Printed Output” L 13 21

Counter Controlled Repetition requires: 1) The name of a control variable (loop counter) 2)

Counter Controlled Repetition requires: 1) The name of a control variable (loop counter) 2) The initial value of the control variable. 3) The increment (or decrement) by which the control variable is modified each time through the loop 4) The condition that tests for the final value of the control variable. continue? L 13 22

Debugging Tips • Trace your code by hand (a hand trace), keeping track of

Debugging Tips • Trace your code by hand (a hand trace), keeping track of the value of each variable. • Insert temporary printf() statements so you can see what your program is doing. o o L 13 Confirm that the correct value(s) has been read in. Check the results of arithmetic computations immediately after they are performed. 23

Header Files • Header files contain function prototypes for all of the functions found

Header Files • Header files contain function prototypes for all of the functions found in the corresponding library • It also contains definitions of constants and data types used in that library L 13 24

Commonly Used Header Files header file <stdio. h> <math. h> <stdlib. h> <time. h>

Commonly Used Header Files header file <stdio. h> <math. h> <stdlib. h> <time. h> <ctype. h> others L 13 Contains function prototypes for the standard input/output library functions & information used by them the math library functions the conversion of number to text, text to number, memory allocation, random numbers and other utility functions manipulating time and date functions that test characters for certain properties and that can convert case see page 154 of text 25

Math Library • • • L 13 double sqrt (double x); o returns the

Math Library • • • L 13 double sqrt (double x); o returns the square root of x double pow (double x, double y) o x raised to the y power • pow (3. 0, 2. 0) is 9. 0 • pow (8. 0, 1. 0 / 3) is 2. 0 double sin (double x) o trigonometric sine of x (x in radians) All math library functions take doubles as arguments and return doubles others on page 145 - 146 of text 26

Common stdlib functions • • • L 13 void exit (int x); o prematurely

Common stdlib functions • • • L 13 void exit (int x); o prematurely ends program execution void srand (unsigned int x); o “seeds” the random number generator with an unsigned integer that is used to start the calculations that generate the pseudorandom number • srand (200); int rand (void); o returns an unsigned pseudo-random integer in the range of 0 to 65535 or 0 to 4294967295 depending on the size of an integer on the system your on o num = rand( ); 27

Manipulating what rand() returns • Since rand( ) returns unsigned integers in a large

Manipulating what rand() returns • Since rand( ) returns unsigned integers in a large range, we often have to manipulate the return value to suit our purposes • Suppose we want only random numbers in the range from 0 to 5 o num = rand ( ) % 6 • How about 1 to 6? o num = 1 + rand( ) % 6; • How about 5 to 20? o num = 5 + rand ( ) % 16; L 13 28

srand ( ) and rand ( ) • The pseudo-random number generator needs an

srand ( ) and rand ( ) • The pseudo-random number generator needs an unsigned int as it’s seed • Although it produces what appear to be random numbers, if we use the same seed, we get the same sequence of random numbers • To get different random numbers each time we run our program, we have to give a different seed each time L 13 29

srand ( ) and rand ( ) #include <stdio. h> #include <stdlib. h> #define

srand ( ) and rand ( ) #include <stdio. h> #include <stdlib. h> #define SEED 67 main ( ) { int i, num; srand (SEED); for (i = 0; i < 5; i++) { num = rand ( ); num = 1 + num % 6; printf (“%dn”, num); } } L 13 Since we are always using the value 67 to seed the generator, the same numbers will be produced whenever we run our program. 30

<time. h> • One of the most useful functions in the time library is

<time. h> • One of the most useful functions in the time library is the time( ) function • It returns the time of day as seconds • Since this number is different every time we call it, a common use is as a seed for the random number generator • Each time we run our program, a different sequence of random numbers will be produced • srand (time ( NULL) ) ; L 13 31