All pastes #987827 Raw Edit

Mine

public text v1 · immutable
#987827 ·published 2008-04-17 00:19 UTC
rendered paste body
#include <iostream.h>
 
int main()
{
int X1 = 1;
double X2 = 2.50;
 
cout << (X1 + X2) << endl;
X2+= 50;
 
cout <<  (int)X2 << endl;
cout << X2 << endl;
return(0);
}
 
---------------------------------------------------------------------------------------------------------------------------------
 
This is similar to the example we did earlier, however there’s some tricks here which will help you much throughout programming. What we are doing different here is first we are giving initial values to our variables as you can see X1 =1; when we declare it that means its starting/initial value will be 1 instead of 0 or NULL which is what it is if you don’t give it a initial value. And instead of just working with int’s multiplying by them self, we are adding a int to a double and displaying it, these different variable types can be mixed and shown as one output. The first line of the program would display 3.5 because 1+2.5 = 3.5, this shows how an integer can be added with a double and be shown as the full calculated number. Next we already know that X2 = 2.50, but what is this X2+=50, all that is, is a fancy way of telling the computer to add 50 to X2’s currently existing value which is 2.5. So after that line of code X2 now equals 52.50. Here is a great example of type casting, on the next line we output (int)X2, what is really going on here is that we already know that X2 is a double however when we put a type such as (int) in brackets next to it, it tells the program that were not allowed to show X2 as a double, it temporarily converts it to and integer/int which will display only its whole number which is 55, so the .50 gets cut off. Now on the next line if we display X2 without type casting it to a integer it will display 55.50 which is a double which was its original type.