Wednesday 14 September 2011

Operators and Expressions

Expression:

An expression is An expression is a sequence of operands and operators that reduces to a single value.


C Programming Provides its own rules of Expression, whether it is legal expression or illegal expression.

For example, in the C language x+5 is a legal expression.

Every expression consists of at least one operand and can have one or more operators.
Operands are values and Operators are symbols that represent particular actions.


Operators and Expressions are a part of C Tokens(words). C contains a rich set of operators.
defn:
  An Opertor in a program language specifies an operation to be performed that gives a result(value).

Category of Operators
The Operators are classified into three types:
1. Unary Opertors
2. Binary Operators
3. Ternary Operators


Unary Operators
A unary operator is an operator, which operates on one operand.

Binary Operators
A binary operator is an operator, which operates on two operands

Ternary Operators
A ternary operator is an operator, which operates on three operands.



These Operators depending on whether they operate one, two, or three operands respectively
(Operands means constants and variables together are called operands)

                                                  Different types of Operators:


1. Arithmetic Operator
2. Relational Operators
3. Logical Operators
4. Assignment Operator
5. Increment/Decrement Operators
6. Conditional Operators
7. Bitwize Operators
8. Special Operators

1. Arithmetic Operator

The arithmetic operator is a binary operator, which needs two operands to perform its operation. Following are the arithmetic operators

Operator       Meaning
+                   Addition (or) unary plus
-                    subraction (or) unary Minus
*(asterisk)     multiplication
/                    division
%                  modulus(remainder)



2. Relational Operators

Relational operators compare between two operands and returns  true or false i.e. 1 or 0. In C and many other languages a true value is denoted by the integer 1 and a false value is denoted by the integer 0. Relational operators are used in conjunction with logical operators and conditional & looping statements.

Following are the various relational operators
Operator       Meaning

 <                    Less than
<=                  Less than or equal to
>                    Greater than
>=                 Greater than or equal to

==               Equal to
!=               Not equal to


3. Logical Operators

A logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators available

Operator     Meaning
&&             Logical AND
||             Logical OR
!            Logical NOT



Truth table for Logical AND

1 1 1
1 0 0
0 1 0
0 0 0

Truth table for Logical OR

1 1 1
1 0 1
0 1 1
0 0 0



The Logical NOT operator (!)
The ! (Logical NOT) operator is a unary operator. This operator opposite(inverses) a result.


2. Assignment Operator


An assignment operator (=) is used to assign a constant or a value of one variable to left handside variable.

(a)Multiple assignments:

You can use the assignment for multiple assignments as follows:

a = b= c = 5;
At the end of this expression all variables a, b and c will have the value 10. Here the order of evaluation is from right to left. First 10 is assigned to c, hence the value of c now becomes 10. After that, the value of c (which is now 10) is assigned to b, hence the value of b becomes 10. Finally, the value of b (which is now 10) is assigned to a, hence the value of a becomes 10.

(b)Arithmetic Assignment Operators

Arithmetic Assignment operators are a combination of arithmetic and the assignment operator. With this operator, the arithmetic operation happens first and then the result of the operation is assigned.

Operators
+=
-=
*=
/=
%=

6. Conditional Operator(?:)

A conditional operator checks for an expression, which returns either a true or a false value. If the condition evaluated is true, it returns the value of the true section of the operator, otherwise it returns the value of the false section of the operator.

Precedence of Operators
precedence of operators in C
Operator                                        Associavity

Unary                                        Right to Left
Arithmetic Operators                Left to Right
Relational Operator                        Left to Right
Equality Operator                        Left to Right
Logical Operator                        Left to Right
Conditional Operator                Right to Left
Assignment Operator                Right to Left
Comma operator                        Right to Left

Differentiate between ‘=’ and’==’ in C language.

               =    
              ==
      1. It is a assignment operator                   
     1.it is a relational operator
      2. It is is used to assign the value         
       to the variable
     Eg: int a=10;
     2. It is used to compare the value to the left hand side variable.
     Eg : int a=5;
            a=10;
     3.The value 10 is string into the   variable a
     3.The value 10 is compare to the left hand side variable.



Related Videos:
 (1.) Arithmetic Operators https://youtu.be/6whfA-ARyMQ
    (2.) 
Relational Operator https://youtu.be/N-rWkVn9Fqs
    (3.) 
Logical Operators https://youtu.be/SQxypOkKWyM
    (4.) 
Assignment operators https://youtu.be/kyh9qQbf_uk
    (5.) 
Special Operators https://youtu.be/kyh9qQbf_uk
    (6.) 
Bitwise Operators https://youtu.be/xBNdZ2ytTiU
    Operator Precedence https://youtu.be/q8jc3s7Nmhk



No comments:

Post a Comment