Escolar Documentos
Profissional Documentos
Cultura Documentos
Arithmetic operator
Relational operator
Logical operators
Bitwise operators
Arithmetic operators
%for modulo
Question
Assignment (=)
Example
// assignment operator
#include <iostream>
int main ()
{ int a, b;
a = 10;
b = 4;
A=b
b = 7;
cout << "a:";
cout << a;
cout << " b:";
cout << b;
return 0; }
Compound Assignment
statement
An assignment that assign a value to many
A=b=20
X=y=z=100
Compound assignment
Operator
Syntax
Variable op=expression
Example
N+=10 = N=N+1
Example
int main ()
{ int a, b=3;
a = b; a+=2; // equivalent to a=a+2
cout << a;
return 0; }
Example
Prefix
B=3;
A=--B;
// A contains 2, B contains 2
Postfix
B=3;
A=B--;
// A contains 3, B contains 2
Example
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
Example
7 == 5) // evaluates to false.
(5 > 4) // evaluates to true.
(3 != 2) // evaluates to true.
(6 >= 6) // evaluates to true.
(5 < 5) // evaluates to false.
Example 2
Be careful
Example
Example 2
For example:
Precedence of operators
Example:
10 * (24 / (5 - 2) ) + 13
10 * ( 24 / 3 ) + 13
10 * 8 + 13
80 + 13
93
Operator Associativity
Operator Associativity
(contd.)
Operators
()
++(postfix)
-(postfix)
Associativity
Left-to-right
Left-to-right
*
+
=
Left-to-right
Left-to-right
Right-to-left
+=
-=
*=
/=
Lvalue
Rvalue
Example
Example 2
Escape Sequences
Escape Sequence
\a
\b
\f
\n
\t
\
\
Purpose
Alarm
Backspace
Form feed
Carriage return
Tab
Single quote
Double quote
Type casting
(type) expression
Example
Float a,b
Int c;
A=10.3
B=5.2
c=(int)a % (int)b;
Cout<<c;
Result
Shows 0
Sizeof(operand)
Example
cout<<sizeof(int)
Result
comments
Basic Input/Output
Standard Input
Example
include <iostream>
int main ()
{ int i; cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n"; return
0; }
Lab work
C++ Manipulator
Endl
Setw
Showpoint
Endl manipulator
Example
Cout<<hello<<endl<<comsats
Constants
Literal constant
Symbolic constant
Literal Constant
int age = 19 ;
Integer constant
Floating point constant
Character constant
String constant
e.g. 87
e.g. 10.22F
e.g. A
e.g. Pakistan
Symbolic Constants
const Qualifier
e.g
Define Directive
e.g
# define identifier
value ;
# define
Pl
3.141593 ;
Expression
e.g
A+B;
Operands
Operator
Program
#include <iostream.h>
void main()
{
char ch1, ch2, sum;
ch1 = 2 ;
ch2 = 6 ;
sum = ch1 + ch2 ;
cout<<Sum =<<sum;
}
Output
104
Because ASCII values of 2 and6 are
50 and 54
Program
#include <iostream.h>
#incldue<conio.h>
void main()
{
clrscr();
short var1 = 32767;
cout << var1 << endl;
var1= var1 +1 ;
cout <<var1 << endl;
var1 = var1 - 1 ;
cout << var1 << endl;
getch();
}
Output
32767
- 32768
32767
Because range of short is -32768 to
32767.
Program
#include <iostream.h>
#incldue<conio.h>
#define PI 3.141
void main()
{
float r, area;
clrscr();
cout << Enter radius:;
cin>> r;
area = 2.0 * PI * r;
cout << Area= << area;
getch();
}
Output
Program
#include <iostream.h>
#incldue<conio.h>
void main()
{
clrscr();
int a,b;
a = 10;
b = 5;
cout << a+b =<< a+b << endl;
cout << a-b =<< a-b << endl;
cout << a*b =<< a*b << endl;
cout << a/b =<< a/b << endl;
cout << a%b =<< a%b << endl;
getch();
}
Output
a+b =15
a-b =5
a*b =50
a/b =2
a%b =0