Saturday, April 9, 2011

Day 1 code

OK so I decided to do this in C++ since I'm much more likely to use C++ in day to day programming.
Here is the code from day one:
/****************************
* Metamomrphix *
* C++ in a year Day 1 *
****************************/
#include
using namespace std;

int main()
{
int limit = 1000;
for (int x = 0; x <= limit; x++)
{
cout << x << endl;
}
cout << "this ends day one. give input to quit" << endl;
cin.get();

return 0;
}

for an explanation the top part before the #include is just a comment section wich can be used to provide information about code.
the #include means to compile the code included in the iostream.h header file. This increases your codes abilities with adding only one line of code. The iostream.h file contains the functions necessary to produce output and will be included in most of the C++ apps that you write. The namespace basically says where the file is located sort of. I learned C++ before namespaces were used so I never really studied them much I just put that line of code in.
int main()
{
......
}
is a function which is a section of code that is executed and can be used in any place in your program. This is a small program so it only has one function wich is the main function that every C++ app must have.
Next you see int limit = 1000;
This is defining a variable that is an integer called limit and initiating it to 1000 Variables are declared in C++ with datatype ;. Or you can also assign a value at the same time you declare it like I did in the example or you could declare the variable on its own line and not assigning a value to it yet. I could have done this by using int limit; and then instantiated it later on in the function by using the line limit = 100;
Next I usewhat is called a for loop to loop through a portion of code. This is the most commonly used loop you will come accross and use in C++. The syntax is for ( datatype name = starting value; name comparison operator limit; name+ value. You see I used the line of code for ( int x; x <= 100; x+). In english this means at the start of the loop a variable of integer type called x is = 0, go through the loop while x is less than or equal to 100, and each time the loop is iterated or gone through increase x by one (x++). Then each time through the loop the code between { and is executed.
cout << << meas to output whatever is between << and <<. In this case I put in the variable x which will be a number between 0 and 100 as per the conditions in my for loop. endl simply means to stop printing on the current line and print on the next line.
You can also output strings or characters between the << and << by putting the text in quotation marks as I did in this line cout << "this ends day one. give input to quit" << endl;. Much more can be done with output in +, but this is day one so we'll call this lesson good after two more quick lines of code.
cin.get() will simply tell the program to pause at this point until input is received from the standard inptu device which is the keyboard in this case. Try putting // before this line of code wich will comment this line out and prevent it from being executed. The program will run through very quick without pausing and you see just a quick glimpse of a dos box on the screen and it'll go away because we didn't tell it to pause.
The last line is very important. Every function in C++ must return a value. We declared the main function of type int so it must return an integer. Therefore upon successful completion the main function will return 0 and let the function and the program in this case end.
That's it until tomorrow.
God Bless,
Metamorphix

No comments:

Post a Comment