1
Operators
A n o p e r a t o r i s a s y m b o l t h a t t e l l s t h e c o m p i l e r t o p e r f o r m s p e c i fi c m a t h e m a ti c a l o r
l o g i c a l f u n c ti o n s . A n o p e r a t o r i s a s y m b o l t h a t o p e r a t e s o n a v a l u e o r a v a r i a b l e .
Following are the types of operators
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Arithmetic Operators
The following table shows all the arithmetic, consider variable A has 10 and variable B has 20 then
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after an integer division. B%A=0
++ Increment operator , it increases the integer value by one. A++ = 11
-- Decrement operator, it decreases the integer value by one. A-- = 9
Relational Operators: The following table shows all the relational operators. Assume variable A holds 10
and variable B holds 20 then −
Operator Description Example
== Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B) is
not true.
!= Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is
true.
> Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B) is not
true.
< Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B) is
true.
>= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition (A >= B) is
becomes true. not true.
<= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition (A <= B) is
becomes true. true.
, 2
Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and
variable B holds 0, then −
Show Examples
Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the (A && B) is false.
condition becomes true.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the (A || B) is true.
condition becomes true.
! Called Logical NOT Operator. It is used to reverse the logical state of its !(A && B) is true.
operand. If a condition is true, then Logical NOT operator will make it false.
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. They are &, |, and ^
Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B)
| Binary OR Operator copies a bit if it exists in either operand. (A | B)
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B)
Assignment Operators
The following table lists the assignment operators supported by the C language
Operator Description Example
= Simple assignment operator. Assigns values from right side C = A + B will assign the value of A + B to
operands to left side operand C
+= Add AND assignment operator. It adds the right operand to
C += A is equivalent to C = C + A
the left operand and assign the result to the left operand.
-= Subtract AND assignment operator. It subtracts the right
operand from the left operand and assigns the result to the C -= A is equivalent to C = C - A
left operand.
, 3
C Control Statements:
Control statements is a process of transferring control from one place to another
place or skipping control or repeating control until the condition satisfies .
We have two types,
Branching
Looping
Branching: Branching is a process of jumping control from one place to another place (one statement
to another statement)
In branching we have several models
1. if
2. if else
3. if else ladder
4. nested if else
5. switch
Looping:
Looping is a process of repeating certain statements again and again until the condition satisfies.
In looping we have
1. while
2. do while
3. for
if statement:
The statements inside the body of “if” only execute if the given condition returns true. If the condition
returns false then the statements inside “if” are skipped.
Syntax of if statement:
if(condition)
{
Statements;
--
--
}
Ex:
#include <stdio.h>
main()
{
int x,y;
x=10; y=20;
if (x<y)
{
printf("Variable x is less than y");
}
}
if else statement
Syntax of if else statement:
If condition returns true then the statements inside the body of “if” are executed and
Operators
A n o p e r a t o r i s a s y m b o l t h a t t e l l s t h e c o m p i l e r t o p e r f o r m s p e c i fi c m a t h e m a ti c a l o r
l o g i c a l f u n c ti o n s . A n o p e r a t o r i s a s y m b o l t h a t o p e r a t e s o n a v a l u e o r a v a r i a b l e .
Following are the types of operators
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Arithmetic Operators
The following table shows all the arithmetic, consider variable A has 10 and variable B has 20 then
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after an integer division. B%A=0
++ Increment operator , it increases the integer value by one. A++ = 11
-- Decrement operator, it decreases the integer value by one. A-- = 9
Relational Operators: The following table shows all the relational operators. Assume variable A holds 10
and variable B holds 20 then −
Operator Description Example
== Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B) is
not true.
!= Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is
true.
> Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B) is not
true.
< Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B) is
true.
>= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition (A >= B) is
becomes true. not true.
<= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition (A <= B) is
becomes true. true.
, 2
Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and
variable B holds 0, then −
Show Examples
Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the (A && B) is false.
condition becomes true.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the (A || B) is true.
condition becomes true.
! Called Logical NOT Operator. It is used to reverse the logical state of its !(A && B) is true.
operand. If a condition is true, then Logical NOT operator will make it false.
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. They are &, |, and ^
Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B)
| Binary OR Operator copies a bit if it exists in either operand. (A | B)
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B)
Assignment Operators
The following table lists the assignment operators supported by the C language
Operator Description Example
= Simple assignment operator. Assigns values from right side C = A + B will assign the value of A + B to
operands to left side operand C
+= Add AND assignment operator. It adds the right operand to
C += A is equivalent to C = C + A
the left operand and assign the result to the left operand.
-= Subtract AND assignment operator. It subtracts the right
operand from the left operand and assigns the result to the C -= A is equivalent to C = C - A
left operand.
, 3
C Control Statements:
Control statements is a process of transferring control from one place to another
place or skipping control or repeating control until the condition satisfies .
We have two types,
Branching
Looping
Branching: Branching is a process of jumping control from one place to another place (one statement
to another statement)
In branching we have several models
1. if
2. if else
3. if else ladder
4. nested if else
5. switch
Looping:
Looping is a process of repeating certain statements again and again until the condition satisfies.
In looping we have
1. while
2. do while
3. for
if statement:
The statements inside the body of “if” only execute if the given condition returns true. If the condition
returns false then the statements inside “if” are skipped.
Syntax of if statement:
if(condition)
{
Statements;
--
--
}
Ex:
#include <stdio.h>
main()
{
int x,y;
x=10; y=20;
if (x<y)
{
printf("Variable x is less than y");
}
}
if else statement
Syntax of if else statement:
If condition returns true then the statements inside the body of “if” are executed and