The next statement z = x + y; evaluates the expression on right hand side. It takes values stored in variables x and y (which are 5 and 10 respectively), adds them and by using the assignment operator (=), puts the value of the result, which is 15 in this case, to the memory location labeled as z.

 

Here a thing to be noted is that the values of x and y remains the same after this operation. In arithmetic operations the values of variables used in expression on the right hand side are not affected. They remain the same. But a statement like x = x + 1; is an exceptional case. In this case the value of x is changed.

 

The next line cout << “ x = “ ; is simple. It just displays ‘ x = ‘ on the screen.

 

Now we want to display the value of x after ‘x =’. For this we write the statement cout << x ;

Here comes the affect of data type on cout. The previous statement cout << “x = “ ; has a character string after << sign and cout simply displays the string. In the statement cout << x; there is a variable name x. Now cout will not display ‘x’ but the value of x. The cout interprets that x is a variable of integer type, it goes to the location x in the memory and takes its value and displays it in integer form, on the screen. The next line cout << ”y =”; displays ‘ y = ‘ on the screen. And line cout << y; displays the value of y on the screen. Thus we see that when we write something in quotation marks it is displayed as it is but when we use a variable name it displays the value of the variable not name of the variable. The next two lines cout << “z = x + y = ”; and cout << z; are written to display ‘z = x + y = ’ and the value of z that is 15.

Now when we execute the program after compiling, we get the following output

 

x = 5 y = 10 z = x + y = 15

 

 

 

Previous

 

 

TOC

 

 

Next