We have the foundation.
We know what it means to input or output.
We won't get confused when we're told to compile something.
It's time to do some programming!
In every language, there is one program which defines all of the syntax basics while displaying a friendly, warm message to encourage beginning programmers to keep going. This program is called Hello World, and here it is!
Expand|Select|Wrap|Line Numbers
- #include <iostream> // Line 1
- using namespace std; // Line 2
- int main() { // Line 3
- cout << "Hello, World!" << endl; // Line 4
- return 0; // Line 5
- } // Line 6
- /* Output
- Hello, World!
- */
Line 1: This line tells the compiler that you want to include the files under iostream.h - a header file. iostream is Computer-Talk for Input/Output Streaming. This means that your program now recognizes all the functions and commands regarding input and output. As input/output are necessary for all programs, this line (also known as a statement) is vital.
Line 2: This will be explained along with line 4. For now, just be content with the knowledge that without this line, programming can become quite frustrating.
Line 3: This is called a function header. It tells the compiler that everything between this curly bracket '{' and the next curly bracket '}' are all part of the function main. Additionally, this function 'returns' an integer (int). The parentheses '()' are more useful when you make your own functions, but that's a ways off.
Line 4: Ahh...the first statement that does something! This is what tells the computer to display the words "Hello, World!". This is done with cout. cout stands for Common OUTput - by default, the monitor is your default output. cout is followed by two braces '<<' which tell the compiler that whatever follows is your output. "Hello, World!" is some text that must be enclosed in quotation marks. Be sure to close any quotation marks you start! Otherwise, the computer doesn't know when to stop reading text for output. Finally, we have endl, which tells the computer to put any further output on the next line.
Here's where Line 2 comes in handy. cout and endl are devices that have already been defined - we didn't define them. But we did have to tell the computer to include them. cout and endl, along with some other basic things, are all included in a group of objects (called a namespace). The namespace these happen to be located in is called std, or standard.
Without telling the computer that we are using std, we would have to type a lot more. Line 4 would become
Expand|Select|Wrap|Line Numbers
- std::cout << "Hello, World!" << std::endl;
Line 5: This is the final statement of the function main. Here is where we return a value. Most functions we will write will have our own special personalized return values, but main() has only 2 return values. If we return 0, the computer knows that everything has run perfectly well. All is fine with the world... But if we return 1, the computer knows something has gone wrong. For example, suppose you ask the user for a number between 1 and 10. Cool. But what if the user types 56? Well, that's not between 1 and 10, so the program won't run like we thought! It would be proper to return 1 in this case to let the computer know that we're finished, but things didn't go as planned.
Line 6: The end bracket, corresponding to the bracket opened in line 3. This lets the computer know that any code after this is NOT involved with main().
A final note before we end: Compilers don't care if my code is easy to read or not. Technically, I could rewrite this program as
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- using namespace std;
- int main() { cout << "Hello World! << endl; return 0; }
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- using namespace std;
- int main()
- {
- cout << "Hello World! << endl;
- return 0;
- }
Expand|Select|Wrap|Line Numbers
- int main() {
- block1 {
- anotherBlock {
- block3 {
- statements;
- }
- moreStatements;
- }
- finalStatement;
- }
- return 0;
- }
Expand|Select|Wrap|Line Numbers
- int main() {
- block1 {
- anotherBlock {
- block3 {
- statements;
- }
- moreStatements;
- }
- finalStatement;
- }
- return 0;
- }
Until next time, programmers, when we'll learn a little more about outputting, and get some practice doing it for ourselves!