URUK UNIVERSITY
Department of Computer Engineering
Techniques
Programming Essentials
First Stage
Lecture 2
By
Lecturer: Asst. Lec. Duha Salam
2025-2026
1. Character Set
C++ has the letters and digits, as show below:
Uppercase: A, B, C, ... , Z
Lowercase: a, b, c, ... , z
Digits: 0, 1, 2, ... ,9
In C++ language, upper case and lower case letters are distinct and hence there
are 52 letters in all. For example bag is different from Bag which is different
from BAG.
Example: int num = 10 ;
int Num = 20 ;
int NUM = 30 ;
Escape Code:-
Special Escape Code:
Escape Code Description
\n New line. Position the screen cursor to the beginning of the next line.
\t Horizontal TAB (six spaces). Move the screen cursor to the next tab stop.
Example:
#include <iostream>
using namespace std; Output:
int main() { Hi
cout<< "Hi\nHow are you\n"; How are you
cout<< "Hi\tHow are you"; Hi How are you
return 0;
}
1
2. Identifiers
An identifier is the name we give to elements in a program, such as a
variable, constant, array, function, structure, or class.
Rules for identifiers:
1) Must start with a letter (A–Z or a–z).
2) Can include letters and numbers, but no spaces are allowed.
3) C++ allows identifiers of up to 127 characters.
A variable should not begin with a digit.
Some examples of valid identifiers are as follows:
My_name (7 char.)
i (1 char)
Examples of invalid identifiers:
1name (starts with a digit)
total marks (contains a space)
@value (contains a symbol)
3. Variables Declaration
A variable is a named location in memory used to store data. Declaring a
variable informs the compiler about its name and data type.
2
Syntax:
data_type variable_name = value;
Examples:
int age; // declares an integer variable
float salary; // declares a floating-point variable
char grade = 'A'; // declares and initializes a character variable
double pi = 3.14; // declares and initializes a double variable
Common Data Types:
int: Integer values (e.g., 1, 42, -10)
float: Decimal numbers (e.g., 3.14, -0.01)
double: More precise decimal numbers
char: Single characters (e.g., 'A', 'b')
bool: Boolean values (true or false)
string: Sequence of characters (e.g., "Hello, World!")
4. C++ Statement
A computer program is a list of "instructions" to be "executed" by a
computer. In a programming language, these programming instructions are
called statements.
Types of Statements:
Statement Description Example
Expression Statement Performs a calculation or assignment. x = y + z;
A block of multiple statements grouped { x = 5; y = x + 2;
Compound Statement
with { }. }
Controls program flow (conditions, if (x > 10)
Control Statement loops). { cout << x; }
Declaration Statement Declares variables or constants. int a;
3
Key Points:
• Each statement ends with a semicolon (;).
• Compound statements do not require a semicolon after the closing brace
( }).
5. C+ + Operators
1. Arithmetic Operators
Arithmetic Operators in C++ are used to perform arithmetic or mathematical
operations on the operands (generally numeric values).
Operator Name Example
+ Addition X+Y
- Subtraction X-Y
* Multiplication X*Y
/ Division X/Y
% Modulus X%Y
++ Increment ++X
-- Decrement --X
4
Example:
#include <iostream>
using namespace std;
Output:
int main() { 13
int x = 10; 7
int y = 3; 30
cout << (x + y) << endl; 3
cout << (x - y) << endl; 1
cout << (x * y) << endl;
cout << (x / y) << endl;
cout << (x % y) << endl;
return 0;
}
Order of Operations:
The arithmetic operators are governed by a law called the precedence
law or the order of operations, which is a law that gives priorities to some
operations over others.
Operator Operation
() Grouping parenthesis
- Negative sign
*, / , % Multiplication, division, modulus
+,- Addition, subtraction
Example 1 : C = X+Y*X-Z
where X=5, Y=6, and Z=8.
5 + (6*5)-8 (5+30)-8 35-8 27
5
Example:
#include <iostream>
using namespace std; Output:
int main() The Result= 27
{ int X=5, Y=6, Z=8;
int C;
C= X + Y * X - Z ;
cout << "The Result= " << C << endl;
return 0;
}
2. Assignment Operators: The operational assignment operator has the form:
Variable= variable operator expression;
Ex: x=x+5; y=y*l0;
The operational assignment operator can be written in the following form:
Variable operator = expression
Ex: x+=5; y*= 10;
It is used to assign back to a variable, a modified value of the present holding:
Operator Example Same As
= X=5 X=5
+= X +=3 X=X + 3
-= X -=3 X=X – 3
*= X *=3 X=X * 3
/= X /=3 X=X / 3
%= X %=3 X=X% 3
&= X &=3 X=X & 3
|= X |=3 X=X | 3
Example:
#include <iostream>
using namespace std; Output:
int main() { 15
int x = 10;
x += 5; // same as x = x + 5
cout << x << "\n";
return 0; }
6
3. Comparision and logical operators:
Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and make
decisions.
The return value of a comparison is either 1 or 0, which means true (1)
or false (0). These values are known as Boolean values. It has three types
relational operators, equality operators, and logical operators.
a. Relational operators: < less than, > greater than, <= less than or equal, >=
greater than or equal.
Ex: 3 > 4 false, 6 <=2 false, 10>-32 true, (23*7)>= (-67+89) true
b. Equality operators: == equal to, != not equal to
Ex: a=4, b=6, c=8. a==b false, (a*b) !=c true, 's' == 'y' false.
c. Logical operators: The logical expression is constructed from relational
expressions by the use of the logical operators not(!), and(&&), or (||).
7
Operator Name Example
= Equal to X==Y
!= Not equal X != Y
< Greater than X>Y
> Less than X<Y
>= Greater than or equal to X >= Y
<= Less than or equal to X <= Y
Example:
#include <iostream>
using namespace std; Output:
int main() { 1
int age = 18; 0
cout << (age >= 18) << endl;
cout << (age < 18) << endl;
return 0;
}
Logical Operator
Operator Name Description Example
&& Logical and Returns true if both statements are x < 5 && x < 10
true
|| Logical or Returns true if one of the x < 5 || x < 4
statements is true
! Logical not Reverse the result, returns false if !(x < 5 && x < 10)
the result is true
Example:
#include <iostream>
using namespace std; Output:
int main() { 1
int x = 5;
cout << (x > 3 && x < 10);
return 0;
}
8
6. Constants
A constant is a value that does not change during the execution of a
program.
Types of Constants in C++:
Literal Constants: Fixed values, e.g., 42, 3.14, 'A', "Hello".
Symbolic Constants: Declared using the const keyword .
Syntax:
const data_type variable_name = value;
Example:
const float pi = 3.14;
Key Point: Use const for variables whose values should not change
Example :
Write a program that reads the radius of a circle, then computes and
outputs its area.
#include <iostream>
using namespace std;
Output:
int main() { enter the radius of circle:4
const float pi = 3.14;
int r; float c; the area of circle:50.24
cout << "enter the radius of circle:";
cin>>r;
cout<<endl;
c = r * r * pi;
cout <<"the area of circle:"<< c;
return 0;
}
9
Example :
The following program computes the arethmatic operators.
#include <iostream>
using namespace std; Output:
enter any two numbers
int main() { 9 3
int a,b,sum,sub,mul,div; a=9 b=3 sum=12
cout << "enter any two numbers"<<endl; sub=6
cin>> a>>b; mul=27
sum=a+b; div=3
sub=a-b;
mul=a*b;
div=a/b;
cout<<"a="<<a<<"b="<<b<<"sum="<<sum<<endl;
cout<<"sub="<<sub<<endl;
cout<<"mul="<<mul<<endl;
cout<< "div="<< div<< endl;
return 0;}
The increment and decrement operators have two ways to be used:
1. Prefix notation: when the increment or decrement operator comes before
the variable, the following code will print 51, 51:
a = 50
b= ++a
Output: a=51 , b=51
This statement b = ++a; first tries to evaluate the right-hand side of the
expression, which is ++a and since it is a prefix notation, it will add one to the
value of the variable a to become 51, and then this value is assigned to the
variable b to also become 51.
10
2. Postfix notation: when the increment or decrement operator comes after
the variable, the following code will print 50, 51.
a = 50
b= a++
Output: a=50 , b=51
This statement b = a++; first tries to evaluate the right-hand side of the
expression, which is a++ and since it is a postfix notation, it will assign the
current value of a, which is 50, to the variable b, then add one to the value of the
variable a to become 51.
Example :
#include <iostream>
using namespace std;
int main() Output:
{ 51 51
int a = 50, b;
b = ++a;
cout << b << " " << a;
return 0;
}
Example :
#include <iostream>
using namespace std;
int main() Output:
{ 51 50
int a = 50, b;
b = a++;
cout << b << " " << a;
return 0;
}
11
7. The "cmath" Library
The <cmath> library in C++ provides essential mathematical functions to
perform complex calculations. These functions are widely used in scientific
computations, engineering applications, and problem-solving.
Example 1:
#include <iostream>
#include <cmath> Output:
using namespace std; 7.38906
0.60206
int main() { 1.38629
double x = 4.0; -0.756802
double n = 2.0; 16
cout << exp(n) << endl; 2
cout << log10(x) << endl;
cout << log(x) << endl;
cout << sin(x) << endl;
cout << pow(x, n) << endl;
cout << sqrt(x) << endl;
return 0;
}
12
Example 2: Simple Equation
Write the following equation in C++ and determine its order of evaluation:
Solution: C++ Expression:
f = sqrt ( (sin(x) + pow(x, 3) ) / ( log10(x) - x / 2 ) );
Order of Evaluation:
1. Compute sin(x).
2. Compute pow(x, 3).
3. Add sin(x) + pow(x, 3).
4. Compute log(x).
5. Compute x / 2.
6. Subtract log(x) - x / 2.
7. Divide the numerator by the denominator.
8. Take the square root using sqrt.
Example 3: Write a C++ program that calculates the value of:
13
Example 3:
Write C++ program to perform the above equation:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x;
cout << "Enter the value x: ";
cin >> x;
double f = sqrt(sin(x) * cos(x)) + log(pow(x, 2));
cout << "The value f= " << f << endl;
return 0;
}
14