468,457 Members | 1,762 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

Class 2: Hello World. All Right, Let's Do This!

Banfa
9,051 Expert Mod 8TB
Posted by Ganon11

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
  1. #include <iostream>                              // Line 1
  2. using namespace std;                             // Line 2
  3.  
  4. int main() {                                     // Line 3
  5.     cout << "Hello, World!" << endl;             // Line 4
  6.  
  7.     return 0;                                    // Line 5
  8. }                                                // Line 6
  9.  
  10. /* Output
  11.  
  12. Hello, World!
  13.  
  14. */
  15.  
*Sniffle*...brings a tear to my eye every time...All right, enough crying. What does all this mean? Let's take it one line at a time.

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
  1. std::cout << "Hello, World!" << std::endl;
  2.  
which is just UGLY. The last thing in this line is the semicolon. Earlier, I called a line of code a statement. Each statement is like an English command. In English, I can say, "Run a lap. Do 50 pushups. Say "I love you." Run another lap." You know how to seperate my commands based on where the period is. Similarly, the computer knows how to seperate my statements based on where the semicolon is. Without the semicolon, the computer might think that Line 4 and Line 5 were all part of the same statement, which would mess things up.

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
  1. #include <iostream>
  2. using namespace std;
  3. int main() { cout << "Hello World! << endl; return 0; }
  4.  
Or
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout << "Hello World! << endl;
  6. return 0;
  7. }
  8.  
But which is easiest to read? My style is called indenting - I use spaces to let you know what statements are part of what block. This may not be too difficult to understand now, but consider this:
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2. block1 {
  3. anotherBlock {
  4. block3 {
  5. statements;
  6. }
  7. moreStatements;
  8. }
  9. finalStatement;
  10. }
  11. return 0;
  12. }
  13.  
It's hard to tell what block moreStatements belongs to, right? That's hard to read! But if I wrote this as
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.     block1 {
  3.         anotherBlock {
  4.             block3 {
  5.                 statements;
  6.             }
  7.             moreStatements;
  8.         }
  9.         finalStatement;
  10.     }
  11.     return 0;
  12. }
  13.  
This is the EXACT SAME THING as before, but it's a lot easier to read, right? Your particular indenting style may vary - for exaple, putting the '{' symbol on the line after int main() instead of on the same line. But this won't change how the program is run, only how easy your code is for someone else to read. Develop your own style, borrow mine, it doesn't matter. Just please use something to make it legible so I can read it!

Until next time, programmers, when we'll learn a little more about outputting, and get some practice doing it for ourselves!
Dec 5 '06 #1
0 4302

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by MoCha | last post: by
10 posts views Thread by Bezalel Bareli | last post: by
2 posts views Thread by Jim Red | last post: by
5 posts views Thread by Joe Van Dyk | last post: by
4 posts views Thread by joh12005 | last post: by
3 posts views Thread by shapper | last post: by
5 posts views Thread by Lyle Avery | last post: by
9 posts views Thread by d.adamkiewicz | last post: by
7 posts views Thread by triplejump24 | last post: by
10 posts views Thread by ma | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.