Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

In the Name of

Allah
The Most Merciful and Compassionate the most
gracious and beneficent, Whose help and
guidance we always solicit at every step, at every
moment.
Programming Concepts

Shabir Ahmad

Gomal University Sub Campus Tank


Programing Fundamentals

 Arithmetic Operators
 Precedence of Operators
 Compound Assignment Statement
 Compound Assignment Expression
 Increment Operator(++)
 Decrement Operator(--)

3 By: Shabir Ahmad


Programing Fundamentals

 There are five arithmetic operators in C/C++.

Operator Name Purpose


+ Addition For addition
- Subtraction For subtraction
* Multiplication For Multiplication
/ Division For Division
% Remainder or To find the remainder of Division
modulus operator.
Operator

4 By: Shabir Ahmad


Programing Fundamentals

 If an expression has two or more operators, then there is a certain order


in which these operations are performed by the computer.
 This order is referred to as the order of precedence. The precedence order
is given below:
 All parentheses are evaluated first.
 *, /, % are performed first. These operators have the same level of
precedence. They are performed in left to right order as they are
encountered in an arithmetic expression. Further, if an expression
contains several like operations, evaluation also proceeds from left to
right.
 Addition and subtraction will be performed next. They also have same
level of precedence, so preference is given to the operator which occurs
first.
 Parentheses are used to change the priority order, because C/C++
evaluates expression in parentheses first. If parentheses are “nested”,
then evaluation begins with the innermost pairs and works outward.

5 By: Shabir Ahmad


Programing Fundamentals

1. 4+3*3-6 = 7
2. (4+3)*3-6 = 15
3. 5+5%3 = 7
4. 5%2*3 = 3
5. (8*5%3)*5+10 = 15
6. 105/5*5%3+2 = 2
7. -(-10/2)*2 = 10
8. 128%3%2 = 0

6 By: Shabir Ahmad


Programing Fundamentals

 The assignment statement can also be used to assign one value to


many variables.
 This type of assignment statement is called Compound Assignment
Statement.
 For Example:
 To assign an integer value 16 to two integer type variables x and y,
the compound assignment statement is written as:
 x=y=16;

7 By: Shabir Ahmad


Programing Fundamentals

 Problem Statement:
 Write a program to assign a value 6 to integer type variables x, y, a, b, c. Also
calculate the sum of the variables and print the result on the screen.
1. #include<iostream.h>
2. #include<conio.h>
3. main() OUT PUT
4. {
5. int x,y,a,b,c,s; The sum=30
6. x=y=a=b=c=6;
7. s=x+y+a+b+c;
8. cout<<“The sum=”<<s;
9. getch();
10. }

8 By: Shabir Ahmad


Programing Fundamentals

 The compound assignment expression is used to add, subtract,


multiply or divide a value to or from a variable without writing the
variable on either side of the assignment operator ‘=‘.
 In this assignment expression, the arithmetic operator is combined
with the assignment operator. Its syntax is:
 var op=expression;
 Var: represent the variable to which the value is to be added,
subtracted , multiplied or divided.
 Op: represents the arithmetic operator i.e. +,-,*,/ etc
 Expression: represents the arithmetic expression whose value is to
be assigned to the variable.

9 By: Shabir Ahmad


Programing Fundamentals

 For Example:
 To add 10 to the variable x that already has a value, the simple arithmetic
statement is written as:
 x= x + y; can be written as x += 10;
 Following are the examples of compound assignment expressions:
 x += 9; // same as x = x + 9;
 x -= 9; // same as x = x - 9;
 x *= 9; // same as x = x * 9;
 x /= 9; // same as x = x / 9;

10 By: Shabir Ahmad


Programing Fundamentals

 The compound assignment expression is used to add, subtract, multiply or


divide a value to or from a variable without writing the variable on either side
of the assignment operator ‘=‘.
 In this assignment expression, the arithmetic operator is combined with the
assignment operator. Its syntax is:
 var op=expression;
 Var: represent the variable to which the value is to be added, subtracted ,
multiplied or divided.
 Op: represents the arithmetic operator i.e. +,-,*,/ etc
 Expression: represents the arithmetic expression whose value is to be
assigned to the variable.
 For Example:
 To add 10 to the variable x that already has a value, the simple arithmetic
statement is written as:
 X= x + y; can be written as x += 10;

11 By: Shabir Ahmad


Programing Fundamentals

 Problem Statement:
 Write a program to assign 3 values to three integer type variables a ,b & c. Add
variables as a & b and multiply their sum to variable c.
1. #include<iostream.h>
2. #include<conio.h>
3. Main()
4. { OUT PUT
5. int a,b,c;
The result=45
6. a=6;
7. b=9;
8. c=3;
9. c *= a+b;
10. cout<<“The result =”<<c;
11. getch();
12. }

12 By: Shabir Ahmad


Programing Fundamentals

 The Increment Operator:


 The operator that is used to add 1 to the value of a variable is called
increment operator.
 It is represented by a double plus (++) sign.
 It is used to add 1 to the value of an integer variable.
 This operator can be used before or after the variable name.
 For Example:
 To add 1 to a value of variable abc, it is normally written as:
 abc = abc +1;
 By using increment operator ++ it is written as:
 abc++; or ++abc;

13 By: Shabir Ahmad


Programing Fundamentals

 The increment operator can be written either before or after the


variable.
 If it is written before the variable, it is known as prefixing.
 If it is written after the variable, it is known as post fixing.
 Prefix and postfix operators have different effects when they are
used in expressions.

14 By: Shabir Ahmad


Programing Fundamentals

 Problem Statement:
 Write a program to show the effect of increment operator
1. #include<iostream.h>
2. #include<conio.h>
3. main()
4. {
5. Int a, b,c,s; OUT PUT
6. a=1; Sum of prefixing =6
c=4
7. b=1;
8. c=3;
9. s =a+b+(++c);
10. cout<<“sum of prefixing=“<<s<<endl;
11. cout<<“c= ”<<c;
12. getch();
13. }

15 By: Shabir Ahmad


Programing Fundamentals

 Problem Statement:
 Write a program to show the effect of increment operator
1. #include<iostream.h>
2. #include<conio.h>
3. main()
4. {
5. Int a, b,c,p; OUT PUT
6. a=1; Sum of prefixing =5
c=4
7. b=1;
8. c=3;
9. p=a+b+(c++);
10. cout<<“sum of post fixing=”<<p<<endl;
11. cout<<“c= ”<<c;
12. getch();
13. }

16 By: Shabir Ahmad


Programing Fundamentals

 The Decrement Operator:


 The operator that is used to subtract 1 from the value of a variable
is called decrement operator.
 It is represented by a double minus (- -) sign.
 It is used to subtract 1 from the value of an integer variable.
 This operator can be used before or after the variable name.
 For Example:
 To subtract 1 from the value of variable abc, it is normally written
as:
 abc = abc - 1;
 By using decrement operator (- -) it is written as:
 abc- - ; or abc - -;

17 By: Shabir Ahmad


Programing Fundamentals

 The decrement operator can be written either before or after the


variable.
 If it is written before the variable, it is known as prefixing.
 If it is written after the variable, it is known as post fixing.
 Prefix and postfix operators have different effects when they are
used in expressions.

18 By: Shabir Ahmad


Programing Fundamentals

 Problem Statement:
 Write a program to show the effect of decrement operator
1. #include<iostream.h>
2. #include<conio.h>
3. main()
4. {
5. Int a, b,c,s; OUT PUT
6. a=8; Sum of prefixing =12
c=2
7. b=2;
8. c=3;
9. s =a+b+(--c);
10. cout<<“sum of prefixing=“<<s<<endl;
11. cout<<“c= ”<<c;
12. getch();
13. }

19 By: Shabir Ahmad


Programing Fundamentals

 Problem Statement:
 Write a program to show the effect of decrement operator
1. #include<iostream.h>
2. #include<conio.h>
3. main()
4. {
5. int a, b,c,p; OUT PUT
6. a=8; Sum of prefixing =13
c=2
7. b=2;
8. c=3;
9. p=a+b+(c--);
10. cout<<“sum of post fixing=”<<p<<endl;
11. cout<<“c= ”<<c;
12. getch();
13. }

20 By: Shabir Ahmad


End of Lecture # 05

Thanks
Questions?

You might also like