1 / 17

Assignment Statement

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;

Download Presentation

Assignment Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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;

  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

  3. ? 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

  4. ? 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

  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

  6. 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”; }

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. Multiple Assignment A = B = C = D = 12;

More Related