If you've ever used a computer, you know about different applications, files, and a variety of other "things". But, have you ever asked yourself "How do you write a program?", "How do people develop '...'?", "How do I write a program?" ...? I'm sure you've asked at least one of these questions, or questions like it... Well, first, you have to know a little 'bit about "Computer Language" and what programming really is.
A program is a sequence of written instructions that a computer can read and execute. Programming is the act of writing and testing a description of telling a computer what to do in exact detail. Sadly, computers aren't like humans, they're stupid. Pure and simple. So when you write a program, you have to describe your desired instructions precisely and with correct grammar. But, a computer doesn't understand English. Or any language that humans read or write. The language of a computer is simply a series of 0's and 1's. This is called Binary Code. And let me tell you... It's complicated to speak to a computer like that. So we use a "programming language", developed by humans, for humans to tell the computer what to do.
There is a variety of different programming languages out there, but I'm going to focus on C++, a high-level programming language.
As I stated above, C++ is a programming language that was created for humans to tell computers what to do. And to do that, C++ requires you to "compile" your written program. What that means is, the code you write, know as the "source code", will go through a program which will compile your source code and turn that into code that a computer can read. Binary Code.
What a compiler does is read your code and try to make sense of it. It checks to see if your program is grammatically correct, and if every word has a defined meaning without actually executing your program. Should you have left out a needed section of code or have written incorrect syntax, the compiler will give you an error. A compiler has no common sense, so it will be extremely picky, "NAZI", about any mistakes in your code.
NOTE: I'll cover errors in the next tutorial.
Most programs consist of several different parts, and they're often written by several different people. For example, when you write a program, you use parts from the "C++ Standard Library" which is a collection of classes and functions, which I will cover later on, and part of the C++ ISO. To include segments from the C++ Standard Library, you'll use the directive, #include, which include declarations. Declarations are simply programming statements that specify how a piece of code is to be used.
Back to Linking! The separate parts of a program are often called "transition units" which need to be compiled in order to create the machine code. Than, the machine code will be linked together with the object code from the C++ Standard Library resulting in your executable program.
An environment is what you use to to write and put a program together. For example, we have the compiler which translates our code and links it together to create an executable. We also have the editor where we write out our program, like a text editor. We're going to use an IDE, "Interactive Development Environment" or "Integrated Development Environment". And IDE usually includes an editor that often times has colored text which will help distinguish between different aspects of our source code. It may also include a compiler for linking the code together, and a debugger, which will actively find errors in our program and fix them.
NOTE: Some programmers, namely professionals, work from a command line where they must issue the command to compile and link a source code together.
IDE's that I recommend are... CodeBlocks, DevC++, and Microsoft Visual C++.
Your First Program:
So... We finally made it. You now know the jist of what programming is and what programs are... So let's have some fun.
Open up your IDE (from here on out I'll refer to this as your compiler), and make a new project and select "Console Application".
Once you've done that, you should have a blank document. If not, simply erase everything already there. Then, copy the code below into your compiler.
NOTE: Do NOT copy and paste it. I want you to write it out yourself.
// This program outputs "Hello, World!" to your screen
#include <iostream> // Straight from the Standard Library that allows input and output
using namespace std;
int main() // Start by executing the function "main"
// This is a comment and is ignored by the compiler
cout << "Hello, World!\n"; // Outputs "Hello, World!" followed by a newline (\n)
return 0; // Return a value of 0 (I'll cover this later on)
SYSTEM("PAUSE"); // Allows you to view the program before it closes (it's not the greatest method, but it'll work for now)
Now you've written your program and want to know what the hell you just typed up. So read on, my friend! We're almost done!
Comments are an extremely useful aspect of a program. They're ignored by the compiler and are meant for human eyes only. I recommend writing comments all over your program so that if you ever go back to read it (or someone else reads it) or you make an error somewhere, you'll know what you were trying to do. Comments are declared by " // " and the entire line is ignored by your compiler.
Functions are a group of statements that are given a name which can be be used throughout the program. An example of a function in "Hello, World!" would be the "int main()". Functions can be named anything.
Input/Output Statments are just that. Statements that will either output a value, or allow a user to input a value. In "Hello, World!", we defined an output statement that was read to your screen. You can also input statements after you've declared a variable or type. Input and Output statements are declared as followed:
Variable and Types are ways of declaring different kinds of values that your program will use and read back and forth. In "Hello, World!", we didn't use any variables or types. In the next lesson, you'll learn about the different kinds of variables and types as well as what they can hold and how they are used properly.
cout << OUTPUT;
cin >> INPUT;