Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2004-2006 Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.

Similar presentations


Presentation on theme: "Copyright 2004-2006 Curt Hill The Assignment Operator and Statement The Most Common Statement you will use."— Presentation transcript:

1 Copyright 2004-2006 Curt Hill The Assignment Operator and Statement The Most Common Statement you will use

2 Copyright 2004-2006 Curt Hill Changing variables What can change in a variable? –Only the value –The type and name are set only at compile time What statements can change the value? –Declaration might or might not initialize –An assignment –Some types of method calls –Input statements are a type of method call

3 Copyright 2004-2006 Curt Hill Assignment Statement Simple form: variable = expression ; Simple meaning: the value computed from the expression is stored in the variable Similar to the assignment in very many languages

4 Copyright 2004-2006 Curt Hill What is an expression? A legal combination of the following: Constants Variables Operators The legal combinations are usually intuitive

5 Copyright 2004-2006 Curt Hill Legal Examples Suppose the following declarations: int a,b,c; double x,y,z; The following are legal: a = 5; // constant x = y; // variable y = 2*z – x/y; // expression

6 Copyright 2004-2006 Curt Hill General rules Item on left must be a variable The two sides should be of the same type with a few exceptions –A weaker type may be converted to a stronger type –A float is weaker than a double –A double is stronger than an int The right hand side items are not changed The old value of the left hand side is lost when a new value is stored

7 Copyright 2004-2006 Curt Hill More Examples Suppose the following declarations: int a,b,c; double x,y,z; The following are not legal: 5 = -c; // cannot change x = y // no semicolon x = y + * z; // malformed expr The following are legal but with issues: x = a; // Stronger type a = 5.8; // loses precision x = false; // incompatible

8 Copyright 2004-2006 Curt Hill Expression Values What is the value of: 2 + 3*2 + 1 Two reasonable candidates: 11 = ((2+3) * 2) + 1 –Left to right evaluation –Some calculators do it this way 9 = 2 + (3*2) + 1 –The multiplication precedes addition –Algebra uses this way We must choose one of these

9 Copyright 2004-2006 Curt Hill Precedence Clearly we cannot have both 11 and 9 correct or we never know what a computation produces C++ like most (but not all) programming languages follows Algebra Multiplication precedes addition This gives rise to a precedence chart –Shows the level of importance for operators

10 Copyright 2004-2006 Curt Hill Precedence Chart Highest to lowest –( ) –- + (Unary sign change) –* / % –+ - (Binary arithmetic) There are several others which we will encounter soon enough

11 Copyright 2004-2006 Curt Hill Algebra and the Assignment There is often confusion about the assignment because it looks like an equation An equation is a true or false statement An assignment is a command to perform some action

12 Copyright 2004-2006 Curt Hill Equations and Assignments The equation: x = y + 5 –States that the value of x is five larger than y’s value –This may be true or false depending on the value of x and y –Usually our job is to find an x and y that makes it true – this is called a solution The assignment: x = y + 5; –Commands that the value of x is computed to be five plus y’s value

13 Copyright 2004-2006 Curt Hill Another Example The equation: x = x + 1 has no solution –It can never be true –No value may equal itself plus one The assignment: x = x + 1; increments x –It is a command and will be carried out

14 Copyright 2004-2006 Curt Hill Another Example Suppose: int a=2, b=5, c=-7; What happens to the variable values when: c = a*b - b%c+1; b = a-c/(1+a*2); is executed? The value of c becomes 6 and the value of b becomes 1

15 Copyright 2004-2006 Curt Hill How does this work? int a=2, b=5, c=-7; c = a*b - b%c+1; c = 2*5 – 5%-7+1 c = 10 – 5 +1 c = 6 b = a – c/(1+a*2); b = 2 – 6/(1+2*2) b = 2 – 6/(1+4) b = 2 – 6/5 b = 2 – 1 b = 1

16 Copyright 2004-2006 Curt Hill Precedence Chart Again Highest to lowest –( ) –- + (Unary – sign change) –* / % –+ - (Binary arithmetic) –=–= Yes, there are quite a few more!

17 Declarations Again Declarations are executable or non- executable? Yes, either A declaration may initialize the variable by following with an assignment Example: int a = 5; double d = 2, e, f=1e5; Copyright 2004-2006 Curt Hill

18 Initialization rules The expression following the declaration may be any expression –Any variables must be defined –Example: int a=2, b=3; … int c = a*b-2; Variables without the equal sign are not initialized Like an assignment, the types should be acceptable Copyright 2004-2006 Curt Hill


Download ppt "Copyright 2004-2006 Curt Hill The Assignment Operator and Statement The Most Common Statement you will use."

Similar presentations


Ads by Google