Line 1
#include <iostream>This is an include statement, which indicates to our compiler that we want to use prewritten parts of the standard C++ library. This include statement tells the C++ compiler that we want to use the “iostream” part of the C++ library. “Iostream” lets us get input from users and display output to the screen; we used “iostream” to display “Hello World” on the screen in our first program.
Line 2
using namespace std;This DaniWeb thread has an excellent explanation of namepaces written by forum user Narue. This line prevents naming conflicts; it tells the compiler that we will be using the standard namespace (called “std”). If another namespace has a function with the same name as the one we want to use from the standard namespace, our compiler won't generate an error or use the wrong function because we specified which namespace to use.
Line 3
int main()This line is the beginning (or the function header) of our main function. A function is a set of several programming statements that accomplishes a task. The name “main” is a keyword that lets our C++ compiler know that this is where the program starts. Every C++ program must have a “main” function.
Line 4
{
This line contains an opening brace, which lets the compiler know that this is the beginning of the body of our function. A function’s body contains all of the statements for that function.Line 5
cout << "Hello World" << endl;This statement outputs the words “Hello World” to the screen. This statement has a string literal (“Hello World”) enclosed within quotation marks; the program will output almost anything inside of the string literal's quotation marks to the screen. However, this statement will not output blank lines to the screen if you attempt to enter them by hitting the “Enter” key as you would in a text document. Blank lines can be output to the screen using the “<< endl” portion of the statement instead. This line also has a semicolon, which lets the compiler know when it has reached the end of a statement.
Line 6
Line 6 contains only a blank line. This blank line is referred to as whitespace in programming. Whitespace is ignored by the compiler; it is only there to make the program more legible to programmers. Whitespace includes blank lines, spaces, and tabs.
Line 7
return 0;This line exits the program and sends the value “0” back to the operating system, which indicates that the program executed without any errors.
Line 8
}
This closing brace lets the compiler know when it’s reached the end of our function’s body.Comments
If you follow the link to the Google Drive file with my Hello World code from my last post, you may have noticed that I included a couple of extra lines. Those extra lines were comments. Comments are ignored by compilers, so programmers use them to write notes that explain what each part of a program is doing. There are two kinds of comments.
/* This is a block comment. It can take up multiple lines. The C++ compiler will ignore this; it's just here for the humans who read the code! */A block comment (which starts with a forward slash and an asterisk and ends with an asterisk and a forward slash) is a comment that can take up multiple lines in a program.
cout << "Hello World" << endl; //this is a line commentA line comment (which has two forward slashes in front of it) can only take up one line in a C++ program; anything on a line after the two forward slashes that start a line comment is ignored by the compiler. This kind of comment can be placed after a statement, to explain what that statement does. Placing this kind of comment before a statement on a line can cause that statement to be ignored by a compiler.
Now that we know what each part of our first program does, we are ready to start making our text game. In the next tutorial, we will output a title to the screen, greet our players by name, and learn about variables.
No comments:
Post a Comment