Broad Network


C++ Basic Syntax

C++ Taking the Bull by the Horns – Part 2

Forward: In this part of the series, I give you the basic syntax of C++.

By: Chrysanthus Date Published: 21 Aug 2012

Introduction

This is part 2 of my series, C++ Taking the Bull by the Horns. In this part of the series, I give you the basic syntax of C++.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Statement
A statement in C++ is a short piece of code that ends with a semicolon. An example is:

        cout << "Hello World!";

This particular statement sends the string "Hello World!" to the output. In other words this prints the string "Hello World!" on the Command Prompt window.

Comments
You should have comments in your code. Comments are not executed. Comments are to remind you later of why you typed a particular piece of code. There are two types of comments: single-line comments and multiple-line comments. A single-line comment can only be in one line; something like:

        // This is a single-line comment.

A single-line comment begins with a double forward slash. For a single-line comment, everything to the right of the comment is not executed.

A multiple-line comment begins with /* and ends with */ . An example is:

/* This is a multiple-line comment. It can be of any length, and
you can put whatever you want here. */

A multiple-line comment spans more than one line. The opening delimiter is a forward slash and asterisk. The closing delimiter is an asterisk and forward slash.

You will try the following code, which has the two types of comments:

/* This is my first C program.
  I am learning C */
#include <iostream>
using namespace std;

int main()
    {
        // this statement outputs a string.
        cout << "Hello World!";

        return 0;
    }

To try the above source code, type it in a text editor. Save the document with the name comment.cpp in the MinGW directory. Go to the C:MinGW> DOS Prompt in your Command Prompt window. Type the following compiler command:

    g++ comment.cpp -o comment.exe

This command instructs the compiler to produce the executable file, comment.exe from comment.cpp and save the executable file in the working MinGW directory.

Note the single and multiple line comments in the source code. The aim of doing all this is to see if the comments in the source code will have any effect in the execution of the program.

At the C:MinGW> DOS Prompt, type the following name of the executable file and press Enter, to run the program:

    comment.exe

The program should output, Hello World! .

As you can see the comments have not affected the execution of the program. Comments are in your source code to remind you in future of what and why you typed a particular code segment.

There are many code samples in this series that you will try. I will not be telling you what to do in order to try a code sample. You will follow the above procedure given your own name to the files. Let the source code file and the executable files have the same name, but with different extensions. Let the source file have the extension, cpp for the program C Plus Plus; and let the executable file have the extension, exe meaning executable.

Block
A block in C++ source code is a set of statements delimited by the curly braces, { and }. Many blocks are preceded by an expression. Such preceding expression can be considered as part of the block. In the above source code you have the expression, “int main()” followed by lines (statements) in curly braces. This is an example of a block. We shall come back to this particular block as we carry on with the series.

That is what I have for this part of the series. We continue in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message