In this mathematical expression a, b are called operands.
+ is treated as Operator.
What is operand??
Operand can be a variable or constant. just like 2, 3, 44.99 so....... on.
Then what is Operator??
Operator specifies what has to be done on the operands for instance addition.
C provides large set of operators, which can be classified as
1. Arithmetic Operator
2. Relational Operator
3.Logical Operator.
4. Increment and Decrement Operators
5. Conditional Operators.
1. Arithmetic Operator:
x % y produces the remainder when x is divided by y, and thus is zero when y divides x exactly. The % operator cannot be applied to a float or double. The direction of truncation for / and the sign of the result for % are machine-dependent for negative operands.
The binary + and - operators have the same precedence, which is lower than the precedence of *, / and %, which is in turn lower than unary + and -. Arithmetic operators associate left to right.
Binary Operator: which needs at least 2 operands. Ex; a+b, a*b, 2/3.....
Unary Operator: which needs only one operand.
2.Relational Operators:
Operator | Meaning |
< | is less than |
<= | is less than or equal to |
> | is greater than |
>= Equality Operators: | is greater than or equal to |
== | is equal to |
!= | is not equal to |
Relational operators have lower precedence than arithmetic operators.
3.Logical Operators:
We use Logical Operators to combine 2 or more expressions.
&& Logical AND
|| Logical OR
Logical Operators have less precedence than Relational Operators. Logical AND has More PRECEDENCE than Logical OR.
NoTe: ! negation operator converts a non-zero to 0 and zEro to Non-zeRo.
Ex: 1) !0 ----> 1
2) 33! ---> 0
! is unary operator.
4.Increment and Decrement Operators:
Operator Meaning
++ Increment
- - Decrement
The increment operator ++ adds 1 to its operand, while the decrement operator -- subtracts 1.
The unusual aspect is that ++ and -- may be used either as prefix operators (before the variable, as in ++n), or postfix operators (after the variable: n++). In both cases, the effect is to increment n. But the expression ++n increments n before its value is used, while n++ increments n after its value has been
used. This means that in a context where the value is being used, not just the effect,
++n and n++ are different. If n is 5, then
x = n++;
sets x to 5, but
x = ++n;
sets x to 6. In both cases, n becomes 6. The increment and decrement operators can only be applied to variables; an expression like (i+j)++ is illegal.
5.Conditional Operator:
C has one last operator which we haven't seen yet. It's called the conditional or ``ternary'' or ?: operator, and in action it looks something like this:
average = (n > 0) ? sum / n : 0
The syntax of the conditional operator is
e1 ? e2 : e3and what happens is that e1 is evaluated, and if it's true then e2 is evaluated and becomes the result of the expression, otherwise e3 is evaluated and becomes the result of the expression.
Ex:
int result = (10>9) ? 1:2 ;
in these example expression one is true so second expression will be executed. Finally 1 will be assigned to result.
No comments:
Post a Comment