Lecture # 03
ABDUL RAUF
Operator
 An operator is a symbol, which causes the
compiler to take an action.
Arithmetic Operator
 The arithmetic operators are used to perform
arithmetic operation.
 The five arithmetic operator supported by c++
includes:
Arithmetic Operator & Operator Precedence
Order of Precedence
Relational Operators
 Relational operators are used to form the
relational expression.
 A relational expression is the expression
whose result can either be true or false.
Relational Operators
Logical Operator ( &&, || )
Logical Operators are used to combine two or
more relational expression
Unary Operator
 Unary operator is an operator that operates on
a single variable or operand.
 Increase and decrease (++, --)
 Post Increment a++ ;
 Pre Increment ++a ;
 Post Decrement a-- ;
 Pre Decrement –a ;
#include <iostream>
using namespace std;
int main(){
int c;
// demonstrate postincrement
c = 5; // assign 5 to c
cout << c << endl; // print 5
cout << c++ << endl; // print 5 then postincrement
cout << c << endl; // print 6
cout << endl; // skip a line
// demonstrate preincrement
c = 5; // assign 5 to c
cout << c << endl; // print 5
cout << ++c << endl; // preincrement then print 6
cout << c << endl; // print 6
return 0; // indicate successful termination
} // end main
Assignment (=) Operator
 The assignment operator assigns a value to a
variable.
Compound assignment
 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:
Compound Assignment
Conditional Operator (?:)
 The conditional operator evaluates an
expression returning a value if that expression
is true and a different one if the expression is
evaluated as false. Its format is:
 condition ? result1 : result2;
 If condition is true the expression will return
result1, if it is not it will return result2.
// conditional operator
#include <iostream>
using namespace std;
int main ()
{
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout << c;
return 0;
}
Comma Operator
 The comma operator is used to put more than
one expression, separated by commas, on a
single
 int x,y;
sizeof operator
 The sizeof operator is a unary operator that
returns the size, in bytes, of its argument.
 sizeof (int);
Any Question
Thanks

Lecture 3 programming fundaments by abdul rauf.pdf

  • 1.
  • 2.
    Operator  An operatoris a symbol, which causes the compiler to take an action. Arithmetic Operator  The arithmetic operators are used to perform arithmetic operation.  The five arithmetic operator supported by c++ includes:
  • 3.
    Arithmetic Operator &Operator Precedence
  • 4.
  • 5.
    Relational Operators  Relationaloperators are used to form the relational expression.  A relational expression is the expression whose result can either be true or false.
  • 6.
  • 7.
    Logical Operator (&&, || ) Logical Operators are used to combine two or more relational expression
  • 8.
    Unary Operator  Unaryoperator is an operator that operates on a single variable or operand.  Increase and decrease (++, --)  Post Increment a++ ;  Pre Increment ++a ;  Post Decrement a-- ;  Pre Decrement –a ;
  • 9.
    #include <iostream> using namespacestd; int main(){ int c; // demonstrate postincrement c = 5; // assign 5 to c cout << c << endl; // print 5 cout << c++ << endl; // print 5 then postincrement cout << c << endl; // print 6 cout << endl; // skip a line // demonstrate preincrement c = 5; // assign 5 to c cout << c << endl; // print 5 cout << ++c << endl; // preincrement then print 6 cout << c << endl; // print 6 return 0; // indicate successful termination } // end main
  • 10.
    Assignment (=) Operator The assignment operator assigns a value to a variable. Compound assignment  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:
  • 11.
  • 12.
    Conditional Operator (?:) The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is:  condition ? result1 : result2;  If condition is true the expression will return result1, if it is not it will return result2.
  • 13.
    // conditional operator #include<iostream> using namespace std; int main () { int a,b,c; a=2; b=7; c = (a>b) ? a : b; cout << c; return 0; }
  • 14.
    Comma Operator  Thecomma operator is used to put more than one expression, separated by commas, on a single  int x,y; sizeof operator  The sizeof operator is a unary operator that returns the size, in bytes, of its argument.  sizeof (int);
  • 15.
  • 16.