Data Types:
Variables are used in virtually every program. Just like in math a
variable is a letter that is used to hold a value. Variables are used to hold
many different data types that must be declared before the program is run. Here
is how to declare a variable called x that will hold an integer.
int x;
Just like integer suggests, 'x' can only hold whole value numbers
and not decimals. Now that we have this integer we can use it in all sorts of
ways but first we must assign it a value, if we don't assign 'x' a value what
value will it hold? It's definitely not zero, right now there is what we call
garbage inside the variable. Garbage is when a variable is holding an insane
value that is useless. In order to fix this we must assign x to a value here’s
how.
x=12;
So now for the rest of the program unless we say otherwise
whenever we use the variable x it will be just like using the number 12. Here
is how to print something to the screen, in this case x
cout<<x;
If you are familiar with the C language you will notice that this
is completely different there is no printf and also no place holder but have no
fear it will catch on quick. The c in cout stands for console
therefore cout is short for console out! So what if you did not want to assign
a variable in your program but would rather have a variable assigned a value
while the program is running? This can be accomplished by using cin, yep you guessed
it, it's short for console in.
int x;
cin>>x;
//Asks the user to input a value for x
First notice the arrows are now pointing in the opposite direction
than in cout. Alright so here we initialized an integer called x but did not
assign it a value. The user of the program will be prompted to enter a number
and that number will then be stored in the variable x. Now let us check out a
full working program.
Copy paste this code into your coding interface:
#include
<iostream>
using
namespace std;
int main()
{
int x;
cout<<"Please enter an integer"<<endl;
cin>>x; //the user will enter a value
that will be stored in 'x'
cout<<"Your number is ";
cout<<x; //the program will print the
value of 'x' to the screen
return 0;
}
What this program does is first declare an integer called 'x' and
then ask you to enter a value. Once a number is entered in, the screen will
display the number that was just entered. If someone wanted to manipulate a
variable through multiplication, division, etc all you would have to is declare
a few more variables and come up with some kind of expression. Lets add a few
more lines to the previous program.
#include
<iostream>
using
namespace std;
int main()
{
int x,y,z;
cout<<"Please enter an
integer"<<endl;
cin>>x;
cout<<"Please enter an
integer"<<endl;
cin>>y;
z=x/y; //the value of 'z' equals 'x'
divided by 'y'
cout<<"Your number is ";
cout<<z;
return 0;
}
This program declares three integers and then equals one of the
variable 'z' to the division of 'x' and 'y'. Once the user has assigned a value
to 'x' and 'y' the program will display the division that is held in the
variable 'z'. This program will work fine but a flaw does arise when 'z' is
equal to the divsion of 'x' and 'y'. If the user assigned 'x' to be 4 and 'y'
to be 2 that would be ok because 2 goes into 4 evenly but what about 6 divided
by 4? If we were to leave the program the way it is it would return an answer
of 0, this is because as of right now x,y,and z can only hold integers and not numbers
with decimals, 6 divided by 4 is obviously a decimal so what's the fix? Simple,
instead of declaring 'z' an integer that can't hold decimal values we declare
it a double. Unlike integers, double data types can hold numbers with decimals,
the new program will look like this.
#include
<iostream>
using
namespace std;
int main()
{
int x,y;
double z;
cout<<"Please enter an
integer"<<endl;
cin>>x;
cout<<"Please enter an
integer"<<endl;
cin>>y;
z=x/y;
cout<<"Your number is ";
cout<<z;
return 0;
}
This program looks better but not so fast!! There is still
something wrong with this program if you were to run it right now you wouldn't
get the right answer. This is because both 'x' and 'y' are integers and you
can't divide two integers that will result in a decimal and get the right
answer this is something called truncation. In order to fix this atleast one
variable either 'x' or 'y' must be of the double type. By doing this the
compiler will recognize this and temporarily convert the other integer to a
double, of course you could always just declare every variable to be of the
double type but I will leave that up to you. Here is the full working program.
#include
<iostream>
using
namespace std;
int main()
{
int x;
double y,z;
cin>>x;
cout<<"Please enter an
integer"<<endl;
cin>>y;
z=x/y;
cout<<"Your number is ";
cout<<z;
return 0;
}
Before the next lesson there is one more thing I would like to
point out. Notice how 'z' was assigned after 'x' and 'y' were assigned values
and not before like this.
#include
<iostream>
using
namespace std;
int main()
{
int x;
double y,z;
z=x/y;//This is not right!!
cout<<"Please enter an
integer"<<endl;
cin>>x;
cout<<"Please enter an
integer"<<endl;
cin>>y;
cout<<"Your number is ";
cout<'<z;
return 0;
}
This program is a small example of what would be called a bug. No
error would be caught by the compiler and would run but just not correctly.
Remember before a variable is assigned a value it still has a number stored in
it and that's what is considered garbage. If we assign 'z' to the division of
'x' and 'y' before 'x' and 'y' are assigned actual values then we have just
assigned 'z' to the garbage of 'x' divided by the garbage of 'y'. Keep this in
mind for future programs
0 comments:
Post a Comment