0% found this document useful (0 votes)
27 views29 pages

Unit 2

unit2 ppsc

Uploaded by

mounica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views29 pages

Unit 2

unit2 ppsc

Uploaded by

mounica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Unit -2

Operators

 An operator is a symbol that tells the compiler to perform specific


mathematical or logical functions.
 These operators are classified as
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Assignment Operators
 Bitwise Operators
 Miscellaneous Operators

Arithmetic Operators:
• An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values.
• Assume that a=10 and b=20, the following are the arithmetic operators:
Operator Description example

+ Adds two operands a+b=30

- Subtracts two operands a-b=-10

* Multiplies both a*b=200


operands
/ Divides numerator by b/a=2
denominator
% Modulus operator find b%a=0
the reminder of integer
division.

Note:
• The modulus(%) operator can be applied to integer operands and cannot
be used for float or double operands.
• All other arithmetic operators can accept a mix of integer and floating
point numbers.
Example program for arithmetic operators:

#include <stdio.h>
int main()
{
int a = 10, b = 4, c;
printf("a is %d and b is %d\n", a, b); // printing a and b
c = a + b; // addition
printf("a+b is %d\n", c);
c = a - b; // subtraction
printf("a-b is %d\n", c);
c = a * b; // multiplication
printf("a*b is %d\n", c);
c = a / b; // division
printf("a/b is %d\n", c);
c= a % b; // modulus
printf("a%b is %d\n", c);
getch();
return 0;
}
Relational Operators:
• A relational operator checks the relationship between two operands.if
relation is true, it returns 1;if the relation is false, it returns 0.
• Relational operators are used in decision making and loops
• Assume that a=10 and b=20, the following are the relational operators.
Operator Description example

== Checks if the values of (A == B) is not true.


two operands are equal
or not. If yes, then the
condition becomes
true.
!= Checks if the values of (A != B) is true.
two operands are equal
or not. If the values are
not equal, then the
condition becomes
true.
> Checks if the value of (A > B) is not true.
left operand is greater
than the value of right
operand. If yes, then
the condition becomes
true.
< Checks if the value of (A < B) is true.
left operand is less than
the value of right
operand. If yes, then
the condition becomes
true.
>= Checks if the value of (A >= B) is not true.
left operand is greater
than or equal to the
value of right operand.
If yes, then the
condition becomes
true.
<= Checks if the value of (A <= B) is true.
left operand is less than
or equal to the value of
right operand. If yes,
then the condition
becomes true.
Example program to demonstrate relational operators using if-else:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf("enter two values\n");
scanf("%d%d",&a,&b);
if(a==b)
printf("a=b\n");
else
printf("a not =b\n");
if(a>b)
printf("a>b\n");
else
printf("a not greater than b\n");
getch();
}
Logical Operators:
• These operators are used to perform logical operations on the given
expressions.
• Logical operators are commonly used in decision making in c language.
• Assume variable A holds 1 and variable B holds 0, then
Operator Description example

&& Called Logical (A && B) is false.


AND operator. If
both the operands
are non-zero, then
the condition
becomes true.
|| Called Logical OR (A || B) is true.
Operator. If any of
the two operands
is non-zero, then
the condition
becomes true.
! Called Logical !(A && B) is true.
NOT Operator. It
is used to reverse
the logical state of
its operand. If a
condition is true,
then Logical NOT
operator will make
it false.
Example program for logical operators:

#include<stdio.h>

#include<conio.h>

main()

int a=10,b=20,c=30,res;
clrscr();

res=(a>b)||(b>c);

printf("(a>b)||(b>c) is %d\n",res);

res=(a<b)&&(b<c);

printf("(a<b)&&(b<c) is %d\n",res);

res=!((a<b)&&(b<c));

printf("!((a<b)&&(b<c)) is %d\n",res);

res=!((a>b)||(b>c));

printf("!((a<b)||(b<c)) is %d\n",res);

getch();

Assignment Operators:
• An assignment operator is used to assign a value to a variable.

The following table lists the assignment operators supported by the C


language

Operator Description Example

= Simple assignment C = A + B will assign


operator. Assigns the value of A + B to
values from right side C
operands to left side
operand
+= Add AND assignment C += A is equivalent
operator. It adds the to C = C + A
right operand to the left
operand and assign the
result to the left
operand.
-= Subtract AND C -= A is equivalent to
assignment operator. It C=C–A
subtracts the right
operand from the left
operand and assigns
the result to the left
operand.
*= Multiply AND C *= A is equivalent to
assignment operator. It C=C*A
multiplies the right
operand with the left
operand and assigns
the result to the left
operand.
/= Divide AND C /= A is equivalent to
assignment operator. It C=C/A
divides the left operand
with the right operand
and assigns the result
to the left operand.
%= Modulus AND C %= A is equivalent
assignment operator. It to C = C % A
takes modulus using
two operands and
assigns the result to the
left operand.
<<= Left shift AND C <<= 2 is same as C
assignment operator. = C << 2

>>= Right shift AND C >>= 2 is same as C


assignment operator. = C >> 2

&= Bitwise AND C &= 2 is same as C =


assignment operator. C&2

^= Bitwise exclusive OR C ^= 2 is same as C =


and assignment C ^ 2
operator.
|= Bitwise inclusive OR C |= 2 is same as C =
and assignment C | 2
operator.
Example program for assignment operators:

#include<stdio.h>

#include<conio.h>

main()

int a,b;

clrscr();

printf("enter 2 integer values\n");

scanf("%d%d",&a,&b);

b=a;

printf("the value of b is %d\n",b);

b+=a;

printf("the value of b is %d\n",b);

b-=a;

printf("the value of b is %d\n",b);

b*=a;

printf("the value of b is %d\n",b);

a<<=2;

printf("the value of a is %d\n",a);

getch();

}
Bitwise Operators:
• Bit wise operators are those operator which performs operations at bit
level.
• Assume variable 'A' holds 60 and variable 'B' holds 13, then

A = 0011 1100
B = 0000 1101

Operator Description Example

& Binary AND Operator (A & B) = 12, i.e.,


copies a bit to the result 0000 1100
if it exists in both
operands.
| Binary OR Operator (A | B) = 61, i.e., 0011
copies a bit if it exists 1101
in either operand.
^ Binary XOR Operator (A ^ B) = 49, i.e., 0011
copies the bit if it is set 0001
in one operand but not
both.
~ Binary One's (~A ) = ~(60), i.e,. -
Complement Operator 0111101
is unary and has the
effect of 'flipping' bits.
<< Binary Left Shift A << 2 = 240 i.e.,
Operator. The left 1111 0000
operands value is
moved left by the
number of bits
specified by the right
operand.
>> Binary Right Shift A >> 2 = 15 i.e., 0000
Operator. The left 1111
operands value is
moved right by the
number of bits
specified by the right
operand.
The truth tables for &, |, and ^ is as follows:

P Q P&Q P|Q P^Q

0 0 0 0 0

0 1 0 1 1

1 0 0 1 1

1 1 1 1 0

Example program for bitwise operators:

#include<stdio.h>

#include<conio.h>

main()

int a,b,c;

clrscr();

printf(“enter the values of a and b\n”);

scanf(“%d%d”,&a,&b);

c=a&b;

printf(“a & b is %d\n”,c);

c=a|b;

printf(“a | b is %d\n”,c);

c=a>>b;

printf(“a >>b is %d\n”,c);

c=a<<b;
printf(“a << b is %d\n”,c);

c=~a;

printf(“~a is %d\n”,c);

getch();

Miscellaneous operators:

1.unary operators:

• Unary operators act on single operands. C language supports three unary


operators. They are unary minus, increment, and decrement operators.
Unary Minus (–):
• Unary minus operator negates the value of its operand.
• For example, if a number is positive then it becomes negative when
preceded with a unary minus operator.
• Similarly, if the number is negative, it becomes positive after applying
the unary minus operator.
• For example,
int a, b = 10;
a = –(b);
Increment Operator (++):
• The increment operator is a unary operator that increases the value
of its operand by 1.

Decrement Operator (– –):


• The decrement operator decreases the value of its operand by 1.
The increment/decrement operators have two variants:
1. prefix - In a prefix expression (++x or – –x), the operator is applied
before the operand
Ex:
y = x++; is equivalent to writing
y = x;
x = x + 1;
Whereas y = ++x; is equivalent to writing
x = x + 1;
y = x;
2. postfix- In a postfix expression (x++ or x––), the operator is applied
after the operand.
Ex:
y=--x;is same as x=x-1;
y=x;
y=x—is same as y=x;
y=x-1;
Example program for increment and decrement operators:
#include<stdio.h>
#include<conio.>
main()
{
int x=4,y;
clrscr();
y=x++;
printf(“the value of y is %d\n”,y);
printf(“the value of x is %d\n”,x);
y=++x;
printf(“the value of y is %d\n”,y);
printf(“the value of x is %d\n”,x);
y=x--;
printf(“the value of y is %d\n”,y);
printf(“the value of x is %d\n”,x);
y=--x;
printf(“the value of y is %d\n”,y);
printf(“the value of x is %d\n”,x);
getch();
}
Sizeof operator:
• sizeof is a unary operator used to calculate the size of data types.
• This operator can be applied to all data types.
• When using this operator, the keyword sizeof is followed by a type
name, variable, or expression.
• The operator returns the size of the data type, variable, or expression
in bytes.
• That is, the sizeof operator is used to determine the amount of
memory space that the data type/variable/expression will take.
Ex:
int a = 10;
unsigned int result;
result = sizeof(a);

Conditional Operator:
• Conditional operator is also known as ternary operator as it takes
three operands.
• It is just like an if-else statements that can be used within
expressions.
• This operator is useful in situations in which there are 2 or more
alternatives for an expression.
Syntax:
exp1 ? exp2 : exp3
• exp1 is evaluated first. If it is true, then exp2 is evaluated and becomes
the result of the expression,otherwise exp3 is evaluated and becomes the
result of the expression.
Ex: large = (a > b) ? a : b
Address operator(&):
• It returns the actual address of the variable.
Indirection operator(*):
• It is a pointer to the variable.
Selection & Making Decisions
Selection & Making Decisions/decision control/conditional/branching
statements:
Conditional Statements in C programming are used to make decisions based
on the conditions.
c supports 2 types of conditional statements.
1.2-way selection
• Simple if(Null else)
• If else
• Nested if
2.multiway selection
• Else if ladder
• Switch
Simple if statements:
• The single if statement in C language is used to execute the code if a
condition is true. It is also called one-way selection statement.
Syntax:
if(condition)
{
// Statements to execute if condition is true
}
• If the condition is evaluated as true, then the block statement is executed,
but if the condition is evaluated as false, then control is passed to the next
Statement following it.
Example program for simple if:
#include<stdio.h>
#include<conio.h>
main()
{
int a=10;
clrscr();
if(a>15)
{
printf(“a is greater than 15\n”);
}
printf(“a is less than 15\n”);
getch();
}
If else statement:
• If the given condition is true, then program control goes inside the if
block and executes the Statement.
• The condition is false, then program control goes inside the else block
and executes the corresponding Statement.
Syntax:
if(expression or condition)

// statement1 ;
}

else

//statement 2;

Example program for else if:


#include<stdio.h>
#include<conio.h>
main()
{
int a=10;
clrscr();
if(a>15)
{
printf(“a is greater than 15\n”);
}
else
{
printf(“a is less than 15\n”);
}
getch();
}
Nested if statement:
• The nested if...else statement is used when a program requires more than
one test expression.
Syntax:
if(test condition1)
{
if (test expression2 )
{
statement block1;
}
else
{
statement block 2;
}
}
else
{
statement block3;
}

Example program for nested if statement:


#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf("enter two integer values\n");
scanf("%d%d",&a,&b);
if(a!=b)
{
printf("a is not equal to b\n");

if(a<b)
{
printf("a is less than b\n");
}
else
{
printf("a is greater than b\n");
}
}
else
{
printf("a is equal to b\n");
}
getch();
}
Else if ladder:
• If else if is used to check multiple conditions.
• If one among the multiple conditions is true then the statement related to
that particular condition is executed and all other conditions are ignored.
• If none of the condition are true then the final else statement is executed.
Syntax:
If(condition1)
{
//statement1;
}
Else if(condition2)
{
//statement2;
}
Else if(condition3)
{
//statement3;
}

.
.
Else
{
//statement in else
}

Example program for else-if ladder:


#include<stdio.h>
#include<conio.h>
main()
{
int day;
clrscr();
printf("enter a day(1-7)\n");
scanf("%d",&day);
if(day==1)
{
printf("monday");
}
else if(day==2)
{
printf("tuesday");
}
else if(day==3)
{
printf("wednesday");
}
else if(day==4)
{
printf("thursday");
}
else if(day==5)
{
printf("friday");
}
else if(day==6)
{
printf("saturday");
}
else if(day==7)
{
printf("sunday");
}
else
{
printf("invalid day\n");
}
getch();
}
Dangling else problem:
• Nested if and if else if statement encounter a problem known as dangling
else problem.
• It is created when there is no matching else for every if statement.
• In this case ‘c’ compiler always match an else statement to the most recent
if statement.
Ex:
If(a>b)
If(a>c)
Printf(“a is greater than b and c\n”);
Else
Printf(“a is not greater than b and c\n”);
Switch case :
• A switch-case statement is a multi-way decision statement that is a
simplified version of an if– else–if block.
Syntax:
switch (variable)
{
case value 1:
statement block 1;
break;
case value 2:
statement block 2;
break;
.....................
case value N:
statement block N;
break;
default:
statement block D;
break;
}
statement X;

• Switch statements are mostly used in two situations:


o When there is only one variable to evaluate in the expression
o When many conditions are being tested for
• Switch case statements are often used as an alternative to long if statements
that compare a variable to several ‘integral’ values (integral values are
those values that can be expressed as an integer, such as the value of a
char).
• The switch case statement compares the value of the variable given in the
switch statement with the value of each case statement that follows. When
the value of the switch and the case statement matches, the statement block
of that particular case is executed.
• Default is the case that is executed when the value of the variable does not
match with any of the values of the case statements. That is, default case is
executed when no match is found between the values of switch and case
statements and thus there are no statements to be executed. Although the
default case is optional, it is always recommended to include it as it handles
any unexpected case.
Break:
• The break statement must be used at the end of each case because if it is
not used, then the case that matched and all the following cases will be
executed.
• For example, if the value of switch statement matched with that of case 2,
then all the statements in case 2 as well as the rest of the cases including
default will be executed.
• The break statement tells the compiler to jump out of the switch case
statement and execute the statement following the switch–case construct.
Thus, the keyword break is used to break out of the case statements.
Advantages of Using a switch–case Statement:
Switch–case statement is preferred by programmers due to the following reasons:
• Easy to debug
• Easy to read and understand
• Ease of maintenance as compared to its equivalent if–else statements
• Like if–else statements, switch statements can also be nested
• Executes faster than its equivalent if–else construct

Example program for switch case:

#include<stdio.h>

#include<conio.h>

main()

int day;

clrscr();

printf("enter a day(1-7)");

scanf("%d",&day);

switch(day)

{
case 1:

printf("monday");

break;

case 2:

printf("tuesday");

break;

case 3:

printf("wednesday");

break;

case 4:

printf("thursday");

break;

case 5:

printf("friday");

break;

case 6:

printf("saturday");

break;

case 7:

printf("sunday");

break;

default:

printf("invalid day\n");

}
getch();

More Standard Functions:

Character libraries are divided into 2 types:

1.classifying functions

2.converting functions

1.Classifying functions :

It is used to examine a character and to check if it belongs to given


classification.

function description

Iscntrl Control characters

Isprint Printable characters that is assigned


with a graphic

Isspace White space character:space


character( 32),horizontal tab(9),line
feed(10),vertical tab(11),form
feed(12) and carriage return(13)

Isgraph Character with printable graphic;all


printable characters except space.

isalnum checks whether the operand is


alphanumeric

ispunct checks whether the operand is


punctuation

isalpha checks whether the operand is


alphabetic

isupper checks whether the operand is an


uppercase
islower checks whether the operand is
lowercase

isdigit checks whether the operand is a digit

isxdigit checks whether the operand is


hexadecimal

Isodigit Checks whether the operand is octal


digit or not

Example program:

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

main()

char c;

clrscr();

printf(“enter a character\n”);

scanf(“%c”,&c);

if(islower(c))

printf(“entered character is lowercase letter\n”);

else if(isupper(c))

printf(“entered character is uppercase letter\n”);

else

printf(“invalid character\n”);

getch();

}
2.converting functions:

• It is used to convert the character from one case to another.


• There are 2 converting functions
o touppercase:converts lower to uppercase letters.if not lowercase
returns unchanged.
o tolowercase: converts upper to lowercase letters.if not uppercase
returns unchanged.

Repetition/iterative statements/loops:

• Iterative statements are used to repeat the execution of a sequence of


statements until the specified expression becomes false. C supports three
types of iterative statements also known as looping statements.
Types of Loops:
• Depending upon the position of a control statement in a program, a loop
is classified into two types:
1. Entry controlled loop:
o In an entry controlled loop, a condition is checked before
executing the body of a loop. It is also called as a pre-checking
loop.
o Ex:while,for

2. Exit controlled loop:


o In an exit controlled loop, a condition is checked after executing the
body of a loop. It is also called as a post-checking loop
o Ex:do-while
• C supports three types of iterative statements. They are
o while loop
o do–while loop
o for loop
1.while loop:
• The while loop provides a mechanism to repeat one or more statements
while a particular condition is true.
Syntax:
While (condition )
{
Statements block;
}
• In the while loop, the condition is tested before any of the statements in the
statement block is executed.
• If the condition is true, only then the statements will be executed, otherwise
if the condition is false, the control will jump to statement y, that is the
immediate statement outside the while loop block.
Example program to calculate the sum of n natural numbers using while
loop:
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,sum=0;n;
clrscr();
printf(“enter the value of n\n”);
scanf(“%d”,&n);
while(i<=n)
{
sum=sum+I;
i++;
}
printf(“sum is %d\n”,sum);
getch();
}
Do-While loop:
• The do–while loop is similar to the while loop.
• The only difference is that in a do–while loop, the test condition is tested
at the end of the loop.
• As the test condition is evaluated at the end, this means that the body of
the loop gets executed at least one time (even if the condition is false).
Syntax:
do
{
\\Statements block;
}
while (expression);
• The do–while loop continues to execute while the condition is true and
when the condition becomes false, the control jumps to the statement
following the do–while loop.
• The major disadvantage of using a do–while loop is that it always executes
at least once, so even if the user enters some invalid data, the loop will
execute.

Example program to calculate the sum of n natural numbers using do-


while loop:
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,sum=0;n;
clrscr();
printf(“enter the value of n\n”);
scanf(“%d”,&n);
do
{
sum=sum+i;
i++;
}
while(i<=n)
printf(“sum is %d\n”,sum);
getch();
}
for Loop:
• Like the while and do–while loops, the for loop provides a mechanism to
repeat a task till a particular condition is true.
• For loop is usually known as a determinate or definite loop because the
programmer knows exactly how many times the loop will repeat.
Synax:

for(initialization;condition;incrementation/decrementation)
{
Statement block;
}

• The initialization step is executed first.


• Next, the condition is evaluated. if true, the loop body is executed. If false,
the body of the loop doesn’t execute and the flow jumps to the next
statement.
• After the loop executes, the flow of control jump back to the
increment/decrement operation.

Example program to calculate the sum of n natural numbers using do-


while loop:
#include<stdio.h>
#include<conio.h>
main()
{
int i,sum=0;n;
clrscr();
printf(“enter the value of n\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf(“sum is %d\n”,sum);
getch();
}
Continue Statement:

• When the compiler encounter a continue statement then the rest of the
statements in the loop are skipped and the control is unconditionally
transferred to the loop continuation portion of the nearest enclosing loop.

Syntax/general form:
Continue;

Example program using continue keyword:

#include<stdio.h>

#include<conio.h>

main()

int i;

clrscr();

for(i=1;i<=10;i++)

if(i==5)

continue;

printf("the value of i is %d\n",i);

getch();

Goto statement:

• The goto statement is used to transfer control to a specified label.


• However,the label must reside in the same function and can appearonly
before one statement in the same function.
Syntax:
goto label;
…………….
……………. Forward jump
Label:
Statements;
Label:
Statements;
…………….. backward jump
……………..
goto label;

Example program to demonstrate goto statement:

#include<stdio.h>

#include<conio.h>

main()

{
clrscr();

printf(“statement1\n”);

printf(“statement2\n”);

goto label;

printf(“statement3\n”);

printf(“statement4\n”);

label:

printf(“statement5\n”);

printf(“statement6\n”);

getch();

You might also like