Saturday 4 October 2014

Operator and Expressions

Operators and Expressions

An operator that performs a specific task and  yield the value. The variables, constants can be joined by various operators to form an expression. An operand is a data on which operator acts. Some operators require two operands.


There are many type of Operators-
  1. Arithmetic Operators
  2. Assignment Operators 
  3. Increment and Decrements Operator
  4. Relational Operator
  5. Logical Operator 
  6. Conditional Operator 
  7. Comma Operator 
  8. Sizeof Operator
  9. Other Operators 

Arithmetic Operator

Arithmetic operator are used for numeric calculations. They are of two type-
  1. Unary arithmetic operator
  2. Binary arithmetic operatoer

Unary Arithmetic Operator : In unary operator only one operand are used.
e.g. +x , -y ,+z , +r.

Binary Arithmetic Operator : Binary operator require two operands. There are five binary arithmetic operators-

Operator
Purpose


+
Addition
-
Subtraction
*
Multiplication
/
division
%
Give the remainder


Example :-

Integer Arithmetic :- When both operand are integer then the arithmetic operation with these operands is called integer arithmetic and the resulting value is always an integer. take two variable a and b. The value of a=17 and b=4, the result of the following operand are-


Expression
Result


a+b
21
a-b
13
a*b
68
a/b
4
a%b
1

After division operation the decimal part will be truncated and result is only
integer part of quotient.

#include<stdio.h>
main()
{
int a=17 , b=4;
printf("Sum =%d\n", a+b);
printf("Substract =%d\n",a-b);
printf("Product =%d\n",a*b);
printf("Quotient=%d\n,a/b);
printf("Remainder =%d\n",a%b);
}

Output-
Sum=21
Substract=13
Product=68
Quotent=4
Remainder=1


Floating point Arithmetic : When both operands are of float type then the arithmetic operation with these operands is called floating point arithmetic. Take two variable a=12.4 and b=3.1 then result of the following operations.


Expression
Result


a+b
15.5
a-b
9.3
a*b
38.44
a/b
4.0

The modules operator % cannot be used floating point number

#include<stdio.h>
main()
{
float a=12.4, b=3.8;
printf("Sum =%.2f\n",a+b);
printf("substract =%.2f\n",a-b);
printf("Product =%.2f\n",a*b);
printf("a/b =%.2f\n",a/b);
}


Assignment Operator


A value can be stored in a variable with the use of assignment operator. This assignment operator "=" is used in assignment expression and assignment statement.
The operand on the left hand side should be a variable, while the operand on the right hand side can be any variable, constant or expression. The value of right hand side operand is assigned to the left hand operand. Example --

x=8               8 is assigned to x.
y=5               5 is assigned to y
s=x+y-2        Value of expression x+y-2 is assigned to s
y=x               Value of x is assign to y.
x=y               Value of y is assigned to x

The value that is being assigned is consider as value of the assignment expression. for example =8 is an assignment expression whose value is 8.
We can multiple assignment operation also.

x=y=z=20
Here all three variable x,y,z will be assigned value 20, and the value of the variable whole expression will be 20.
if we put a semicolon after the assignment expression then it become an assignment statement  for e.g. 
x=8;
y=5;
s=x+y-2;
x=y=z=20;
when the variable on the left hand side of assignment operator also occurs on the right hand side then we can avoid writing the variable twice by using compound assignment operators. for e.g.
x=x+5
can also be written as
x+=5
here += is a compound statement
we have other compound statement-
x-=5             is equivalent                      x=x-5
y*=5             is equivalent                      y=y*5
sum/=5         is equivalent                     sum=sum/5



Increment And Decrements Operators

C has two useful operators increment(++) and decrement(--). there are unary operators because they operate on a single operand. The increment operator(++) increment the value of the variable by 1 and decrement by 1 and decrement operator (--) drecrements the value of the variable by 1.
++x    is equivalent    x=x+1
--x      is equivalent    x=x-1
These operators should be used only with variable; they can't be used with constant or expressions.
for example the expressions ++5 or ++(x+y+z) are invalid.

These operator are two type-


  1. prefix increment / decrement 
  2. Postfix increment / decrement 

  prefix increment / decrement 

In prefix,operator written before the operand e.g. ++x or --x . here first the value of the variable is incremented/decremented then the new value is used in the operation. take a variable x whose value is 3. means x=3.
The statement y=++x; means first increment the value of x by 1, then assign the value of x to y.
This single statement is equivalent to these two statements-
x=x+1;
y=x;
Hence now value of x is 4 and value of y is 4.

The statement y=--x; means first decrement the value of x by 1 then assign the value of x and y.
This statement x=x-1;
y=x;
Hence now value of x is 3 and value of y is 3.

// Program to understand the use of prefix increment/ decrements 
#include<stdio.h>
main()
{
int x=8;
printf("x=%d\t",x);
printf("x=%d\t",++x);
printf("x=%d\t,x);
printf("x=%d\t",--x);
printf("x=%d\t",x);
}

Output
x=8, x=9, x=9, x=8;x=8


Postfix Increment /Decrement

Operator is written after the operand. here first the value of variable is used in the operation and then increment/decrement is performed. take a variable x whose value is 3 means x=3;
The statement y=x++; means first the value of x is assigned to y and then x is incremented. This statement is equivalent to these two statements-
y=x;
x=x+1;
hence now value of x is 4 and value of y is 3
The statement y=x--; means first the value of x is assigned to y and then x is decremented. this statement is equivalent to these two varibale-
y=x;
x=x+1
now value of x is 3 and value of y is 4

// understood by program//

#include<stdio.h>
main()
{
int x=8;
printf("x=%d\t",x);
printf("x=%d\t",x++);
printf("x=%d\t,x);
printf("x=%d\t",x--);
printf("x=%d\t",x);
}

Output
x=8, x=8, x=9, x=9;x=8



Relational Operators

Relational operators are used to compare values of two expression depending on their relations. An expression that contains relational operators is called relational expression. If the relation is true then the value of relational expression is 1 and if the relational is false then the value of expression is 0.
The relational operators are.

Operator
Meaning


< 
Less than
<=
Less than or equal to
==
Equal to
!=
Not equal to
> 
Greater than
>=
Greater than or equal to
Two variables a=9 and b=5, and form simple relational expression with them

Expression
Relation
Value of Expression



A<b
False
0
A<=b
False
0
A==b
False
0
A!=b
True
1
a>b
True
1
A==0
False
0
B!=0
True
1

The relational operators are generally used in If.......else construct and loops. 

=Program to understand the use of relational operators.

#include<stdio.h>
main()
{
int a,b;
printf("Enter the value for a and b");
scanf("%d%d",&a,&b);
if(a<b)
printf("%d is less than %d\n",a,b);
if(a<=b)
printf("%d is less than or equal to %d\n",a,b);
if(a==b)
printf("%d is equal to %d\n",a,b);
if(a!=b)
printf("%d is not equal to %d\n",a,b);
if(a>b)
printf("%d is greater than %d\n",a,b);
}

It is important to note that the assignment operator(=) and equality (==) are entirely different. assignment operator = is used to assign the value, and == operator is used to compare the value.



Logical Or Boolean Operators

An expression that combines two or more expressions is termed as a logical expression. for combining these expressions we use logical operators. These operators return 0 for false and 1 for true. The operands may be constants, variable expressions. C has three logical operators.

Operator
Meaning


&&
AND
||
OR
!
NOT

  1. AND (&&) Operators

This operator give the net result true if both the conditions are true,otherwise the result is false.


Condition1
Condition2
Result



False
False
False
False
True
False
True
False
False
True
True
True

Lets us take three  variable a=10, b=5, c=0
if we have a logical expression 
(a==10)&&(b<a)Here both the conditions a==10 and b<a are true, and hence this whole expression is true. Since the logical operators return 1 for true hence the value of this expression is 1.


Expression

Result
Value of expression




(a==10)&&(b>a)
True && false
False
0
(b>=a)&&(b==3)
False && false
False
0
A && b
True && true
True
1
A && c
True && false
False
0


2.OR (| |) Operator :

This operator gives the net result false, if both the conditions have value false , otherwise the result is true.

Condition  1
Condition  1
Result



False
False
False
False
True
True
True
False
True
True
True
True

Let us take three variable a=10 , b=5, c=0
Consider the logical expression-
(a>=b) ||(b>15)

this gives result true because one condition is true.

Expression

Result
Value of expression




A||b
True || true
True
1
A||c
True||false
True
1
(a<9)||(b>10)
False||false
False
0
(b!=7)||c
True||false
True
1

3.Not (!) Operator

This is a unary operator and it negates the value of the condition. if the value of the condition is false then it gives the result true. if the value of the condition is true then it gives the result false.

Condition
result


False
True
True
False

Conditional Operator

Conditional operator is a ternary operator (? and :) which requires the three expression as operands. 

Syntax- (condition)? Expression 1 : Expression 2

If the condition is true then expression 1 is proceed and if condition is false then expression 2 is proceed.
for eg. if a=5 and b=6;
(a>b)?printf("the greater", a):printf("b is greater",b);

// program to print the larger of two number using conditional operator

#include<stdio.h>
main()
{
int a,b,max;
printf("enter value for a and b");
scanf("%d%d",&a,&b);
max=a>b? a:b;
printf("Larger of %d and %d is %d\n"a,b,max);
}

Output

Enter value for a and b: 12  7
Larger of 12 and 7 is 12

Comma Operator

The comma operator (,) is used to permit different expression to appear in situations where only one expression would be used. The expression are separated by the comma operator.The separated expression are evaluated from left to right . Comma Operator separated two or more variable.

a=8, b=7,c=9,a+b+c

Here 4 variable 8 is assigned to variable a, 7 is assigned to variable b, 9 is assigned to variable c and all variable is separated by comma operator.

// Program to understand the use of comma operator.

#include<stdio.h>
main()
{
int a,b,c,sum;
sum=(a=8,b=7,c=9,a+b+c);
printf("Sum=%d",sum);
}

Output

24

Sizeof Operators

Sizeof is an unary operator. This operator gives the size of its operand in the term of bytes. The operand can be a variable, constant or any datatype( int, float,char etc) for example- sizeof(int) gives the byte occupied by the int data type.

// Program to understand the sizeof operator.

#include<stdio.h>
main()
{
int var;
printf("Size of int =%d",sizeof(int));
printf("Size of float =%d",sizeof(float));
printf("Size of var =%d",sizeof(var));
printf("Size of an integer constant=%d",sizeof(45));
}

output

Size of int : 2
Size of float : 4
Size of var : 2
Size of integer Constant =2


No comments:

Post a Comment