Introduction to Computing &
Programming
Lecture 24, 25 & 26
Engr. Beenish Adeel
Topics to be Discussed…
• Operators
• Types of Operators
Operators
• Operators are the symbols that cause a program to
do something on the variables.
• Following are the operators which are commonly
used in C++
– Assignment
– Arithmetic
– Compound
– Increment and Decrement
– Relational & Equality
– Logical
1. Assignment Operator (=)
• The assignment
operator assigns a
value to a variable.
a = 5;
• This statement
assigns the integer
value 5 to the variable
a.
#include <iostream>
using namespace std;
int main ()
{
int a, b
a = 10;
b = 4;
a = b;
b = 7;
cout << "a:“<<a<<endl;
cout << " b:“<<b<<endl;
return 0;
}
2. Arithmetic Operators (+,-,*,/,%)
+ Addition
- Subtraction
* multiplication
/ Division
% Modulo
• The five arithmetical operations supported by the
C++ language are:
3. Compound assignment (+=, -=, *=, /=, %=,
>>=, <<=, &=, ^=, |=)
expression is equivalent to
value += increase; value = value + increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
price *= units + 1; price = price * (units + 1);
• When we want to modify the value of a variable by
performing an operation on the value currently stored in that
variable we can use compound assignment operators:
#include <iostream>
using namespace std;
int main ()
{
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout << “The Value of a is: “<<a<<endl;
return 0;
}
Compound Operator- Example
• Shortening even more some expressions, the increase
operator (++) and the decrease operator (--) increase or
reduce by one the value stored in a variable. They are
equivalent to +=1 and to -=1, respectively. Thus:
1. c++
2. c= c+1;
3. c +=1;
• are all equivalent in its functionality: the three of them
increase by one the value of c.
4. Increment and Decrement (++, --)
Example 1 Example 2
B=3;
A=++B;
// A contains 4, B contains 4
B=3;
A=B++;
// A contains 3, B contains 4
5. Relational and equality operators
• In order to evaluate a comparison between two expressions we can use
the relational and equality operators. The result of a relational operation is
a Boolean value that can only be true or false, according to its Boolean
result.
• We may want to compare two expressions, for example, to know if they
are equal or if one is greater than the other is. Here is a list of the
relational and equality operators that can be used in C++:
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Answer the following
1. (7 == 5)
2. (5 > 4)
3. (3 != 2)
4. (6 >= 6)
5. (5 < 5)
Suppose that a=2, b=3 and c=6 then,
6. (a == 5)
7. (a*b >= c)
8. (2*3 >= 6)
9. (b+4 > a*c)
10. ((b=2) == a)
6. Logical operators ( !, &&, || )
1. !(5 == 5) //evaluates to false b/c the expression at its right is true
2. (5 == 5) // evaluates true
3. !(6 <= 4 ) // evaluates to true because (6 <= 4) would be false.
4. !true // evaluates to false
5. !false // evaluates to true.
1. NOT Operator (!)
The Operator ! is the C++ operator to perform the Boolean operation NOT, it
has only one operand, located at its right, and the only thing that it does is to
inverse the value of it, producing false if its operand is true and true if its
operand is false. Basically, it returns the opposite Boolean value of evaluating
its operand. For example:
Logical operators ( !, &&, || )
2. && OPERATOR
– The logical operators && and || are used when evaluating two expressions to
obtain a single relational result. The operator && corresponds with Boolean
logical operation AND. This operation results true if both its two operands are
true, and false otherwise. The following panel shows the result of operator &&
evaluating the expression a && b:
OR
a b a && b
False False False
False True False
True False False
True True True
a b a && b
0 0 0
0 1 0
1 0 0
1 1 1
Logical operators ( !, &&, || )
3. || OPERATOR
– The operator || corresponds with Boolean logical operation OR. This
operation results true if either one of its two operands is true, thus being false
only when both operands are false themselves. Here are the possible results
of a || b:
OR
a b a || b
False False False
False True True
True False True
True True True
a b a || b
0 0 0
0 1 1
1 0 1
1 1 1
Exercise
1. WAP in which you have to take two variables
a and b, assign a=20, b=15 and perform the
following operations and print the results:
1. Addition
2. Subtraction
3. Division
4. Multiplication
5. Modulo
THANKS

ComputerProgrammingVarialblesanddeclaration.pptx

  • 1.
    Introduction to Computing& Programming Lecture 24, 25 & 26 Engr. Beenish Adeel
  • 2.
    Topics to beDiscussed… • Operators • Types of Operators
  • 3.
    Operators • Operators arethe symbols that cause a program to do something on the variables. • Following are the operators which are commonly used in C++ – Assignment – Arithmetic – Compound – Increment and Decrement – Relational & Equality – Logical
  • 4.
    1. Assignment Operator(=) • The assignment operator assigns a value to a variable. a = 5; • This statement assigns the integer value 5 to the variable a. #include <iostream> using namespace std; int main () { int a, b a = 10; b = 4; a = b; b = 7; cout << "a:“<<a<<endl; cout << " b:“<<b<<endl; return 0; }
  • 5.
    2. Arithmetic Operators(+,-,*,/,%) + Addition - Subtraction * multiplication / Division % Modulo • The five arithmetical operations supported by the C++ language are:
  • 6.
    3. Compound assignment(+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) expression is equivalent to value += increase; value = value + increase; a -= 5; a = a - 5; a /= b; a = a / b; price *= units + 1; price = price * (units + 1); • When we want to modify the value of a variable by performing an operation on the value currently stored in that variable we can use compound assignment operators:
  • 7.
    #include <iostream> using namespacestd; int main () { int a, b=3; a = b; a+=2; // equivalent to a=a+2 cout << “The Value of a is: “<<a<<endl; return 0; } Compound Operator- Example
  • 8.
    • Shortening evenmore some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus: 1. c++ 2. c= c+1; 3. c +=1; • are all equivalent in its functionality: the three of them increase by one the value of c. 4. Increment and Decrement (++, --) Example 1 Example 2 B=3; A=++B; // A contains 4, B contains 4 B=3; A=B++; // A contains 3, B contains 4
  • 9.
    5. Relational andequality operators • In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result. • We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is. Here is a list of the relational and equality operators that can be used in C++: == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to
  • 10.
    Answer the following 1.(7 == 5) 2. (5 > 4) 3. (3 != 2) 4. (6 >= 6) 5. (5 < 5) Suppose that a=2, b=3 and c=6 then, 6. (a == 5) 7. (a*b >= c) 8. (2*3 >= 6) 9. (b+4 > a*c) 10. ((b=2) == a)
  • 11.
    6. Logical operators( !, &&, || ) 1. !(5 == 5) //evaluates to false b/c the expression at its right is true 2. (5 == 5) // evaluates true 3. !(6 <= 4 ) // evaluates to true because (6 <= 4) would be false. 4. !true // evaluates to false 5. !false // evaluates to true. 1. NOT Operator (!) The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. For example:
  • 12.
    Logical operators (!, &&, || ) 2. && OPERATOR – The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds with Boolean logical operation AND. This operation results true if both its two operands are true, and false otherwise. The following panel shows the result of operator && evaluating the expression a && b: OR a b a && b False False False False True False True False False True True True a b a && b 0 0 0 0 1 0 1 0 0 1 1 1
  • 13.
    Logical operators (!, &&, || ) 3. || OPERATOR – The operator || corresponds with Boolean logical operation OR. This operation results true if either one of its two operands is true, thus being false only when both operands are false themselves. Here are the possible results of a || b: OR a b a || b False False False False True True True False True True True True a b a || b 0 0 0 0 1 1 1 0 1 1 1 1
  • 14.
    Exercise 1. WAP inwhich you have to take two variables a and b, assign a=20, b=15 and perform the following operations and print the results: 1. Addition 2. Subtraction 3. Division 4. Multiplication 5. Modulo
  • 15.