Writing C++ Syntax

Considering the C++ compiler only processes information the way we feed it, a strict syntax is needed to prevent errors from occurring. In programming, every item must be declared, and to prevent the programmer from writing out hundreds of additional pages of code there are global declarations set in place. Below is a brief set of code.

#include
using namsepace std;

int main ()
{
cout return 0;
}

First you will need to include your library. Normal coding in C++ will need the iostream library. This will allow you to provide input and output functionality.

You will follow by including using namespace std; You will need this because it allows you to properly set objects and functions throughout your program as it is a global declaration and will help reduce unnecessary repetition.

The next code you will be placing is declaring the main function. Functions are frequently used in programming and allow the programmer to prevent repeats of code. The main function, however, is a specialized function and may be considered the foundation of your code. The majority of your code will appear within main. You will notice before the main function, is the abbreviation int. Int stands for integer, which of course is a whole number. Each variable or function that you declare will have an initial set variable type. We will go into more depth with this in another tutorial. The empty parenthesis are necessary as main is a function. You will notice in more advanced coding you will have parameters set within the parenthesis to act as local variables for the function. Again, this will be covered later.

You will then notice a curly bracket “{“. This shows the beginning of the function. At the end of the code you will find a closing curly bracket “}”, signalling the end of the function.

Within the main function, you notice the line saying “cout Cout stands for console output. It is a statement used to send information to the output screen. The sister statement of this would be a cin statement (console input) and will be seen in a later tutorial. The signs are put in place to keep order to your code. Following the endl;. The endl statement simply tells the compiler to go to the next line. This is useful if providing multiple lines of code. I will provide an example below.

#include
using namsepace std;

int main ()
{
cout cout cout return 0;
}

Due to the placement of endl; the compiler will display the program as so:
This is proper syntax.
Cout stands for console output.
Don’t forget to keep the statement within the quotes.

If endl; was not in place, your program would of looked like this:
This is proper syntax.Cout stands for console output.Don’t forget to keep the statement within the quotes.

Obviously, you do not want this. You may have also noticed the use of a semi-colon after the endl statement. The semi-colon signals the compiler that that line of code is over. You can never forget a semi-colon. It is also used after using namespace std.

Next is the return statement. After this you will find a 0. Not every function will have a return statement returning the variable 0. What this is doing is that when the function reaches the return statement, it will send back a 0. If a 0 is not returned the compiler will know there is an error appearing within your code. As a beginner programmer, you should not have to worry about changing the variable for a while.


People also view

Leave a Reply

Your email address will not be published. Required fields are marked *