Anda di halaman 1dari 27

OPERATORS AND EXPRESSIONS

Programming languages for Bioinformatics


BIF507

By Neeru Redhu
CCS HAU, Hisar
Operator is a symbol that tells the computer to
perform certain mathematical or logical
manipulations
Operators used in programs to manipulate data and
variables.
Usually form a part of mathematical or logical
expression

2
C operator can be classified into:
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators

3
ARITHMETIC OPERATORS

Basic arithmetic operators

Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division

4
Integer Arithmetic
Operands integer (a, b)
From a integer expression (a+b or a-b etc)
Integer arithmetic

Real Arithmetic
Involves only real operands (variables declared as float)
Operator % cannot be used.

Mixed- mode Arithmetic


One operand is real and other is integer.

5
RELATIONAL OPERATORS
To compare two quantities relative to each other

Relational operators

Operator Meaning

> Greater than

>= Greater than or equal to

<= Less than or equal to

< Less than

== Is equal to
6
!= Is not equal to
A>B or a<b (relational expression)

Examples
4.5 <= 10 TRUE
4.5 < -10 FALSE
-35 >= 0 FALSE
10 < 7+5 TRUE
a+b = c+d TRUE/FALSE

Used in decision statements

7
RELATIONAL OPERATORS COMPLEMENTS

Actual Complement
> <=
< >=
== !=

8
Actual Simplified
! (x < y) x >= y
! (x > y) x <= y
! (x!=y) x == y
! (x<= y) x>y
! (x>= y) x<y
! (x == y) x != y

9
LOGICAL OPERATORS
&& -- AND
|| -- OR
! -- NOT

Logical expression / compound relational expression

(age > 55 && salary < 1000)


(number < 0 || number > 100)

10
PRECEDENCE OF RELATIONAL AND LOGICAL OPERATORS

Highest !
> , >=, <, <=
= =, ! =
&&
Lowest ||

11
ASSIGNMENT OPERATORS
Used to assign result/value to a variable

a=3
b = -6

c=a+b

d = d + c etc

12
SHORTHAND ASSIGNMENT OPERATORS
Statement with simple Statement with
assignment operator shorthand operator
a=a+1 a += 1
a=a1 a=1
a=a*b a *= b
a=a/b a/=b
a=a%b a%=b

13
INCREMENT AND DECREMENT OPERATORS
++ and -- (+1 and 1 respectively)

++c and c++


--c and c--

c = 5; c = 5;
d = ++c; d = c++;

14
CONDITIONAL OPERATORS
uses ternary operator ( ? : )

exp1 ? exp2 : exp3

if (a > b)
e. g. a = 10; x = a;
b = 15; else
x = b;
x = (a > b) ? a : b;

15
BITWISE OPERATOR
Operates only on integer

For manipulating of data as bit level

Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right

16
SPECIAL OPERATORS
Comma operator ( , )
Used to link related expression together
val = (a = 1, b = 2, x + y);

sizeof operator
Compile time operator
Returns the number of bytes the operand occupies
m = sizeof (sum);

Pointer operator ( & and * )


Member selection ( . and -> ) 17
TYPE CONVERSIONS
Implicit
automatic
Explicit
Usage (datatype_name) expression
e.g
x = (int) 4.5
a = (double) sum / n

18
Operator Description Associativity
left-to-right
() Parentheses (function call) (see Note 1)
[] Brackets (array subscript)
. Member selection via object name
-> Member selection via pointer
++ -- Postfix increment/decrement (see Note 2)

right-to-left
Prefix increment/decrement
++ -- Unary plus/minus
+- Logical negation/bitwise complement
!~ Cast (convert value to temporary value
(type) of type)
* Dereference
& Address (of operand)
sizeof Determine size in bytes on this
implementation

* / % Multiplication/division/modulus left-to-right

+ - Addition/subtraction left-to-right

left-to-right
<< >> Bitwise shift left, Bitwise shift right
left-to-right
Relational less than/less than or equal to 19
< <=
Relational greater than/greater than or
> >=
equal to
Operator Description Associativity
left-to-right
== != Relational is equal to/is not equal to

& Bitwise AND left-to-right

^ Bitwise exclusive OR left-to-right

| Bitwise inclusive OR left-to-right

&& Logical AND left-to-right

|| Logical OR left-to-right

?: Ternary conditional right-to-left

right-to-left

= Assignment
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment

20
left-to-right
, Comma (separate expressions)
MANAGING INPUT AND OUTPUT OPERATIONS

FORMATTED INPUT

The specification for reading integer %w d

e.g scanf ( %2d %5d , &num1, &num2);


50 31256

21
22
23
COMMONLY USED SCANF CODES

Code Meaning Code Meaning


Decimal,
%c Single Char %i hex or octal
int
Decimal Octal
%d %o
Integer integer
Floating
%e %s String
point
Floating Unsigned
%f %u
point int
Floating Hexadecima
%g %x
point l int
String of 24
%h Short int %[/^..]
words
FORMATTED OUTPUT
Specification output %w d

Output of integer number

Format Output
printf (%d,9876); 9876
printf (%6d,9876); _ _9876
printf (%2d,9876); 9876
printf (%-6d,9876); 9876_ _
printf (%06d,9876); 009876

25
Output of Real number

Format Output
printf (%7.4f,y) 98.7654
printf (%7.2f,y) _ _ 98.77
printf (%-7.2f,y) 98.77_ _
printf (%f,y) 98.7654
printf (10.2e,y) _ __ 9.88e+01
printf (%11.4e,-y) -9.8765e+01
printf (%-10.2e,y) 9.88e+01_ _
printf (%e,y) 9.876540e+01
26
END

27

Anda mungkin juga menyukai