You are on page 1of 7

‫‪C++ / First stage‬‬ ‫‪M.Sc.

Ali AL-musharfawi‬‬

‫‪C++ Programming‬‬
‫‪Part one‬‬

‫برمجة الحاسوب ‪ /‬المرحلة األوىل‬


‫هندسة تقنيات الحاسوب‬

‫‪By:‬‬
‫م‪.‬م عل ر‬
‫المشفاوي‬ ‫ي‬
C++ / First stage M.Sc. Ali AL-musharfawi

Introduction
Variables types:
1. Integer (int) 4-bytes
ex: int a; int a,b; int a=3;
2. Float 4-bytes
ex: float a; float x,y,z; float a=3.5;
3. Double 8-bytes
ex: double a; double a,b; double a=8.95;
4.character (char) 1-byte
ex: char x; char x='a'; char y='A';
5.string
ex: string x; string x="computer engineering";

+Addition - subtraction * multiplication / division % Modula


(remainder of an integer division)

The division result are:


Integer / integer = integer ► 39/7=5
Integer / float = float ► 39/7.0 =5.57
float / float = float ► 39.0/7.0=5.57
float / integer = float ► 39.0/7 =5.57
Assignment Operators: The operatonal assignment operator has the
form: Variable = variable operator expression;
Ex: x=x+5; y=y*10;

1
C++ / First stage M.Sc. Ali AL-musharfawi

The operational assignment operator can be written in the following form:


Variable operator = expression;
Ex: x+=5; y*=10;
Exercise: Rewrite the equivalent statements for the following examples,
and find it results. Assume: X=2, Y=3, Z=4, V=12, C=8.

Equivalent
Example Result
Statement
X += 5 X=X+5 X=7
Y -= 8 Y=Y-8 Y = -5
Z *= 5 Z=Z*5 Z=
V /= 4 V=
C %= 3 C=

Relational operators:
< less than
> greater than
<= less than or equal
>= greater than or equal
Equality operators:
= = equal to
!= not equal to
Logical operators:
Not (!)
And (&&)
or (||)

2
C++ / First stage M.Sc. Ali AL-musharfawi

The keyboard and screen I/O instructions in C++ are:


(a) COUT/ display an object onto the video screen:

Cout<<var.1<<var2<<…<<var.n;

(b) : Cin/ It is used to read an object from a standard input device


(keyboard):

Cin>>var.1>>var.2>>…>>var.n;

3
C++ / First stage M.Sc. Ali AL-musharfawi

To begin learning C++ lets examine our first C++ Program:

Example: A program to print welcome

#include<iostream.h> output
int main( )
{ Welcome

cout << “Welcome”;


}

endl or \n is used in c++ to represent a new line, as shown in the


following example:

Example:
#include<iostream.h>
output:
void main( )
hello
{
students
cout << “hello” << endl;
cout << “students”;
}

4
C++ / First stage M.Sc. Ali AL-musharfawi

Expample: Write a program that reads the radius of a circle, then computes and
outputs its area.

#include<iostream.h>
int main( )
{ Output:
const float pi = 3.14; enter the radius of circle: 5
int r; the area of circle : 78.5
float c;
cout << “enter the radius of circle:”;
cin>>r;
cout<<endl;
c = r * r * pi
cout << “the area of circle :” << c;
}

Expample: The following program computes the arithmetic operators.

#include<iostream.h>

int main( ) Output:


{ enter two numbers:
int a,b ; 20 10
cout << “enter two numbers:"<<endl; Sum= 30
cin>> a>>b;
Sub= 10
cout<<”Sum=”<<a+b<<endl;
Mul= 200
cout<<”Sub=”<<a-b<<endl;
Div=2
cout<<”Mul=”<<a*b<<endl;
cout<<”Div=”<<a/b<<endl;
} 5
C++ / First stage M.Sc. Ali AL-musharfawi

The “math.h” library contains the common mathematical function


used in the scientific equations.

Common function from math.h library:


Mathematical Expression C++ Expression
eⁿ Exp (x)
Log(x) Log10 (x)
Ln(x) Log (x)
Sin(x) Sin (x)
xⁿ Pow (x,n)
√x Sqrt (x)

Example: Write the following equation as a C++ expression and state the
order of evaluation of the binary operators:

You might also like