C++ Syntax Details
Item |
Description |
Example |
| syntax | Think of this as a combination of spelling and punctuation. C++ only understands you if you use perfect "grammar.". C++ will not work if you don't use the right spelling or use the wrong punctuation. | |
| case sensitive | C++ is case sensitive. That means a command typed as COUT or Cout will not work; the proper syntax is cout | |
| // | Comment |
//This is a comment. /* This is also a comment. Comments let you leave notes to yourself and others. You should use *lots* of comments in your programs. |
| #include <iostream> | preprocessor directive | This sends a message to the compiler to include other items with your program. In this case, it tells C++ to allow you to use basic input and output utility programs. This is an instruction to the compiler to include a header file, as opposed to the rest of your code that compiles. |
| using namespace std; | the using directive | A C++ program can be divided into different namespaces. We'll always use the namespace std, which saves us work later (we don't have to enter std::cout every time we want to use the cout statement. |
|
int main()
|
The main function | Every program has a main function. It must include all of the elements shown. |
| {} | curly brackets | Curly brackets are used to "block" the codes in each function. |
| cout << "message"; | standard output |
Using the built-in cout command, you can make messages print to the console (screen). Notice that the message is enclosed in quotes, and the entire statement ends with a ; The << is called the insertion operator. You are telling C++ to insert data into an output stream (send it to the screen). You can put several << on the same line.
|
| endl | new line |
Go to a new line. Use with the cout statement: cout << "Hello, mommy!" << endl; |
| \n | new line |
Adds a new line, also. Put inside your "output" statement: cout << "\n\n\n This goes down three lines!!"; |