Monday, February 28, 2011

Fundamental of Programming - 4th Week

CHAPTER 4

OPERATORS & EXPRESSIONS


1. Arithmetic operators
a) UNARY
* plus ++
* minus --

b) BINARY
: addition +
: subtraction -
: multiplication *
: division /
: modulus (remainder)

c) INCREMENT & DECREMENT
: pre-increment --> ++m [ 1+m]
: post-increment --> m++ [ m+1]
: pre-decrement --> --m [1-m]
: post-decrement --> m-- [m-1]

2. Relational Operators

a) NOT EQUIVALENT GROUP
symbols operators
> greater than
>= greater than or equals to
< less than
<= less than or equals to

b)EQUIVALENT GROUP
symbols operators
== equal to
!= not equal to

3. Logical Operators
symbols operators
&& AND
|| OR
! NOT

OPERATOR COMPOUND
()
! ++ --
* / %
+ -
< <= > >=
== !=
&&
||
=

( HIGHEST PRIORITY: TOP to BELOW !!!!!! )



4. Assignment Operators
Expressions Meaning
a+b=b a=a+b
a*=5 a=a*5
a-+2 a=a-2
a%=3 a=a%3


EX 1:
a=2 b=3
m=2 n=3
p=2 q=3
x-2 y=3

++a + --b * 4 > ++m * --n + 4
1+2 + 3-1 * 4 > 2+1 * 3-1 + 4
3 + 2 * 4 > 3 * 2 + 4 [settle *]
3 + 8 > 6 + 4
11 > 10
= TRUE/ 1

EX 2:
++a + --b * 4 > ++m * --n + 4 && p++ + q-- * 4 < x++ * y-- + 4;
(1+2) + (3-1) * 4 > (1+2) * (3-1) + 4 && 2 + 3 * 4 < 2 * 3 + 4
3 + 2 * 4 > 3 * 2 + 4 && 2 + 12 < 5 + 4
3 + 8 > 6 + 4 && 14 < 9
11 > 10 && 14 < 9
1 && 0
= FALSE/0

PRE! n=5
n --> 5
++n --> 1+5=6
n=6

POST!
n --> 5
n++ -->> 5
n --> 6


EXAMPLE 1:
#include
main()
{
int n;
n=5;
//pre-increment
cout<cout<<++n;
cout<//post-increment
cout<cout<cout<return 0;
}

OUTPUT
5
6
6

5
5
6


EXAMPLE 2:
#include
main()
{
int m;
m=7;
//pre-decrement
cout<cout<<--m;
cout<//post-decrement
cout<cout<cout<return 0;
}

OUTPUT
7
6
6

7
7
6


EXAMPLE 3:

#include
main()
{
int result, a,b,m,n,p,q,x,y;
result=0;
a=6;
b=7;
m=8;
n=9;
p=3;
q=2;
x=4;
y=5;
result=++a + --b * 4 < ++m * --n + 4 || p++ + q-- * 4 > x++ y-- + 4;
cout<<"InResult="<return 0;
}

What is the output of result?
Result=True/1

No comments:

Post a Comment