Assignment Statement

17
ASSIGNMENT STATEM ENTS

description

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; - PowerPoint PPT Presentation

Transcript of Assignment Statement

Page 1: Assignment Statement

ASSIGNMENT STATEMENTS

Page 2: Assignment Statement

Assignment StatementAssignment Statement Syntax is:Syntax is:

variable identifiervariable identifier = = valuevalue ;; An assignment statement is An assignment statement is used to assign a value to used to assign a value to

a variable;a variable;

The value on the right hand side of the assignment The value on the right hand side of the assignment operator can be a:operator can be a: a constanta constant (2, -3.5,4.0e8); (2, -3.5,4.0e8); a symbolic constant;a symbolic constant;

for example for example x = Pi;x = Pi; (where Pi is a symbolic (where Pi is a symbolic

constant declared earlier);constant declared earlier); TheThe value of another variable value of another variable

for example for example x = y;x = y; The The result of an expressionresult of an expression

for example for example k = (x+y)/2;k = (x+y)/2;

Page 3: Assignment Statement

C++ always assigns the value in the right hand side of the C++ always assigns the value in the right hand side of the equal sign to the variable whose identifier is in the left hand equal sign to the variable whose identifier is in the left hand side;side;

If the value in the right hand side is an expression, C++ If the value in the right hand side is an expression, C++ evaluates it first then assigns its result;evaluates it first then assigns its result;

Consider the Example below:Consider the Example below:int main()int main(){{

float area;float area; float radius;float radius; const double Pi = 3.14159;const double Pi = 3.14159;

area = radius * radius * Pi;area = radius * radius * Pi; cout <<“Area equals “<<areacout <<“Area equals “<<area <<“ Perimeter equals “<<perimeter;<<“ Perimeter equals “<<perimeter;}}

An assignment statement. The variable An assignment statement. The variable areaarea will be assigned the result of the will be assigned the result of the expression on the right hand side of the expression on the right hand side of the equal signequal sign

An assignment statement. The variable An assignment statement. The variable areaarea will be assigned the result of the will be assigned the result of the expression on the right hand side of the expression on the right hand side of the equal signequal sign

Assignment StatementAssignment Statement

Page 4: Assignment Statement

number

?

Assignment Statements

5

The value in number is number_

# include <iostream.h># include <iostream.h>

void main ()void main (){ int number;{ int number;

number = 5;number = 5; cout << “The value in number is ” cout << “The value in number is ” << “number” << endl; << “number” << endl;}}

Page 5: Assignment Statement

number

?

Assignment Statements

5

The value in number is 5_# include <iostream.h># include <iostream.h>

void main() void main() { int number;{ int number;

number = 5;number = 5; cout << “The value in number is ” cout << “The value in number is ” << number << endl; << number << endl; }}

Page 6: Assignment Statement

Checking

?

Multiple Assignment Statements

-20

Days

??

Miles

?4276

187000

# include <iostream.h># include <iostream.h>

void main ()void main (){ int Checking;{ int Checking; unsigned int Miles;unsigned int Miles; long Days;long Days; Checking = -20;Checking = -20; Miles = 4276;Miles = 4276; Days = 187000;Days = 187000;

Page 7: Assignment Statement

Days

187000

Checking

-20

Miles

4276

cout << “We have made a long trip of ”cout << “We have made a long trip of ” << Miles << “ miles.\n”;<< Miles << “ miles.\n”; cout << “Our checking account balance is ”cout << “Our checking account balance is ” << Checking;<< Checking; cout << “\nExactly ” << Days << “ days”cout << “\nExactly ” << Days << “ days” << “ ago Columbus stood”<< “ ago Columbus stood” << “ on this spot.\n”; << “ on this spot.\n”; }}

Page 8: Assignment Statement

Anatomy of a C++ programAnatomy of a C++ program

Variables in ActionVariables in ActionLets look at execution Lets look at execution of the program belowof the program below

void main()void main(){{

int x; int x; int y;int y; float k;float k;

y=5;y=5;x=4;x=4;

k=(x+y)/2;k=(x+y)/2; cout <<“k equals “<<k;cout <<“k equals “<<k;}}

Main Memory

Screen

Executable Program

Page 9: Assignment Statement

•C++ compiler Allocates 2 bytes of Main Memory for variable of type C++ compiler Allocates 2 bytes of Main Memory for variable of type intint

•The program specifies that this variable should be given the name (identifier) The program specifies that this variable should be given the name (identifier) xx

•C++ compiler Allocates 2 bytes of Main Memory for variable of type C++ compiler Allocates 2 bytes of Main Memory for variable of type intint

•The program specifies that this variable should be given the name (identifier) The program specifies that this variable should be given the name (identifier) xx

Anatomy of a C++ program(cnt’d)Anatomy of a C++ program(cnt’d)

Variables in ActionVariables in ActionDuring compile timeDuring compile time

void main()void main(){{ int x;int x; int y;int y; float k;float k; y=5;y=5; x=4;x=4; k=(x+y)/2;k=(x+y)/2; cout <<“k equals cout <<“k equals “<<k;“<<k;}}

xxxx

Main Memory

Screen

Executable Program

Page 10: Assignment Statement

•C++ compiler Allocates 2 bytes of Main Memory for variable of type C++ compiler Allocates 2 bytes of Main Memory for variable of type intint

•The program specifies that the variable name is The program specifies that the variable name is yy

•C++ compiler Allocates 2 bytes of Main Memory for variable of type C++ compiler Allocates 2 bytes of Main Memory for variable of type intint

•The program specifies that the variable name is The program specifies that the variable name is yy

Anatomy of a C++ program(cnt’d)Anatomy of a C++ program(cnt’d)

xxxx

Main Memory

Screen

Executable Program

yyyy

Variables in ActionVariables in ActionDuring compile timeDuring compile time

void main()void main(){{ intint x; x; int y;int y; float k;float k; y=5;y=5; x=4;x=4; k=(x+y)/2;k=(x+y)/2; cout <<“k equals cout <<“k equals “<<k;“<<k; }}

Page 11: Assignment Statement

•C++ compiler Allocates 4 bytes of Main Memory for variable of type C++ compiler Allocates 4 bytes of Main Memory for variable of type floatfloat

•The program specifies that this variable should be given the name The program specifies that this variable should be given the name kk

•C++ compiler Allocates 4 bytes of Main Memory for variable of type C++ compiler Allocates 4 bytes of Main Memory for variable of type floatfloat

•The program specifies that this variable should be given the name The program specifies that this variable should be given the name kk

Anatomy of a C++ program(cnt’d)Anatomy of a C++ program(cnt’d)

xxxx

Main Memory

Screen

Executable Program

yyyy

kkkk

Variables in ActionVariables in ActionDuring compile timeDuring compile time

void main()void main(){{ intint x; x; int y;int y; float k;float k; y=5;y=5; x=4;x=4; k=(x+y)/2;k=(x+y)/2; cout <<“k equals cout <<“k equals “<<k;“<<k; }}

Page 12: Assignment Statement

•C++ runtime system stores the integer 5 (C++ runtime system stores the integer 5 (00000000000001010000000000000101))2’s complement2’s complement in locations allocated to in locations allocated to yy

•The old contents of the two bytes allocated to The old contents of the two bytes allocated to yy are overwritten are overwritten

•C++ runtime system stores the integer 5 (C++ runtime system stores the integer 5 (00000000000001010000000000000101))2’s complement2’s complement in locations allocated to in locations allocated to yy

•The old contents of the two bytes allocated to The old contents of the two bytes allocated to yy are overwritten are overwritten

Anatomy of a C++ program(cnt’d)Anatomy of a C++ program(cnt’d)

xxxx

Main Memory

Screen

Executable Program

yyyy

kkkk

5

Variables in ActionVariables in ActionDuring compile timeDuring compile time

void main()void main(){{ intint x; x; int y;int y; float k;float k; y=5;y=5; x=4;x=4; k=(x+y)/2;k=(x+y)/2; cout <<“k equals cout <<“k equals “<<k;“<<k;}}

Page 13: Assignment Statement

•C++ runtime system stores the integer 4 (C++ runtime system stores the integer 4 (00000000000001000000000000000100))2’s complement2’s complement

•The old contents of the two bytes allocated to The old contents of the two bytes allocated to x x are overwritten are overwritten

•C++ runtime system stores the integer 4 (C++ runtime system stores the integer 4 (00000000000001000000000000000100))2’s complement2’s complement

•The old contents of the two bytes allocated to The old contents of the two bytes allocated to x x are overwritten are overwritten

Anatomy of a C++ program(cnt’d)Anatomy of a C++ program(cnt’d)

xxxx

Main Memory

Screen

Executable Program

yyyy

kkkk

5

4

Variables in ActionVariables in ActionDuring compile timeDuring compile time

void main()void main(){{ intint x; x; int y;int y; float k;float k; y=5;y=5; x=4;x=4; k=(x+y)/2;k=(x+y)/2; cout <<“k equals cout <<“k equals “<<k;“<<k;}}

Page 14: Assignment Statement

•C++ runtime reads contents of bytes allocated to C++ runtime reads contents of bytes allocated to xx and and yy from Main Memory into registers in the CPU from Main Memory into registers in the CPU

•CPU adds values of CPU adds values of xx and and yy; the result is ; the result is 99

•The result is divided by 2 (integer division); the result is the quotient of the division. The result is The result is divided by 2 (integer division); the result is the quotient of the division. The result is 4.04.0

in floating point notation (in floating point notation (4 4 bytes; bytes; 1 bit for the sign, 7 bits for the exponent in excess notation, 24 bits for 1 bit for the sign, 7 bits for the exponent in excess notation, 24 bits for

mantissa)mantissa)

•Finally, the end result is written into the bytes allocated to the variable Finally, the end result is written into the bytes allocated to the variable kk

•C++ runtime reads contents of bytes allocated to C++ runtime reads contents of bytes allocated to xx and and yy from Main Memory into registers in the CPU from Main Memory into registers in the CPU

•CPU adds values of CPU adds values of xx and and yy; the result is ; the result is 99

•The result is divided by 2 (integer division); the result is the quotient of the division. The result is The result is divided by 2 (integer division); the result is the quotient of the division. The result is 4.04.0

in floating point notation (in floating point notation (4 4 bytes; bytes; 1 bit for the sign, 7 bits for the exponent in excess notation, 24 bits for 1 bit for the sign, 7 bits for the exponent in excess notation, 24 bits for

mantissa)mantissa)

•Finally, the end result is written into the bytes allocated to the variable Finally, the end result is written into the bytes allocated to the variable kk

Anatomy of a C++ program(cnt’d)Anatomy of a C++ program(cnt’d)

xxxx

Main Memory

Screen

Executable Program

yyyy

kkkk

5

4

4.0

Variables in ActionVariables in ActionDuring compile timeDuring compile time void main()void main(){{ intint x; x; int y;int y; float k;float k; y=5;y=5; x=4;x=4; k=(x+y)/2;k=(x+y)/2; cout <<“k equals cout <<“k equals “<<k;“<<k;}}

Page 15: Assignment Statement

•C++ runtime system sends the text “C++ runtime system sends the text “k equalsk equals” to the controller of the screen using memory-mapped I/O” to the controller of the screen using memory-mapped I/O

•C++ runtime then reads the value of C++ runtime then reads the value of kk from Main Memory and send that value to the screen in the same from Main Memory and send that value to the screen in the same wayway

•C++ runtime system sends the text “C++ runtime system sends the text “k equalsk equals” to the controller of the screen using memory-mapped I/O” to the controller of the screen using memory-mapped I/O

•C++ runtime then reads the value of C++ runtime then reads the value of kk from Main Memory and send that value to the screen in the same from Main Memory and send that value to the screen in the same wayway

Anatomy of a C++ program(cnt’d)Anatomy of a C++ program(cnt’d)

xxxx

Main Memory

Screen

Executable Program

yyyy

kkkk

K equals 4K equals 4

Variables in ActionVariables in ActionDuring compile timeDuring compile time

void main()void main(){{ intint x; x; int y;int y; float k;float k; y=5;y=5; x=4;x=4; k=(x+y)/2;k=(x+y)/2; cout <<“k equals cout <<“k equals “<<k;“<<k;}}

5

4

4.0

Page 16: Assignment Statement

•The function main() terminates, and accordingly the whole program terminatesThe function main() terminates, and accordingly the whole program terminates•The function main() terminates, and accordingly the whole program terminatesThe function main() terminates, and accordingly the whole program terminates

Anatomy of a C++ program(cnt’d)Anatomy of a C++ program(cnt’d)

xxxx

Main Memory

Screen

Executable Program

yyyy

kkkk

K equals 4K equals 4

Variables in ActionVariables in ActionDuring compile timeDuring compile time

void main()void main(){{ intint x; x; int y;int y; float k;float k; y=5;y=5; x=4;x=4; k=(x+y)/2;k=(x+y)/2; cout <<“k equals cout <<“k equals “<<k;“<<k; }}

5

4

4.0

Page 17: Assignment Statement

Multiple Assignment

A = B = C = D = 12;