0% found this document useful (0 votes)
5 views5 pages

Lab 1 Intro To C++

This document outlines the basic components of a simple C++ program, including comments, preprocessor directives, and the main function structure. It explains the use of cout for outputting text and the significance of using the std namespace. Additionally, it describes how to insert new lines using and endl.

Uploaded by

Putra Hazim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Lab 1 Intro To C++

This document outlines the basic components of a simple C++ program, including comments, preprocessor directives, and the main function structure. It explains the use of cout for outputting text and the significance of using the std namespace. Additionally, it describes how to insert new lines using and endl.

Uploaded by

Putra Hazim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lesson Outcomes

At the end of this session, student should be able to:


 Understand basic program components
 Use cout
 Use endl, \n

i. Basic Program Components


A simple C++ program has the following form:
//line comments
/*block
comments
*/

#include <iostream> //preprocessor directive


using namespace std;

int main() //main function {


//body of program
variable declaration section;
statements;
return 0;
}

Example:

//My First Program


/* Author : Nor Azida
Date : 20/10/2022
*/

#include <iostream> //preprocessor directive


using namespace std;

int main() //main function {

//body of program
cout << "Hello World!";

return 0;
}

Explanation:
Line 1

1
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by the compiler (will not be executed and they
are used only to allow the readability of the program.

Line 2-4

Multi-line comments start with /* and ends with */.


Any text between /* and */ will be ignored by the compiler.

Line 6

The #include directive is a special instruction for the C++ compiler that tells the compiler to include
the contents of another file, in this case the iostream file. Files that are included in the programs
are called include files or header files.

In the C++, the iostream file contains the instruction (source code) needed to handle input and
output operation, such as inputting data form keyboard and outputting information on the computer
screen. C++ programs typically include at least one directive, and most include many directives.

Line 7

using namespace std means that we can use names for objects and variables from the standard
library.

You might see some C++ programs that runs without the standard namespace library. The using
namespace std line can be omitted and replaced with the std keyword, followed by the :: operator
for some objects.
Example:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}

2
Line 9

Another thing that always appears in a C++ program, is int main(). This is called a function. A function
is simply a block of code that performs a task. The entire line of code, int main() is referred to as a
function header, because it marks the beginning of a function.

Line 10

The beginning brace { marks the beginning of the code block that comprises the function. Everything
between the opening and closing set of braces ({ }) belongs to this main function and is referred to
as the function body.

Line 12

cout (pronounced "see-out") is an object used together with the insertion operator (<<) to
output/print text. In our example it will output Hello World.

Note: Every C++ statement ends with a semicolon ;.

Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }

Line 14

return 0 – this keyword indicate that program terminate in normal condition. return 0 must be

written if the main() function has data type integer (int).

Line 15

Closing brace } marks the end of the program body.

3
New Lines
To insert a new line, you can use the \n character
Another way to insert a new line, is with the endl manipulator.
But what is \n exactly?
The newline character (\n) is called an escape sequence, and it forces the cursor to change its position to
the beginning of the next line on the screen. This results in a new line.

4
5

You might also like