Basic Program Structure in C++: Study Guide For Module No. 2
Basic Program Structure in C++: Study Guide For Module No. 2
Basic Program Structure in C++: Study Guide For Module No. 2
0 10-July-2020
Welcome to this module! By the time that you are reading this, you have been immersed with the introductory
concepts of programming and started your adventurous journey in programming. Further, problem-solving steps
were introduced to you in the previous module. However, the best way to learn to program is through writing
programs directly. Thus, ready your tools and enjoy programming!
Introduction
C++ programs have a specific structure that you need to discover. It also features specific language syntax
that you need to follow. Step-by-step, you will learn what specific statement does. Please take the time to read
the discussions and try to run the actual codes.
int main()
{
std::cout<<"Hello World";
return 0;
}
[Sample Output]
Hello World
Build (Compile) your source file to create an executive file and run the executable file, either thru:
Going to Build > Build and run
Pressing [F9] key
Clicking the Build and Run button in the tool strip.
If you don’t prefer to use a dedicated Integrated Development Environment (IDE) such as the Code::Blocks,
you may use online C++ editors and compilers such as:
http://cpp.sh/
https://www.onlinegdb.com/online_c++_compiler
https://www.jdoodle.com/online-compiler-c++/
https://repl.it/languages/cpp
For example, the following screenshot provides shows the use of the cpp.sh site where you can enter your
source codes and compile it. The output for your program will also be shown on the same page!
Preprocessor Directives
These lines are noticeable to start with a hash sign (#). These are not program statements but are directives for the preprocessor which
examines these lines before compilation and resolves the directives giving instructions to the compiler to preprocess the information.
Other preprocessor directives are #define and other C++ Macros.
[https://www.tutorialspoint.com/cplusplus/cpp_preprocessor.htm]
The preprocessor directive #include<iostream> makes the inclusion of another C++ code, the iostream
header file. These standard C++ codes are necessary for our input and output operations.
Bear in mind that all of our programs will use the #include<iostream> directive.
The next line to consider is the int main() marks the entry point to our program. It declares the main
function. The main function is a special function that is called when the program is run, thus, the entry point to
our program. In that line, int specifies a return type and the parenthesis is used for parameter declaration.
Details about these will be discussed in the next modules.
Functions
A function is a block of code grouped under a specific name. When the function name is called, the program codes inside the function
body will be executed.
After the int main(), you’ll see { while at the bottom of the code you’ll see its partner }. These are curly
braces and shall be partnered with each other. If you see an opening curly brace ({), there should be a
corresponding closing curly brace (}). These specify the function’s body which will be done when the function
is called.
std::cout<<"Hello World"; is the statement that makes the words “Hello World” are displayed on the
console, but without the double-quotes (“”).
C++ Statements
These are expressions terminated by a semicolon (;) that specifies an action.
std::cout specifies an action to output or display something on the screen. It stands for standard character
output. After it, the insertion operator (<<) is used to pass the characters or numbers to the cout command. So
whatever follows the insertion operator, it is displayed on the screen. In this case, the string “Hello World” is
used. When displaying words to the screen, be sure to wrap them inside double-quotes.
Before the closing curly brace, the return 0; line marks the end for the execution of our program. The
program will send a value back to the Operating System, which is zero (0), to indicate that it ran successfully.
It is also worth to mention about whitespaces. Whitespaces can be spaces, tabs, and newlines or blank lines.
These are ignored if not inside the double-quotes (“”).
At this point, you should have been able to create a C++ source file, build/compile the source file into an
executable file, and execute/run the executable file. Let’s look at another code:
C++ Code [helloworld.cpp]
#include<iostream>
using namespace std;
int main(){
cout<<"Hello "<<endl;
cout<<"World";
return 0;
}
[Sample Output]
Hello
World
A slight difference in the previous code versus our very first code is the line using namespace std;. This
line specifies that we are using the namespace std or standard. Thru this line, all our std::cout lines can be
shortened into just cout.
A good discussion on why should you use the using namespace std; can be found here.
endl specifies that the next output will be shown on a new line.
LEARNING ACTIVITY 1
Create another C++ source file and save it as biodata.cpp. Your program should display your complete name,
address, contact numbers, birthdate, birthplace, and a list of your skills, each separated by a new line.
Introduction
We’ve covered the cout object in our first codes which are used for output. Let’s discuss some things you
could do with the cout object and introduce the input object. All of these objects are taken from the iostream
library under std namespace, thus, don’t forget to add the #include<iostream> and
using namespace std; lines.
int main()
{
cout<<3+4;
cout<<endl;
cout<<"3+4 is "<<3+4<<endl<<"ending line";
return 0;
}
[Sample Output]
7
3+4 is 7
ending line
The cout object can be used with various expressions and values to be displayed. On the cout<<3+4; line,
the answer to the expression (7) is displayed. However, when the same expression is wrapped inside the
double-quotes, it becomes a string constant, which is displayed exactly how it is typed. Notice too that the
endl can be sandwiched between string constants or expressions which breaks the line with a new line.
int main()
{
cout<<"new \nline\n";
cout<<"backslash \\";
cout<<"\n";
cout<<"first word \t second word\n";
cout<<"This is what he said, \"Hi!\"";
}
[Sample Output]
new
line
backslash \
first word second word
This is what he said, "Hi!"
int main()
{
short age=0;
cout<<"Enter your age: ";
cin>>age;
cout<<"You are "<<age<<" year(s) old";
return 0;
}
[Sample Output]
Enter your age: 23
You are 23 year(s) old
short age=0; declares a variable named age to be of short data type. We’ll cover many things about
variables and data type on the next module. So don’t worry much about it, think of variables as holders of
data. We just need to have a holder to store the value which the user will be going to input.
The line cin>>age; allows the user to enter a number and store it to the variable age. After which, that value
is displayed on the line cout<<"You are "<<age<<" year(s) old";
When you enter a different value aside from a number to our program, it will return an error. That is, our
program expects a number as an input.
LEARNING ACTIVITY 2
Write a C++ program that displays “9 x 1 equals 1”, but the answer should be displayed through an
expression. Repeat the output until it becomes “9 x 10 equals 90”, separated by a new line.
Introduction
Comments are an essential part of our source code to document our programs. However, comments are not
executable portion of our source code, as these are ignored by the compiler. Thus, comments are just notes
to be read by programmers. In this part, we will discuss two ways we could use comments in our codes.
int main()
{
//This program will display the answer for the expression 3+4
cout<<3+4;
cout<<endl;
//cout<<"3+4 is "<<3+4<<endl<<"ending line";
return 0;
}
[Sample Output]
7
In the previous code, we have two comments. The symbol // marks that specific line as a comment and will be
ignored by the compiler. Hence, the line //cout<<"3+4 is "<<3+4<<endl<<"ending line"; is not
executed.
int main()
{
/*
[Sample Output]
7
3+4 is 7
ending line
As in the example, anything in the multi-line comment symbols (/* and */) are treated as comments and will be
ignored.
It is not necessary to put comments on each statement explaining what the statement does. Typically, comments
are used to describe what a program does to give the reader an idea without reading the actual code.
LEARNING EXERCISES
In your program, substitute ??? with your name. If necessary, adjust the positions and the number of stars to
produce a rectangle.
SUMMARY
In this section, we have covered the basic program structure for creating C++ programs. Try to remember the
basic program structure as we will be using them in the next modules. Cout and cin objects are useful in
creating interactive programs. Character escape sequences can be used to modify the output from the cout
object. Also, comments are essential for the documentation of our programs.
REFERENCES