0% found this document useful (0 votes)
2 views

1-cpp_basic_elements

The document provides an overview of basic elements in C++ programming, including the structure of a simple program that outputs 'Hello World!'. It explains the use of functions, the importance of including libraries, and how to perform output operations using cout and endl. Additionally, it covers comments in code and demonstrates basic mathematical operations with examples.

Uploaded by

Ahmed Al-nasheri
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)
2 views

1-cpp_basic_elements

The document provides an overview of basic elements in C++ programming, including the structure of a simple program that outputs 'Hello World!'. It explains the use of functions, the importance of including libraries, and how to perform output operations using cout and endl. Additionally, it covers comments in code and demonstrates basic mathematical operations with examples.

Uploaded by

Ahmed Al-nasheri
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

C++ Basic elements.

C++ program must have a function


The following figure is a simple called main;
program written using C++ Function name must be followed by
programming Language. The ().
program display the statement void: is the return data type
(Hello World!) on screen: (will be discussed later).

#include<iostream> cout: is the output operation


which output data to default
void main()
{ output device (screen).
std::cout<<"Hello World!"<<std::endl; cout operation is defined in a
} namespace called std. so to use
cout operation the prefix std::
main() :is a function ( function must be used befaur cout.
is a block of code that have a
name) function begin with { and << : is the output operator which
end with } the instruction must be used with every output
inside the function must be operation. << must be followed
placed between { and } with data to output
This function called main; every
After << there is "Hello World!" # is for preprocessor directive to
note the statement Hello World! include the library iostream with
Enclosed by " " . Which means the program being written.
this is a text or string that
will be output as it is. To avoid writing the prefix std::
before every output operation we
endl: is an instruction to start can use the instruction
new line after the output using namespace std;
operation. Note endl also defined After the include statement so the
in namespace std. program become:
Finally every instruction in C++
must end with semicolon ( ; ). #include<iostream>

using namespace std;


cout and endl instructions are
defined in a library called void main()
{
(iostream) so to be able to use cout<<"Hello World!"<<endl;
them in our program we must }
include the iostream library:

#include<iostream>
Example: write a program that Comments:
display your name on screen: Comments are notes written be the
programmer that have no affect on
Since we what the perform output program instructions and the
operation we need to use cout compiler ignore it. The comment
instruction can be defined using // which will
and to use cout instruction we define a comment for one line.
must include iostream library To define a comment for more than
The output operation must be one line we can start the comment
define inside a function (which be /* and end it be */
is main)your name is a text data #include<iostream>
using namespace std;
so it must be written with in " "
After the output operator << /* this program is an example
how to use comments in
The program become: C++ program */

#include<iostream> void main()


{
using namespace std;
//this is an output opeartion
void main() //this program will print my name
{
cout<<"Sultan M. Al-Rushdan"<<endl; cout<<"Sultan M. Al-Rushdan"<<endl;
} }
Given the following program: #include<iostream>
#include<iostream> using namespace std;
using namespace std;
void main()
void main() {
{ cout<<"3+5="<<3+5<<endl;
cout<<3+5<<endl; }
}
The output of the program is:
The output of the program is: 8 3+5=8
Note the output operation after
Note after the output operator << the first output operator << there
the is the statement 3+5 which is "3+5=" which is enclose in " "
is not defined in " " , so the so it will be considered as text
compiler will not consider it as and will be displayed as it is on
a text, it will consider it as the screen.
numerical data 3 and 5 associated After the second output operator
with addition (+) operation so << the statement 3+5 without " "
the computer will perform the so it will be considered as
addition operation then it will mathematical operation that will
display the result of addition. be performed and the result will
be printed.
The following program shows how The outputs of the program are:
to perform mathimatical
7+5=12
operations:
7-5=2
7*5=35
#include<iostream>
7/5=1
using namespace std;
void main() 7%5=2
{
//addition operation Note the result of division operation
cout<<"7+5="<<7+5<<endl; is 1 not 1.4
This is because the numbers in this
//subtraction opeartion
format is treated as integer numbers so
cout<<"7-5="<<7-5<<endl;
the result of the division is also an
//multiplication operation integer number, and a reminder.
cout<<"7*5="<<7*5<<endl; The modulus operation is the reminder
of the division operation. so 2 in the
//division operation result above is the reminder of
cout<<"7/5="<<7/5<<endl;
dividing 7 over 5.
//modulus operation To produce a real result of division
cout<<"7%5="<<7%5<<endl; operation one of the numbers must be
} real so write 7.0 or 5.0 or both; then
the result will be 1.4

You might also like