170 likes | 450 Views
Assignment Statement. Syntax is: variable identifier = value ; An assignment statement is used to assign a value to a variable; The value on the right hand side of the assignment operator can be a: a constant (2, -3.5,4.0e8); a symbolic constant;
E N D
Assignment Statement • Syntax is: • variable identifier = value; • An assignment statement is used to assign a value to a variable; • The value on the right hand side of the assignment operator can be a: • a constant (2, -3.5,4.0e8); • a symbolic constant; • for example x = Pi; (where Pi is a symbolic • constant declared earlier); • The value of another variable • for example x = y; • The result of an expression for example k = (x+y)/2;
Assignment Statement • C++ always assigns the value in the right hand side of the equal sign to the variable whose identifier is in the left hand side; • If the value in the right hand side is an expression, C++ evaluates it first then assigns its result; • Consider the Example below: • int main() • { • float area; • float radius; • const double Pi = 3.14159; • area = radius * radius * Pi; • cout <<“Area equals “<<area • <<“ Perimeter equals “<<perimeter; • } An assignment statement. The variable area will be assigned the result of the expression on the right hand side of the equal sign
? number Assignment Statements The value in number is number _ # include <iostream.h> void main () { int number; number = 5; cout << “The value in number is ” << “number” << endl; } 5
? number Assignment Statements The value in number is 5 _ # include <iostream.h> void main() { int number; number = 5; cout << “The value in number is ” << number << endl; } 5
? ? Days ? ? Checking Miles Multiple Assignment Statements # include <iostream.h> void main () { int Checking; unsigned int Miles; long Days; Checking = -20; Miles = 4276; Days = 187000; 187000 -20 4276
187000 -20 4276 Days Checking Miles cout << “We have made a long trip of ” << Miles << “ miles.\n”; cout << “Our checking account balance is ” << Checking; cout << “\nExactly ” << Days << “ days” << “ ago Columbus stood” << “ on this spot.\n”; }
Anatomy of a C++ program • Variables in Action • Lets look at execution • of the program below • void main() • { • int x; • int y; • float k; • y=5; • x=4; • k=(x+y)/2; • cout <<“k equals “<<k; • } Main Memory Executable Program Screen
x Anatomy of a C++ program(cnt’d) • Variables in Action • During compile time • void main() • { • int x; • int y; • float k; • y=5; • x=4; • k=(x+y)/2; • cout <<“k equals “<<k; • } Main Memory Executable Program Screen • C++ compiler Allocates 2 bytes of Main Memory for variable of type int • The program specifies that this variable should be given the name (identifier) x
y Anatomy of a C++ program(cnt’d) • Variables in Action • During compile time • void main() • { • int x; • int y; • float k; • y=5; • x=4; • k=(x+y)/2; • cout <<“k equals “<<k; • } Main Memory x Executable Program Screen • C++ compiler Allocates 2 bytes of Main Memory for variable of type int • The program specifies that the variable name is y
y k Anatomy of a C++ program(cnt’d) • Variables in Action • During compile time • void main() • { • int x; • int y; • float k; • y=5; • x=4; • k=(x+y)/2; • cout <<“k equals “<<k; • } Main Memory x Executable Program Screen • C++ compiler Allocates 4 bytes of Main Memory for variable of type float • The program specifies that this variable should be given the name k
y k Anatomy of a C++ program(cnt’d) • Variables in Action • During compile time • void main() • { • int x; • int y; • float k; • y=5; • x=4; • k=(x+y)/2; • cout <<“k equals “<<k; • } Main Memory 5 x Executable Program Screen • C++ runtime system stores the integer 5 (0000000000000101)2’s complement in locations allocated to y • The old contents of the two bytes allocated to y are overwritten
y k Anatomy of a C++ program(cnt’d) • Variables in Action • During compile time • void main() • { • int x; • int y; • float k; • y=5; • x=4; • k=(x+y)/2; • cout <<“k equals “<<k; • } Main Memory 4 5 x Executable Program Screen • C++ runtime system stores the integer 4 (0000000000000100)2’s complement • The old contents of the two bytes allocated to x are overwritten
y k Anatomy of a C++ program(cnt’d) • Variables in Action • During compile timevoid main() • { • int x; • int y; • float k; • y=5; • x=4; • k=(x+y)/2; • cout <<“k equals “<<k; • } Main Memory 5 4 x 4.0 Executable Program Screen • C++ runtime reads contents of bytes allocated to x and y from Main Memory into registers in the CPU • CPU adds values of x and y; the result is 9 • The result is divided by 2 (integer division); the result is the quotient of the division. The result is 4.0 • in floating point notation (4 bytes; 1 bit for the sign, 7 bits for the exponent in excess notation, 24 bits for • mantissa) • Finally, the end result is written into the bytes allocated to the variable k
y k Anatomy of a C++ program(cnt’d) • Variables in Action • During compile time • void main() • { • int x; • int y; • float k; • y=5; • x=4; • k=(x+y)/2; • cout <<“k equals “<<k; • } Main Memory 5 4 x 4.0 Executable Program Screen K equals 4 • C++ runtime system sends the text “k equals” to the controller of the screen using memory-mapped I/O • C++ runtime then reads the value of k from Main Memory and send that value to the screen in the same way
y k Anatomy of a C++ program(cnt’d) • Variables in Action • During compile time • void main() • { • int x; • int y; • float k; • y=5; • x=4; • k=(x+y)/2; • cout <<“k equals “<<k; • } Main Memory 5 4 x 4.0 Executable Program Screen K equals 4 • The function main() terminates, and accordingly the whole program terminates
Multiple Assignment A = B = C = D = 12;