Programming in C++ (I) 2020-2021
Programming in C++ (I) 2020-2021
2020-2021
ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ /ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ
ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ
ﻡ.ﻡ .ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ
Lec1 Programming in C++ (I) Sura Abed Sarab Hussien
Lecture 1
[ ] array subscriber
int num[5+3]; Declaration statement :[Comment [DS21
In memory declares an array with name num of
eight elements of integer type.
int num[8];
Another declaration :[Comment [DS22
statement.
int i = 4;
list[2 * i - 3] = 58;
list[5] = 58;
stores 58 in list[5] because 2 * i - 3 evaluates to 5. The index expression
is evaluated first, giving the position of the component in the array.
Lecture 2
The following for loop steps through each element of the array list,
starting at the first element of list:
for (i = 0; i < 100; i++) //Line 1
//process list[i] //Line 2
main()
{
int a[100]; Declaration statement. :[Comment [DS21
It declares an array with 100 elements.
Each element is of type integer.
cin >> a[0]; The name of the array is a.
.
cin >> a[99];
}
main()
{
int a[100]; Declaration statement. :[Comment [DS22
It declares an array with 100 elements.
Each element is of type integer.
for(int i = 0; i < 100; i++) The name of the array is a.
{
cout << "please, enter an integer number" << endl;
cin >> a[i];
}
}
main()
{
int a[100]; Declaration statement. :[Comment [DS23
It declares an array with 100 elements.
Each element is of type integer.
for(int i = 0; i < 100; i++) The name of the array is a.
main()
{
int a[100];
for(int i = 0; i <= 99; i++)
cin >> a[i];
}
main()
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien
{
int a[100];
for(int i = 99; i >= 0; i--)
cin >> a[i];
}
main()
{
int a[100];
for(int i = 0; i < 100; i++)
cin >> a[i]; Loop for reading (reading :[Comment [DS24
loop)
int sum = 0;
for(int i = 0; i < n; i++)
sum = sum + a[i];
cout << sum;
int sum_e = 0;
int c_e = 0;
for(int i = 0; i < n; i++)
if(a[i]%2 == 0){
c_e++;
sum_e = sum_e + a[i];
}
cout << sum_e << endl;
cout << sum_e / c_e << endl;
int max = a[0];
for(int i = 1; i < n; i++)
if(a[i] > max)
max = a[i];
cout << max << endl;
int max = -9999;
for(int i = 0; i < n; i++)
if(a[i]%2 == 0 && a[i] > max)
max = a[i];
cout << max << endl;
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien
If processing the list requires inputting data into list, the statement in
Line 2 takes the form of an input statement, such as the cin
statement. For example, the
following statements read 100 numbers from the keyboard and store
the numbers in list:
main()
{
int list[100];
int i;
for (i = 0; i < 100; i++)
cin >> list[i];
}
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[5] = {1, 2, 3, 4, 5};
int i;
for(i=0; i<5; i++)
{
cout<< arr[i] << endl;
}
getch();
}
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10];
int i;
int sum=0, avg=0;
cout <<"Enter 10 array elements: ";
for(i=0; i < 10; i++)
{
cin >> arr[i];
sum = sum + arr[i];
}
cout<<"\nThe array elements are: \n";
for(i=0; i < 10; i++)
{
cout << arr[i] <<" ";
}
cout<<"\n\nSum of all elements is: " << sum << endl;
avg = sum/10;
cout<<"And average is: "<<avg;
}
Here is the sample run of the above C++ program:
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien
)Programming in C++(I
2020-2021
ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ /ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ
ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ
ﻡ.ﻡ .ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ
Lec3 Programming in C++ (I) Sura Abed Sarab Hussien
Lecture 3
wherein intExp1 and intExp2 are constant expressions yielding positive integer
values. The two expressions, intExp1 and intExp2, specify the number of rows and
the number of columns, respectively, in the array.
The statement:
double sales[10][5];
declares a two-dimensional array sales of 10 rows and 5 columns, in which every
component is of type double. As in the case of a one-dimensional array, the rows are
numbered 0. . .9 and the columns are numbered 0. . .4:
To access the components of a two-dimensional array, you need a pair of indices: one
for the row position and one for the column position.
Lec3 Programming in C++ (I) Sura Abed Sarab Hussien
{20, 4, 7},
{11, 18, 14}}; Initialization of the 4th row :[Comment [DS23
This statement declares board to be a two-dimensional array of four rows and three
columns. The components of the first row are 2, 3, and 1; the components of the
second row are 15, 25, and 13; the components of the third row are 20, 4, and 7; and
the components of the fourth row are 11, 18, and 14, respectively.
Lec3 Programming in C++ (I) Sura Abed Sarab Hussien
cout<<”please, enter a mark for student” << i <<endl; Inner loop for the columns of :[Comment [DS26
the matrix
cin>> marks[i][j]; You need a nested loop to :[Comment [DS27
visit all the rows and all the columns in the matrix.
} // end for j
LCV :[Comment [DS28
} // end main This LCV points to the rows :[Comment [DS29
of the matrix marks
In memory:
This LCV points to the :[Comment [DS210
i=0 1 2 3 columns of the matrix marks
j=0 1 2 3 0 1 2 3 0 1 2 3
marks
marks[0][0]=90 marks[0][1]=85 marks[0][2]=95
marks[1][0]=60 marks[1][1]=85 marks[1][2]=70
marks[2][0]=93 marks[2][1]=90 marks[2][2]=80
80
)Programming in C++(I
2020-2021
ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ /ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ
ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ
ﻡ.ﻡ .ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ
Lec4 Programming in C++ (I) Sura Abed Sarab Hussien
Lecture 4
Questions:
Write a C++ program to read a 2D array of 𝒏 × 𝒏 integer numbers. Then, find
the following:
1. The sum of all even elements.
2. The largest element in the matrix.
3. The smallest element in each row.
4. The sum of the smallest odd element in each column.
5. Print the elements of the main diagonal.
6. Print the elements of the minor (secondary) diagonal.
7. Swap between the elements of the main diagonal and the minor
diagonal.
8. Print the elements of the left triangle of the main diagonal.
9. The sum of the elements of the right triangle of the main diagonal.
10. The sum of the elements of the right triangle of the minor diagonal.
11. Print even elements of odd rows.
12. Print odd elements located at the even columns.
13. Print the index of the largest even number in the matrix.
14. Copy the factorial of each element in a second matrix. What is the size
of this matrix?
15. Print the elements of each row in a distinct line.
16. Print the elements of each column in a distinct line.
main(){
const int n = 5;
int a[n][n];
int i,j;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
cin>> a[i][j]; For reading the elements of :[Comment [DS21
the matrix. Visiting the elements row by row, and
// Answer for (1) column followed by column.
int sum = 0;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(a[i][j]%2 == 0)
sum = sum + a[i][j];// sum += a[i][j];
cout<< sum<<endl;
// Answer for (2)
Lec4 Programming in C++ (I) Sura Abed Sarab Hussien
if(i <= j)
sum_rt + = a[i][j];
cout<< sum_rt << endl;
//answer for 10
int sum_rs = 0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if( j >= n-1-i)
sum_rs + = a[i][j];
cout<< sum_rs << endl;
//answer for 11
//method 1: visiting all rows and check for odd row
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(i%2 == 1 && a[i][j]%2 == 0)
cout<< a[i][j];
i_index = i;
j_index = j;
}
cout<< “the row index of the largest even is:” << i_index<<end;
cout<< “the column index of the largest even is:” << j_index<<end;
Lec4 Programming in C++ (I) Sura Abed Sarab Hussien
// answer for 14
int a_fact[n][n];
int fact, k;
for(i=0; i<n; i++)
for(j=0; j<n; j++){
fact = 1;
for(k=1; k<= a[i][j]; k++)
fact = fact * k;
a_fact[i][j] = fact;
cout<< a_fact[i][j];
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
cout<< a_fact[i][j]<<endln;
//answer for 15
for(i=0; i<n; i++){
for(j=0; j<n; j++)
cout<<a[i][j] << “ “;
cout<<endl;
}
//answer for 16
for(i=0; i<n; i++){
for(j=0; j<n; j++){
cout<<a[j][i] << “ “;
} First statement :[Comment [DS218
cout<<endl; Second statement :[Comment [DS219
}
}//end of main
i=0 1 2
S S
j=0 1 2 3 4 5 0 1 2 3 4
S S 5
[0][0] [0][1] [0][2] [0][3] [0][4] All elements in cyan color :[Comment [DS232
are located at the right triangle of the main diagonal
[1][0] [1][1] [1][2] [1][3] [1][4]
[2][0] [2][1] [2][2] [2][3] [2][4]
[3][0] [3][1] [3][2] [3][3] [3][4]
[4][0] [4][1] [4][2] [4][3] [4][4]
[0][0] [0][1] [0][2] [0][3] [0][4] [0][5] The indices of the elements :[Comment [DS233
of the main diagonal
i=j
[1][1] [1][2] [1][3] [1][4] j>=i && i+j <= n-1 :[Comment [DS234
The indices of the elements :[Comment [DS235
of the main diagonal
[2][2] [2][3] i+j=n-1
Lecture 5
Testing: tracing
x[0]=9 x[1]=2 x[2]=19 x[3]=1 x[4]=30 x[5]=6 x[6]=99
x[7]=22 x[8]=33 x[9]=44
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9
main( )
{
const int n = 10;
int x[n];
int i;
int large_odd = -99999;
int index_large_odd = -1;
int small_even = 10000000;
int index_small_even = -1;
for(i = 0; i < n; i++){ // i is the LCV of the first loop
cout << "please, enter an integer number" << endl;
cin >> x[i]; // index of the array x
}
for(i = 0; i < n; i++)// i is the LCV of the second loop
(question 1)
if(x[i]%2 == 1)
cout << x[i] << endl;
for(i = 0; i < n; i++)
// (question 2)
if(x[i]%2 == 0 && x[i] > 10)
cout << x[i] << endl;
for(i = 0; i < n; i++)
// (question 3)
if(x[i] %2 == 1 && x[i] > large_odd){
large_odd = x[i];
index_large_odd = i;
Lec.5 Programming in C++ (I) Sura Abed Sarab Hussien
}
Lec.5 Programming in C++ (I) Sura Abed Sarab Hussien
cout << index_large_odd << endl;
for(i = 0; i < n; i++) // (question 4)
if(x[i] %2 == 0 && x[i] < small_even){
small_even = x[i];
index_small_even = i;
}c
out << index_small_even << endl;
// question 5
x[index_small_even] = large_odd;
x[index_large_odd] = small_even;
}
)Programming in C++(I
2020-2021
ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ /ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ
ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ
ﻡ.ﻡ .ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ
Lec.6 Programming in C++ (I) Sura Abed Sarab Hussien
Lecture 6
• Editor
• main function
• begin
• end
• variable
• assignement operator
• constant
• integer
• mathematical expression
• semicolon
• output operator
• string
• endl
Lec.6 Programming in C++ (I) Sura Abed Sarab Hussien
The following steps, as shown in the above figure are necessary to process a C++
program.
1. You use a text editor to create a C++ program following the rules, or syntax, of the
high-level language. This program is called the source code, or source program. The
program must be saved in a text file that has the extension .cpp.
2. The C++ program contains the statement #include <iostream>. In a C++ program,
statements that begin with the symbol # are called preprocessor directives. These
3. After processing preprocessor directives, the next step is to verify that the program
obeys the rules of the programming language — that is, the program is syntactically
correct—and translate the program into the equivalent machine language. The
compiler checks the source program for syntax errors and, if no error is found,
translates the program into the equivalent machine language. The equivalent machine
4. The programs that you write in a high-level language are developed using an
integrated development environment (IDE). The IDE contains many programs that are
useful in creating your program. This prewritten code (program) resides in a place
called the library. A program called a linker combines the object program with the
programs from libraries. Linker: A program that combines the object program with
other programs in the library and is used in the program to create the executable code.
5. You must next load the executable program into main memory for execution. A
program called a loader accomplishes this task. Loader: A program that loads an
#include <iostream>
using namespace std;
int main() {
int main() {
return 0;
}
int main() {
cout << "This is a C++ Programming"; Output screen :[Comment [DS25
#include <iostream>
using namespace std;
int main() {
cout << "This is a C++ Programming lesson"<< endl <<"This is Output operator :[Comment [DS210
Output
#include <iostream>
using namespace std;
int main() {
cout << "This is a C++ Programming"<< endl <<"This is the second Output operator :[Comment [DS215
Lecture 7
#include <iostream>
using namespace std;
int main() {
int x; integer :[Comment [DS21
Output
70
256.783
character: z
Notes:
• The endl manipulator is used to insert a new line. That's why each output is displayed
in a new line.
• The << operator can be used more than once if we want to print different variables,
strings and so on in a single statement. For example:
#include <iostream>
using namespace std;
int main()
{
int num;
num = 6;
cout << "My first C++ program." << endl;
cout << "The sum of 2 and 3 = " << 5 << endl;
cout << "7 + 8 = " << 7 + 8 << endl;
cout << "Num = " << num << endl;
return 0;
}
Output
Tips:
When there is no two quotations (also called double quotes denoted by the symbol"),
the output should be either:
a number,
a character,
a mathematical expression, or
a declared variable.
Anything in double quotes is a string.
For example, cout << "The sum of 2 and 3 = " << 5 << endl;
This output statement consists of two expressions. The first expression (after the first
<<) is "The sum of 2 and 3 = " and the second expression (after the second <<)
consists of the number 5. The expression "The sum of 2 and 3 = " is a string and
Lec.7 Programming in C++ (I) Sura Abed Sarab Hussien
evaluates to itself. (Notice the space after =.) The second expression, which consists
of the number 5 evaluates to 5. Thus, the output of the preceding statement is: The
sum of 2 and 3 = 5
Another example, "My first C++ program." and "7 + 8 = " are strings. Typically, a
string evaluates to itself.
Arithmetic expressions are evaluated according to rules of arithmetic operations,
which you typically learn in an algebra course.
Let us now consider the following statement.
cout << "7 + 8 = " << 7 + 8 << endl;
In this output statement, the expression "7 + 8 = ", which is a string, evaluates to
itself.
Let us consider the second expression, 7 + 8. This expression consists of the numbers
7 and 8 and the C++ arithmetic operator +. Therefore, the result of the expression 7 +
8 is the sum of 7 and 8, which is 15. Thus, the output of the preceding statement is:
7 + 8 = 15
Finally, consider the statement:
cout << "Num = " << num << endl;
This statement consists of the string "Num = ", which evaluates to itself, and the word
num. The statement num = 6; assigns the value 6 to num. Therefore, the expression
num, after the second <<, evaluates to 6. It now follows that the output of the previous
statement is:
Num = 6
)Programming in C++(I
2020-2021
ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ /ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ
ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ
ﻡ.ﻡ .ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ
Lec.8 Programming in C++ (I) Sura Abed Sarab Hussien
Lecture 8
• Input statement
• Input operator
• Mathematical expression
• Arithmetic operators
• Operator precedence
#include <iostream>
using namespace std;
int main()
{
int num; Declaration statement :[Comment [DS21
cout << "My first C++ program." << endl; Output operator :[Comment [DS24
cout << "The sum of 2 and 3 = " << 5 << endl; Output operator :[Comment [DS25
cout << "7 + 8 = " << 7+8 << endl; Output state,ment :[Comment [DS26
cout << "Num = " << num - 2 << endl; Constant: integer :[Comment [DS27
keyword :[Comment [DS28
return 0;
mathematical expression :[Comment [DS29
}
variable :[Comment [DS210
Body of the program (body :[Comment [DS211
of the main function)
Output
20T
#include <iostream>
using namespace std;
int main()
{
Mathematical expression :[Comment [DS212
Int num = 10 +2;
Declaration statement? :[Comment [DS213
cout << "My 2nd C++ program."<<endl; Assignment statement?
cout << endl<<"Result is: num = " << num *2 << endl; Assignment and declaration statement?
Declaration and assignment statement?
return 0; Output statement?
}
Mathematical expression :[Comment [DS214
Lec.8 Programming in C++ (I) Sura Abed Sarab Hussien
Output
My 2nd C++ program.
C++ Input
In C++, cin takes formatted input from standard input devices such as the keyboard.
We use the cin along with the >> operator for taking input. The syntax of an input
statement using cin and the extraction operator >> is:
#include <iostream>
using namespace std;
int main() {
int x1;
cout << "Enter an integer: ";
cin >> x1; Input operator :[Comment [DS215
cout << "The number is: " << x1; Input statement :[Comment [DS216
Output
Enter an integer: 15
#include <iostream>
using namespace std;
int main() {
int x1;
cout << "Enter an integer: ";
cin >> x1;
cout << "The number is: " << x1;
return 0;
}
#include <iostream>
using namespace std;
int main() {
char a;
int num;
return 0;
}
Output
The following statements show how the extraction operator >> works.
Lecture 9
Mathematical expressions and mathematical operators:
One of the most important uses of a computer is its ability to calculate. You can use
the standard arithmetic operators to manipulate integral and floating-point data types.
There are five arithmetic operators.
You can use the operators +, -, *, and / with both integral and floating-point data
types. You use % with only the integral data type to find the remainder in ordinary
division. Consider the following:
-5
8-7
3+4
2 + 3 *5
5.6 + 6.2 *3
x + 2 *5 + 6 / y3
#include <iostream>
using namespace std;
int main()
{
cout << "2 + 5 = " << 2 + 5 << endl;
cout << "13 + 89 = " << 13 + 89 << endl;
cout << "34 - 20 = " << 34 - 20 << endl;
cout << "45 - 90 = " << 45 - 90 << endl;
cout << "2 * 7 = " << 2 * 7 << endl;
cout << "5 / 2 = " << 5 / 2 << endl;
cout << "14 / 7 = " << 14 / 7 << endl;
cout << "34 % 5 = " << 34 % 5 << endl;
cout << "4 % 6 = " << 4 % 6 << endl;
return 0;
}
2+5=7
13 + 89 = 102
34 - 20 = 14
45 - 90 = -45
5/2=2
14 / 7 = 2
34 % 5 = 4
4%6=4
Order of Precedence
When more than one arithmetic operator is used in an expression, C++ uses the
operator precedence rules to evaluate the expression. According to the order of
precedence rules for arithmetic operators,
*, /, %
are at a higher level of precedence than:
+, -
Note that the operators *, /, and % have the same level of precedence. Similarly, the
operators + and - have the same level of precedence. When operators have the same
level of precedence, the operations are performed from left to right. To avoid
confusion, you can use parentheses to group arithmetic expressions. For example,
using the order of precedence rules,
3*7-6+2*5/4+6
means the following:
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6
= ((21 – 6) + (10 / 4)) + 6 (Evaluate *)
= ((21 – 6) + 2) + 6 (Evaluate /. Note that this is an integer division.)
Lec.9 Programming in C++ (I) Sura Abed Sarab Hussien
= (15 + 2) + 6 (Evaluate –)
= 17 + 6 (Evaluate first +)
= 23 (Evaluate +)
Note that the use of parentheses in the second example clarifies the order of
precedence. You can also use parentheses to override the order of precedence rules
In the expression:
3+4*5
* is evaluated before +. Therefore, the result of this expression is 23. On the other
hand,
in the expression: (3 + 4) * 5
+ is evaluated before * and the result of this expression is 35. Because arithmetic
operators are evaluated from left to right, unless parentheses are present, the
associativity of the arithmetic operators is said to be from left to right.
Increment and decrement operators each have two forms, pre and post. The syntax of
the increment operator is:
Pre-increment: ++variable
Post-increment: variable++
The syntax of the decrement operator is:
Pre-decrement: – –variable
Lec.9 Programming in C++ (I) Sura Abed Sarab Hussien
Post-decrement: variable– –
The statement:
++count;
or:
count++;
increments the value of count by 1.
Similarly, the statement:
––count;
or:
count––;
decrements the value of count by 1.
What is the difference between the pre and post forms of these operators?
The difference becomes apparent when the variable using these operators is employed
in an expression. Suppose that x is an int variable. If ++x is used in an expression,
first the value of x is incremented by 1, and then the new value of x is used to evaluate
the expression. On the other hand, if x++ is used in an expression, first the current
value of x is used in the expression, and then the value of x is incremented by 1. The
following example clarifies the difference between the pre- and post-increment
operators.
Suppose that x and y are int variables. Consider the following statements:
x = 5;
y = ++x;
The first statement assigns the value 5 to x. To evaluate the second statement, which
uses the pre-increment operator, first the value of x is incremented to 6, and then this
value, 6, is assigned to y. After the second statement executes, both x and y have the
value 6.
Example
#include<iostream>
main() {
int x, y, z;
x = 10;
y = 10;
cout << "Z: " << z << " and y is: " << y << endl;
Output
Z: 11
Z: 10 and y is: 11
)Programming in C++(I
2020-2021
ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ /ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ
ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ
ﻡ.ﻡ .ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ
Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien
Lecture 10
#include <iostream>
using namespace std;
int main() {
char a;
int num;
return 0;
}
Output
Memory: :[Comment [DS22
a='Z'
Enter a character and an integer: Z 40 num=40
Character: Z
Number: 80
The following statements show how the extraction operator >> works.
You can use the operators +, -, *, and / with both integral and floating-point data
types. You use % with only the integral data type to find the remainder in ordinary
division. Consider the following:
int x =8;
-5 negation operator :[Comment [DS23
unary operator
8-7
left operand :[Comment [DS24
3+4
subtraction operator :[Comment [DS25
2 + 3 *5
right operand :[Comment [DS26
5.6 + 6.2 *3
x + 2 *5 + 6 / 2 10 :[Comment [DS27
2nd operator :[Comment [DS28
+27
In this expression, the operator + indicates that the number 27 is positive. Here, + has
only one operand and so acts as a unary operator. Both - and + are both unary and
binary arithmetic operators. However, as arithmetic operators, *, /, and % are binary
and so must have two operands.
#include <iostream>
using namespace std;
int main()
{
cout << "2 + 5 = " << 2 + 5 << endl;
cout << "13 + 89 = " << 13 + 89 << endl;
cout << "34 - 20 = " << 34 - 20 << endl;
cout << "45 - 90 = " << 45 - 90 << endl;
cout << "2 * 7 = " << 2 * 7 << endl;
cout << "5 / 2 = " << 5 / 2 << endl;
cout << "14 / 7 = " << 14 / 7 << endl;
cout << "34 % 5 = " << 34 % 5 << endl;
cout << "4 % 6 = " << 4 % 6 << endl;
return 0;
}
2+5=7
13 + 89 = 102
34 - 20 = 14
45 - 90 = -45
5/2=2
14 / 7 = 2
34 % 5 = 4
4%6=4
Order of Precedence
When more than one arithmetic operator is used in an expression, C++ uses the
operator precedence rules to evaluate the expression. According to the order of
precedence rules for arithmetic operators,
*, /, %
are at a higher level of precedence than:
+, -
Note that the operators *, /, and % have the same level of precedence. Similarly, the
Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien
operators + and - have the same level of precedence. When operators have the same
level of precedence, the operations are performed from left to right. To avoid
confusion, you can use parentheses to group arithmetic expressions. For example,
using the order of precedence rules,
3*7-6+2*5/2+6
(3*7) - 6 + 2 * 5 / 2 + 6
21- 6 + 2 * 5 / 2 + 6
21- 6 + (2 * 5) / 2 + 6
21- 6 + 10 / 2 + 6
21- 6 + (10 / 2) + 6
21- 6 + 5 + 6
(21- 6) + 5 + 6
15 + 5 + 6
(15 + 5) + 6
20 + 6
26
3*7-6+2*5/4+6
Note that the use of parentheses in the second example clarifies the order of
precedence. You can also use parentheses to override the order of precedence rules
In the expression:
3+4*5
Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien
* is evaluated before +. Therefore, the result of this expression is 23. On the other
hand,
in the expression: (3 + 4) * 5
+ is evaluated before * and the result of this expression is 35. Because arithmetic
operators are evaluated from left to right, unless parentheses are present, the
associativity of the arithmetic operators is said to be from left to right.
int count = 0;
count++; ------- count = count + 1; Increment operator by one :[Comment [DS213
int count = 0;
++count; ------- count = count + 1; Increment operator by one :[Comment [DS214
int x = 10;
x++; x ---- 11 Post increment :[Comment [DS215
|
|
|
V
int x = 10;
x = x + 1; X=11 :[Comment [DS217
cout<<i<<endl;
i++; i=7 :[Comment [DS221
cout<<i<<endl;
cout<<i<<endl;
--i; i=3 :[Comment [DS224
cout<<i<<endl;
cout<<i<<endl;
++i; i=5 :[Comment [DS227
cout<<i<<endl;
Increment and decrement operators each have two forms, pre and post. The syntax of
the increment operator is:
Pre-increment: ++variable
Post-increment: variable++
Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien