Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views4 pages

Lab 1b - Introduction To C++

introduction of C++

Uploaded by

fa23-bsm-040
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)
11 views4 pages

Lab 1b - Introduction To C++

introduction of C++

Uploaded by

fa23-bsm-040
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/ 4

First C++ Program

Hello World

C++ Syntax
Line 1: #include <iostream> is a header file library that lets us work with input and output
objects, such as cout (used in line 5). Header files add functionality to C++ programs
Line 2: using namespace std means that we can use names for objects and variables from the
standard library.
Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.

Line 4: Another thing that always appear in a C++ program, is int main(). This is called a function.
Any code inside its curly brackets {} will be executed.

Line 5: 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 ;
Line 6: return 0 ends the main function.

Line 7: Do not forget to add the closing curly bracket } to actually end the main function.

Omitting namespace
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
C++ Output (Print on Screen)
The cout object, together with the << operator, is used to output values/print text:
Note:
You can add as many cout objects as you want. However, note that it does not insert a new line
at the end of the output:

New Line:
To insert a new line, you can use the \n character:
Another way to insert a new line, is with the endl manipulator:

Display Variable Values


The cout object is used together with the << operator to display variables.

To combine both text and a variable, separate them with the << operator:

Example
Exercise
1. Create a variable named myNum, assign value 50 to it and display it on screen.
2. Display the sum of 5 + 10 using two variables x and y only.
3. Create a variable named z, assign the sum of values in x and y that are initialized before.
Display the result.
4. Create three variables of the same type i.e. float, using the comma-seperated list.
Calculate the sum of three initialized values and display the result.

You might also like