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
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
using namespace std;
int main()
{
// this statement outputs a string.
cout
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
To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search:
C++ Taking the Bull by the Horns - Part 1
C++ Taking the Bull by the Horns - Part 2
C++ Taking the Bull by the Horns - Part 3
C++ Taking the Bull by the Horns - Part 4
C++ Taking the Bull by the Horns - Part 5
C++ Taking the Bull by the Horns - Part 6
C++ Taking the Bull by the Horns - Part 7
C++ Taking the Bull by the Horns - Part 8
C++ Taking the Bull by the Horns - Part 9
C++ Taking the Bull by the Horns - Part 10
C++ Taking the Bull by the Horns - Part 11
C++ Taking the Bull by the Horns - Part 12
C++ Taking the Bull by the Horns - Part 13
C++ Taking the Bull by the Horns - Part 14
C++ Taking the Bull by the Horns - Part 15
C++ Taking the Bull by the Horns - Part 16
C++ Taking the Bull by the Horns - Part 17
C++ Taking the Bull by the Horns - Part 18
C++ Taking the Bull by the Horns - Part 19
C++ Taking the Bull by the Horns - Part 20
C++ Taking the Bull by the Horns - Part 21
C++ Taking the Bull by the Horns - Part 22
C++ Taking the Bull by the Horns - Part 23
C++ Taking the Bull by the Horns - Part 24
C++ Taking the Bull by the Horns - Part 25
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- Web Accessibility, Usability and Basic SEO TipsThis is an article containing definitions of accessibility and usability, important things to consider when designing a website and basic SEO tips.
- The Basic Dilemma of the ArtistThe function of bridging the gap between our idiosyncratic, private languages and a more universal one was relegated to a group of special individuals called artists.
- Esperanto 101: Simple Answers to Basic QuestionsImagine a simple language with a 100% phonetic spelling and pronunciation, whose grammar can be summed up in just sixteen rules. Is it for real?
- Styles of Manuscript Editing: There Are Two Basic Forms of Helping an Author Write...When you are correcting or making changes in a book manuscript, what you are doing is editing that manuscript. I've been in the business of editing would-be books for over the past twenty-five years.
- How Syntax is Ruining Gay MarriageA look at Senator John McCain's definition of "Gay Marriage" and how that affects the public's opinion on the matter.
- C Basic Syntax
- PHP Basic Syntax
- ActivePerl Basic Syntax
- The Principles of Batch Process Program Development: Basic Syntax
- Understanding Sentence Types and Syntax in Grammar
- JavaScript Conditional Statements
- How to Write a Basic Sentence




1 Comments
Post a Commentgood day im a student here in the philippines,my course is B.S. in Information Technology 1st year 2nd sem we tackle about c++ programming hope you can help a student like me in studying and understanding this kind of program.thank you.