Sei sulla pagina 1di 49

Operators and Flow Control

Operators
A symbol that denotes an operation that
can be performed on some data

Arithmetic Binary

Logical

Arithmetic Unary

Bitwise

Relational

Conditional
Assignment

Arithmetic Binary
Assume the following declaration: int
a=10,b=20;
Operation

Operator

Example

Result

Addition

a+b

30

Subtraction

a-b

-10

Back to
School

Multiplication

a*b

200

20) 10( 0

Division

a/b

Modulo

10%20

10

0
10

Applicable only to integral types. The sign of the result is the


same as that of the numerator.
3

Arithmetic Unary
Sign Operator:

+ Example: +5, -5
Increment and Decrement by 1

++ - Pre-increment

y=++x; (x=x+1; and y=x;)


Post-increment

y=x++; (y=x; and x=x+1;)

Relational operators
Operators used to
compare two operands
(variables, constants or
expressions)
Characters can be
compared as well
These operators are
used extensively with
flow control statements.
Examples:
=b, a<=b

a!
5

Operator

Meaning

<

less than

>

greater than

<=

less than or
equal to

>=

greater than
or equal to

==

equal to

!=

not equal to

Logical Operators: &&

To be used when a statement or set of


statements are to be executed based on
whether two expressions are true
The expression on the RHS will be evaluated
only if the expression on the LHS is true.
Example: (x>10)&&(y<a)
Also called Short circuit and operator

Logical Operators: ||
To be used when a statement or set of
statements are to be executed based on
whether at least any one of two expressions are
true
The expression on the RHS will be evaluated
only if the expression on the LHS is false.
Example: (x>10)||(y<a)
Also called Short circuit or operator

Truth table for && and ||


expression 1 expression 2 exp1&&exp2

exp1||exp2

non-zero
number

non-zero
number

non-zero
number

non-zero
number

0 implies False and 1 (or any non-zero number) implies true


8

Logical operators
Logical not: !
to used when the truth value of an expression
is to be reversed
Example: !(x>10)

Bitwise operators
Assume the following declaration:
char a=1,b=3;
Operation

Operator

Example

Result

Bitwise AND

&

1&3

Bitwise OR

1|3

Bitwise XOR

1^3

1s complement

~1

-2

Back to School
0000001
0000101
&
0000001
0000001
| 0000101
0000101
0000001
0000101
^
0000100

10

~
Back to School

~1 00000001 (1s complement) Sign bit


1s complement 1111111110
How to find the decimal equivalent of 11111110?
Since the sign bit is 1 the number is negative.
To get the decimal value of ve number get
the 2s complement -( 1s complement + 1)
1s complement of 11111110 00000001
00000001
+
1
------------00000010 decimal value 2. Hence the number is -2.
11

Shift operators
<< : left shift
char a=64;
printf(%d,a<<1); 128

215 214

213 212

211

210

29

28

27

26

25

24

23

22

21

20

>> : right shift


char a=64;
printf(%d,a>>1); 32
215 214

213 212

211

210

29

0 0

28

27

26

25

24

23

22

21

20

12

Shifting ve numbers
Some implementation fill the vacant bits with 0s,
where as others fill with ones.
-52

15

214

213 212

211

210

29

28

27

26

25

24

23

22

21

20

-5>>2 = 213 -1
0

or -2
1

1
13

Other non-portable shifts


Shift bits are higher than the number of bits in the

integer unpredictable and not portable.


Shift bits are ve unpredictable and not portable.

14

Conditional operators
?:
Also known as ternary operators as they work on three
operands

If condition results in false value


execute expression 2
condition ? expression 1 : expression 2

If condition results in true value


execute expression 1
Example: z=x>y?10:15
15

Assignment operator
=
variable=expression;
Assigns the value of the expression to the variable
Examples: a=b, a=10, a=x+y, a=x++, a=++x

16

Compound Operators
+=

-=

*=

/=

%=

>>=

>>>=

<<=

&=

examples

expression

equivalent

x=x+1

x+=1

x=x-1

x-=1

x=x*(5+10)

x*=(5+10)

x=x/(5+10)

x/= (5+10)

x=x%20

x%=20
17

|= ^=

sizeof

Operator

Returns the number of bytes the operand

occupies in memory.
Example:
sizeof(float)
sizeof(2)
int x=9;
sizeof(x) ;
18

Predict the output

1. printf(%d, -5%-3);
2. printf("%d",10<20);
3. printf(%d,10&&20);
4. int i=10,j=20,k;
k=(i>j) && (j++>i);
printf("%d",j);
5. printf(%d,!-1);
19

Conversions
What happens when two variables of different data types are
used in an expression?
For example, what happens when a variable of type int is
added to a variable of type float?
When two operands are of different types, then the operand
that has smaller size is converted to the type of the operand
of larger size before the operation is performed. This is implicit
or widening conversion.

20

Implicit conversion
The implicit conversion in the direction indicated
happens automatically to one of the operands when
the operands of different types are used.
charintlong intfloatdouble
If one of the operands is signed, the other is
automatically converted to signed.
When a variable of type int is added to a variable of
type float, implicit type conversion takes place where
the variable of type int is promoted to a float before
the operation is done.
21

Type Casting
Implicit conversion is done by the compiler

automatically.
Sometimes we may need to change implicit behavior
In such cases we must explicitly request the

compiler by using a cast operator.


Cast operator: (datatype)

22

Example

int i=10, j=20;


float f;
f=i/j;
printf(%f, f); prints 0.0
But
f=(float)i/j; prints 0.500000
Casting i to float type.
23

Predict the output

char c=127;
char c1=c+2;
printf("%d",c1);

char c=127;
printf("%d",c+2);
24

Precedence
Operators
Associativity
unary
-------------------------------------------------------[] . () ->
left to right
! ~ ++ -- - + * (type) sizeof
right to left
* / %
left to right
+ left to right
>> <<
left to right
< > <= >=
left to right
== !=
left to right
&
left to right
^
left to right
|
left to right
&&
left to right
||
left to right
?:
left to right
25
= += -= *= /= >>= <<= >>>= &= ^= |=
left to
right

Control Statements
Statements that enable us to order the
sequence of flow of a program.
Decision control statements
if statement
switch statement
Loops
while statement
for statement
do-while statement
26

if
if (condition is true)
execute this statement;
if (condition is true)
{
execute statement 1;
execute statement 2;

execute statement n;
}
27

if-else
if (condition is true)
execute this statement;
else
execute this statement;
if (condition is true){
execute statement 1;
execute statement 2;

execute statement n; }
else{
execute statement n+1;
execute statement n+2;

execute statement n+m;

28

Switch
switch (integer expression 1){
case integer constant 1:
statement 1;
statement 2;

statement n;
default:
statement n+1;
statement n+2;

statement n+m;
}

Can appear
0, 1 or more
times for
different
constant
values
Can appear 0
or 1 time
29

Switch example
/* This is a menu-driven program for basic
arithmetic operations between two numbers*/
main(){
int a,b,i;
printf("enter 2 integer numbers separated by
a blank space: ");
scanf("%d%d",&a,&b);
printf("please enter \n 1 to add\n 2 to
subtract\n 3 to divide\n 4 to multiply\n 5
for modulo\n");
scanf("%d",&i);
cal1.c

30

switch(i){
case 1: printf("Result of addition is :%d
",(a+b));
case 2: printf("Result of subtraction is :
%d ",(a-b));
case 3: printf("Result of division is :%d
",(a/b));
case 4: printf("Result of multiplication
is :%d ",(a*b));
case 5: printf("Result of modulo is :%d ",
(a%b));
default:printf(Inappropriate value
entered");
}
}
31

execution
Execution path 1 :
enter 2 integer numbers separated by a blank
You enter this
space: 12
2
please enter
1. 1 to add
2. 2 to subtract
3. 3 to divide
4. 4 to multiply
5. 5 for modulo
You enter this
3
Result of division is :6 Result of multiplication is :24
Result of modulo is :0
Inappropriate value entered
32

Fall through problem


Do you see the problem ?
To correct this problem break is used at the
end of each case statement.
It however not necessary to put the break at the
end of the last case or default statement.
Let us correct the program by including break.

33

cal2.c

switch(i){
case 1: printf("Result of addition is :%d ",
(a+b));
break;
case 2: printf("Result of subtraction is :%d ",
(a-b));
break;
case 3: printf("Result of division is :%d ",
(a/b));
break;
case 4: printf("Result of multiplication is :%d
",(a*b));
break;
case 5: printf("Result of modulo is :%d ",(a%b));
break;
default:printf(Inappropriate value entered");
}}
34

Execution
enter 2 integer numbers separated by a blank space:
You enter this
12 4
please enter
1 to add
2 to subtract
3 to divide
4 to multiply
5 for modulo
You enter this
2
Result of subtraction is :8
35

More points on switch


Since char is also of integer type you can use char
also as switch expression variables and case
constants. We can also mix char and integers.
default can be anywhere in between the switch
cases. It need not be at the end. But if it is in
between make sure that break is inserted.
You can have a case without any statement. To
understand where you would require such a thing
move on to the next slide.

36

Example: case without any statement


cal3.c

main(){
int a,b;
char i;
printf("enter 2 integer numbers
separated by a blank space: ");

scanf("%d%d",&a,&b);
scanf("%c",&i);
printf("please enter \n a to add\n s to
subtract\n d to divide\n m to
multiply\n o for modulo\n any other
char to exit\n ");
?
scanf("%c",&i);
37

switch(i){
Fall through
case 'a':
case 'A': printf("Result of addition
is :%d ",(a+b));
break;
Fall through
case 's':
case 'S': printf("Result of
subtraction is :%d ",(a-b));
break;
Fall through
case 'd':
case 'D': printf("Result of division
is :%d ",(a/b));
break;
38

case 'm':
Fall through
case 'M': printf("Result of
multiplication is :%d ",(a*b));
break;
case 'o':
Fall through
case 'O': printf("Result of modulo
is :%d ",(a%b));
break;
Exits out of the program
default: exit();
}
}
39

while-loop
while(condition is true)

statement;
while(condition is true){

statement 1;
statement 2;

statement n;
}
40

Example 1
/* program to display numbers divisible by 2
and 3 between 1 to 100 */
main()
div1.c
{
int i=1;
while(i!=100){
if(i%2==0 && i%3==0)
printf("%d, ",i);
i++;
}
}

41

do-while
do-while is same as while with the only exception that
the condition is checked only at the end of the loop.
Therefore do-while guarantees that the statement within
the loop will execute at least once.
do{
statement(s)
}
while(condition)
42

for
In example 1 of the while loop, we saw that we set up an
initial value for int i and then executed the loop until i
reached certain value, incrementing is value by 1 each
time.
This can be more simply achieved through for loop.
for(initialize var;condition;
increment/decrement var){
statement(s);
}

43

Count backwards
/* factorial of a number*/
main(){
int num,i,f;
printf("enter a number");
scanf("%d",&num);
f=num;
for(i=num-1;i>1;i--)
f=f*i;
printf("%d",f);
}
44

break in loops
The keyword break is used to exit out of the loop
anytime even when if it does not satisfy the loop
conditions.
When break is encountered the control jumps to the first
statement after the loop.

45

Example
What
does this code
do?*/
/*
primality
test
main(){
int num,j,i,flag=1;
printf("enter an integer ");
scanf("%d",&num);

Do you see any other


problem with this code?

for (i=2;i<num;i++){
if(num%i==0){
flag=0;
printf(number is not Prime ");
break;
}
}
if (flag) printf("number is Prime
");
}

Can you make this code more efficient?

46

continue in loop
The continue statement is used to skip the
rest of the statements in the loop and carry on
with the next iteration.
In other words, when continue is encountered,
the control goes to the beginning of the
enclosing loop.

47

Example
/* program to determine if an integer
greater than 100 entered by the user
is a prime number */
main(){
int num,j,i,flag=1;
while(1){
printf("enter an integer greater than
100 ");
scanf("%d",&num);
if(num<100) continue; If a number < 100 is entered
j=num/2;
48

for (i=2;i<=j;i++){
if(num%i==0){
flag=0;
printf("Not prime");
break;
}
}
if (flag) printf("Prime number");
break;
}
}
49

Potrebbero piacerti anche