Alright no corrections no new stipulations lets get started
Lesson 3
OK MB I MAK PONG FURST
Alright so we've covered basic variables and what they are (remember the boxes) we used the integer type (int an_int and declared(or instantiated) an int. Now I failed to mention something else about the types, not only do they tell you how many boxes there are to the variable, it also gives instructions about how to put things together, now short and int are basically the same except their size, unfortunately char decides to be a little difficult, it reads the same way as an integer type(this includes both short and int) but char is specialized to be treated as a character, that is a letter.
There are two more groups of types, the void type, and the floating point types. The void type is used almost as a non-type, we aren't going to use this much when declaring variables, so for now you can forget about it. floating point type refers to decimal numbers, that is to say instead of 3(integer) you can have 3.2(floating point). Here are the three floating point types:
I want to also add a bit to the integer types: First, all integer types come in two varieties signed(includes negative numbers), and unsigned(does not include negative numbers), unless you put an unsigned in front of it an integer is considered a signed integer. Second there is another type of integer called a long, it is supposed to be at least as big as an int. here are all the integer types:
Ok so now we have a whole bunch of new variables to play with, and that is great but we still can't do much of anything interesting or dynamic, so let's fix that:
Well now doesn't look like much in code, but running it you should get something a bit more exciting than a string of numbers. The new bit here is the cin line:
cin >> an_int;
this is pretty simple, it takes input from in this case the keyboard, and puts it into an int, you can actually input to any of the types (excluding void) that I have mentioned.
Alright well this is all very interesting but we still haven't done anything useful, to do that we're going to have to work in some conditionals. What are conditionals ? well simply put they are going to test if something is true, in C++ something is true when it is not zero, it is false if it is zero. Now this isn't terribly helpful if we wanted to see if two variables are the same, so to help this out we have a set of operators(like =,+,-,etc.) that will allow us to do these kind of things, lets take a look at them:
Alright so now we know how to get some true falses, but that doesn't do much good without the conditionals! I bet you though I forgot about them, in any case we are going to start off with the most basic conditional, the if/else combinations, rather than me rattle on lets get to an example:
ok so we have a few new things to talk about first thing is the if block(fancy word for everything in the if statement and inside the curly brackets) basically what happens here is if decides if it executes based upon what the statement inside the parentheses evaluates to. In this case if an_int is actually equal to 5 then it evaluates to 1, and the code inside the curly braces({}) is executed. If(falling into if statements already) an_int is not equal to 5 then the left evaluates to 0, and instead of the if block being executed, the else block is. Alright time for us to make our partially useful application, for this we are going to write a calculator program. Now there are about a million ways to do this(and probably all of them are better than what we will settle on), for now we are going to keep it simple, to do that we have two restrictions:
Before we get started we have to talk a little bit about how cin works, and also how characters work. something you should always remember about cin is that no matter what you put on the right, what actually gets input is in a character format, it is just translated to the variable on the right for you, though in some cases it can't be translated in which case the character is left in a buffer. All input goes through this buffer if it can be translated it is pulled out, otherwise it is left in the buffer, until something can translate it. When we input numbers there is no problem it will translate them right to an integer, but what happens when you try and put in + or - or * ? Basically they would be ignored by a cin >> an_int; statement, because we are going to use +, - or * to allow the user to select what they want done we need to be able to pull those out to do this we are going to use a char type variable, this will allow us to pull whatever is in the buffer. We have one more thing we have to know about characters, and that is how to use them in operators, ints, and floats are supported in a naturally written way, but you can't do the same thing for characters, therefore to write a character in C++ you simply have to put single quotes around it like this: 'a' is the same as a lowercase A.
Ok due to message length the code for this will be included as a seperate file in a zip, sorry about that, the comments in the file will hopefully give you a good idea what is going on. That's it for today, tomorrow we'll be departing from Variables, and will work on getting this code in an acceptable state.
Lesson 3
OK MB I MAK PONG FURST
Alright so we've covered basic variables and what they are (remember the boxes) we used the integer type (int an_int and declared(or instantiated) an int. Now I failed to mention something else about the types, not only do they tell you how many boxes there are to the variable, it also gives instructions about how to put things together, now short and int are basically the same except their size, unfortunately char decides to be a little difficult, it reads the same way as an integer type(this includes both short and int) but char is specialized to be treated as a character, that is a letter.
There are two more groups of types, the void type, and the floating point types. The void type is used almost as a non-type, we aren't going to use this much when declaring variables, so for now you can forget about it. floating point type refers to decimal numbers, that is to say instead of 3(integer) you can have 3.2(floating point). Here are the three floating point types:
- float a_float;
- double a_double;
- long double a_long_double;
I want to also add a bit to the integer types: First, all integer types come in two varieties signed(includes negative numbers), and unsigned(does not include negative numbers), unless you put an unsigned in front of it an integer is considered a signed integer. Second there is another type of integer called a long, it is supposed to be at least as big as an int. here are all the integer types:
- unsigned short an_unsigned_short;
- short a_short;
- unsigned int an_unsigned_int;
- int an_int;
- unsigned long an_unsigned_long;
- long a_long;
Ok so now we have a whole bunch of new variables to play with, and that is great but we still can't do much of anything interesting or dynamic, so let's fix that:
PHP:
#include <iostream.h>//this is starting to get old
//if you want you could just edit a single file
int main()
{
int an_int; //now where have I seen this before
cout << "Please Enter an Integer Number\n>"; //not just an escape character but text too!
cin >> an_int; //ooh something new and shiny
cout << "was your number "; //old hat just some text
cout << an_int; //whats that something new... oh wait nm
cout << "?"; //more text how cliched
return 0;
}
Well now doesn't look like much in code, but running it you should get something a bit more exciting than a string of numbers. The new bit here is the cin line:
cin >> an_int;
this is pretty simple, it takes input from in this case the keyboard, and puts it into an int, you can actually input to any of the types (excluding void) that I have mentioned.
Alright well this is all very interesting but we still haven't done anything useful, to do that we're going to have to work in some conditionals. What are conditionals ? well simply put they are going to test if something is true, in C++ something is true when it is not zero, it is false if it is zero. Now this isn't terribly helpful if we wanted to see if two variables are the same, so to help this out we have a set of operators(like =,+,-,etc.) that will allow us to do these kind of things, lets take a look at them:
- 3 == 4;
this is a binary operator often reffered to as equals equals, or just equals this is how it reads
is 3 equal to 4 ?
if the left operand(3) is equal to the right operand(4) this will evaluate to a 1, in this case because they are not equal they evaluate to a 0
- 3 != 4;
This one is just the opposite, the ! reads as 'not' in C++ so this reads like this:
is 3 not equal to 4 ?
In this case we evaluate to a 1 because 3 is indeed not equal to 4
- !3;
This is our first unary operator, that means it takes one operand(3) basically if the operand is a non zero
this returns a zero, otherwise it returns a 1.
- 3 < 4;
this reads is 3 less than 4 ?
- 3 <= 4;
this one reads is 3 less than or equal to 4 ?
- 3 > 4;
ok guess... come on not that hard, if you said it reads is 3 greater than 4 ? then you win!
- 3 >= 4;
if you say anything but is 3 greater than or equal to 4, I'm starting to worry.
Alright so now we know how to get some true falses, but that doesn't do much good without the conditionals! I bet you though I forgot about them, in any case we are going to start off with the most basic conditional, the if/else combinations, rather than me rattle on lets get to an example:
PHP:
#include <iostream.h> //if you don't know what this does by now go back and read lesson 1
int main ()
{
int an_int; //du du du
cout << "Please Enter a 5\n>"; //hey a number in the quotes that is sort of new
cin >> an_int; //remember we put a number into an_int with this one
if(an_int == 5) //well that is new
{//OMG brackets inside BRACKETS!
cout << "Good human.";
}
else //hey this is new too
{
cout << "Bad human!";
}
return 0;
}
ok so we have a few new things to talk about first thing is the if block(fancy word for everything in the if statement and inside the curly brackets) basically what happens here is if decides if it executes based upon what the statement inside the parentheses evaluates to. In this case if an_int is actually equal to 5 then it evaluates to 1, and the code inside the curly braces({}) is executed. If(falling into if statements already) an_int is not equal to 5 then the left evaluates to 0, and instead of the if block being executed, the else block is. Alright time for us to make our partially useful application, for this we are going to write a calculator program. Now there are about a million ways to do this(and probably all of them are better than what we will settle on), for now we are going to keep it simple, to do that we have two restrictions:
- Only integer numbers
- No division
Before we get started we have to talk a little bit about how cin works, and also how characters work. something you should always remember about cin is that no matter what you put on the right, what actually gets input is in a character format, it is just translated to the variable on the right for you, though in some cases it can't be translated in which case the character is left in a buffer. All input goes through this buffer if it can be translated it is pulled out, otherwise it is left in the buffer, until something can translate it. When we input numbers there is no problem it will translate them right to an integer, but what happens when you try and put in + or - or * ? Basically they would be ignored by a cin >> an_int; statement, because we are going to use +, - or * to allow the user to select what they want done we need to be able to pull those out to do this we are going to use a char type variable, this will allow us to pull whatever is in the buffer. We have one more thing we have to know about characters, and that is how to use them in operators, ints, and floats are supported in a naturally written way, but you can't do the same thing for characters, therefore to write a character in C++ you simply have to put single quotes around it like this: 'a' is the same as a lowercase A.
Ok due to message length the code for this will be included as a seperate file in a zip, sorry about that, the comments in the file will hopefully give you a good idea what is going on. That's it for today, tomorrow we'll be departing from Variables, and will work on getting this code in an acceptable state.