Create Presentation
Download Presentation

Download

Download Presentation

Expressions So far, we have seen fairly simple expressions. For example, x = 4; z = z - y; if(x%4 == 0) However, expr

136 Views
Download Presentation

Download Presentation
## Expressions So far, we have seen fairly simple expressions. For example, x = 4; z = z - y; if(x%4 == 0) However, expr

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Expressions**So far, we have seen fairly simple expressions. For example, x = 4; z = z - y; if(x%4 == 0) However, expressions can be much more complex.**Expressions – Complex**Here is an example of a complex expression being used in a for loop. From page 41 of The C Programming Language, 2nd. Ed. by Kernighan and Ritchie, for(i=0; i<lim-1 && (c=getchar()) != ’\n’ && c != EOF; ++i) s[i] = c; We can see that the expressions for initializing and incrementing the counter are simple. The complexity occurs in the middle expression for testing whether to continue the loop.**Expressions**Sometimes a complex expression is less cumbersome than the equivalent using only simple expressions. if (a && b && c && d && e) x = 5; does the same thing as if (a) if (b) if (c) if (d) if (e) x = 5;**Expressions – Examples**To use more complex expressions, we often must either • have knowledge of the order of precedence of the operators • control the order of precedence using parentheses Recall the example from Kernighan and Ritchie for determining if a year is a leap year: if( year%4 == 0 && year%100 != 0 || year%400 == 0 )**Increment/Decrement, Part 2**We saw how to use the increment (++) and decrement (- -) operators to increase or decrease a variable’s value by 1, e.g., sum++. We can place the double plus or minus signs before the variable (prefix) or after the variable (postfix). Depending on where a variable is being used when incremented/decremented, this can make a difference.**Increment/Decrement, Part 2**The increment and decrement operators are unary operators, so they require one argument. We can use them to modify a variable’s value when that variable is part of a larger expression. Examples: x++; /* makes no difference */ ++x; /* makes no difference */ sum = sum + y++; /* y is added to sum, then y is incremented */ sum = sum + ++y; /* y is incremented, then the new value of y is added to sum */**Increment/Decrement, Part 2**Here we see equivalent methods for using a variable before incrementing it: x = 3 + y++; x = 3 + y; y++; Here we see equivalent methods for incrementing a variable before using it: x = 3 + ++y; y++; x = 3 + y;**Increment/Decrement, Part 2**This code int x = 3; printf("line 1: x is %d\n", x); printf("line 2: x is %d\n", x++); /* increment x after using */ printf("line 3: x is %d\n", x); printf("line 4: x is %d\n", ++x); /* increment x before using */ printf("line 5: x is %d\n", x); produces line 1: x is 3 line 2: x is 3 line 3: x is 4 line 4: x is 5 line 5: x is 5**Incrementing in for Loops**int i; for(i = 0; i <= 5; i++) /* increment as postfix */ printf("%d ", i); for(i = 0; i <= 5; ++i) /* increment as postfix */ printf("%d ", i); for(i = 5; i >= 0; i--) /* increment as postfix */ printf("%d ", i); for(i = 5; i >= 0; --i) /* increment as postfix */ printf("%d ", i); produces 0 1 2 3 4 5 0 1 2 3 4 5 5 4 3 2 1 0 5 4 3 2 1 0**Incrementing in for Loops**Why doesn’t it matter in a for loop if we use the postfix or prefix versions? Remember that for a for loop we can create an equivalent while loop. int i, x = 0; for(i = 0; i < 5; i++) x = x + i; is equivalent to int i, x = 0; i = 0; while(i < 5) { x = x + i; i++; }**Functions in Expressions**When we call a function that returns a value, that value is used in the expression. y = 4; x = x + doubleNum(y); /* assume doubleNum returns 8 */ This is equivalent to y = 4; x = x + 8; This means we can use functions in expressions just as we would use other values.**Functions in Expressions**Here we use a function in the typical way. #include <stdio.h> int squareNum(int num) { return num*num; } int main(void) { int x = 1; int sq = squareNum(x); while( sq <=100 ) { printf("The square of %2d is %3d\n", x, sq); x++; sq = squareNum(x); } }**Functions in Expressions**This program has the same functionality but includes the function in the test of the while loop. #include <stdio.h> int squareNum(int num) { return num*num; } int main(void) { int x = 1, sq; while( (sq = squareNum(x)) <= 100 ) { printf("The square of %2d is %3d\n", x, sq); x++; } }**Functions in Expressions**#include <stdio.h> int doubleNum(int num) { return 2*num; } int main(void) { int x = 8, output; output = doubleNum( doubleNum(x) ); printf("output is %d\n", output); /* output is 32 */ } To evaluate, we must work our way inside much the way we do when looking at mathematical expressions with many parentheses, e.g., (((x − 3) − 2x2) + 8)