Presentation is loading. Please wait.

Presentation is loading. Please wait.

CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.

Similar presentations


Presentation on theme: "CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable."— Presentation transcript:

1 CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable name = a value or an expression; –Declaration must be done before assigning a value to a variable !!! Examples int a; a = 3;  int a = 3;  int a = 3.56; double f = 3560.1;  double f = 3.561e3; char c = ’a’;  char c = 97; int a, b, c = 5; int a=b=c=5;  int a=5, b=5, c=5; x = y + z + 2.0; x = x + 1;

2 CP104 Introduction to Programming Overview of C Lecture 4__ 2 Examples int x = 5; x = x + 3; What the value in x after executing the above two statements? Data type conversion int z; double y = 10.11; z = y; /* or x = (int) y; */

3 CP104 Introduction to Programming Overview of C Lecture 4__ 3 Testing example #include main() { int a = 3.5, b; double f = 3.5601e3; char c = 97; b = a + 3.0; printf("%d\n", a); printf("%f\n", f); printf("%c\n", c); printf("%f\n", b); fflush(stdin); getchar(); } What are the output of the above program ?

4 CP104 Introduction to Programming Overview of C Lecture 4__ 4 Arithmetic Operations +, -, *, / % For +, -, *, /, if both operands have the same type, the result has the same type. If one operand is float, the result will be float Addition “+” : e.g. 5 + 7, 5.0 + 7 Subtraction “–” : e.g. 7 – 2, 5 – 7, 7.0-3.0 Multiplication “ * ” : e.g. 3 * 5 = ?, 3.0*5 = ? Division “ / ” : e.g. 4 / 2 = ? 7/2 = ? 7/2.0 = ? 7/2+0.5 = ? Modulus “%” (remainder of a division) –both operands must be integers or type int e.g. 7 % 3 = 1 15 % 10= ? 10 % 15= ? 4 % 3= ? 7 % 3.0 = ?

5 CP104 Introduction to Programming Overview of C Lecture 4__ 5 More examples 3.5 + 5 / 2=? 3.0 * 5 / 2=? 4 / 5 / 2=? int m = 15, n = 10; double x; x = m / n; x = ?

6 CP104 Introduction to Programming Overview of C Lecture 4__ 6 Arithmetic Expressions An expression consists of variables, values, and special symbols +, -, *, /, % (reminder), (, ) in a mathematical style –Example x*y*c + (x+c*d) Precedence of Arithmetic Operators –( ) -Parentheses done first, inside-out –Unary - e.g. -x –*, /, % –+, - –Operators at the same precedence level are computed LEFT to RIGHT (except unary operation)

7 CP104 Introduction to Programming Overview of C Lecture 4__ 7 Arithmetic Expressions (con’d) The data type of an expression –The data type of an expression is determined by its operands If all variable and data have the same data type, then the expression has the same data type –Mixed-type is one with different data types. If there is a double data type, then the expression is a double data type Mixed-type assignment –Type cast converts an expression to a different data type by writing the desired type in parenthesis Evaluation can be down by an evaluation tree

8 CP104 Introduction to Programming Overview of C Lecture 4__ 8 Arithmetic Expressions (con’d) Examples: int x=5, y=3, z=4; double d=0.5; What are the types and values of x+y*z, x*y + x/z*d, x/z*(int)d ?

9 CP104 Introduction to Programming Overview of C Lecture 4__ 9 Evaluation Tree for area = PI * radius * radius;

10 CP104 Introduction to Programming Overview of C Lecture 4__ 10 Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1);

11 CP104 Introduction to Programming Overview of C Lecture 4__ 11 Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y

12 CP104 Introduction to Programming Overview of C Lecture 4__ 12 Simplified Assignment Expressions (see demo) a +=3;  a = a+3; x *= y+3  x = x*(y+3); m = i++;  m = i; i = i+1;  m = i; i += 1; m = ++i;  i=i+1; m = i;  i+=1; m = i; n = i--;  n = i; i = i-1;  n = i; i -= 1; n = --i;  i-=1; n = i;


Download ppt "CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable."

Similar presentations


Ads by Google