0% found this document useful (0 votes)
2 views65 pages

Unit-2-Cprg-ppt

The document provides an overview of operators in C programming, categorizing them into arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators. It explains how these operators function, including their precedence and usage in expressions, as well as decision-making constructs like if statements, switch statements, and looping mechanisms. Additionally, it covers the syntax and examples for each operator and control structure.

Uploaded by

Anand Patil
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)
2 views65 pages

Unit-2-Cprg-ppt

The document provides an overview of operators in C programming, categorizing them into arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators. It explains how these operators function, including their precedence and usage in expressions, as well as decision-making constructs like if statements, switch statements, and looping mechanisms. Additionally, it covers the syntax and examples for each operator and control structure.

Uploaded by

Anand Patil
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/ 65

Operators

• An operator is a symbol that tells the computer to


perform some mathematical or logical manipulation
• Categories of C operators
– Arithmetic
– Relational
– Logical
– Assignment
– Increment, Decrement
– Conditional
– Bitwise
– Special
Arithmetic Operators
Operator Meaning Ex (a=14, b=4)
+ Addition, unary plus a+b, +a
- Subtraction, unary minus a-b, –a
* Multiplication a*b
/ Division a/b
% Modulo division a%b
where a and b are variables and are called as operands
Integer arithmetic
• Both operands are integers, result will also be
integer
– Such expression called as integer expression
• If a = 14 and b = 4, then
a–b =10
a+b =18
a*b = 56
a/b = 3, decimal part truncated
a%b = 2, remainder of division
Real and mixed mode arithmetic
• Both operands are real, result will also be real
x = 6.0/7.0 = 0.857143
y = 11.0+3.25 = 13.25
z = 12.0*2.5 = 30.0
a = 13.5-2.4 = 11.1
• One operand is real and the other is integer,
then the expression is mixed mode
15/10.0 = 1.5
Relational operators
• Relational operators are used to compare two
operands.
• Expressions involving relational operators are called
relational expressions, their result will always be TRUE
or FALSE (Y or N, 1 or 0)
• 6 relational operators
< is less than a<b
<= is less than or equal to a<=b
> is greater than a>b
>= is greter than or equal to a>=b
== is equal to a==b
!= is not equal to a!=b
Logical Operators
• Used when two or more comparisons are
required
• Three types
&& logical AND (x<5)&&(y>10)
|| logical OR (age<25) || (birthyear>1999)
! logical NOT !(x<5)
• Also called compound relational operators
Assignment operators
• Used to assign result of expression(RHS) to
variable(LHS)
= or shorthand form op=
a=a+1 a += 1
a=a-3 a -= 3
a=a*(n+1) a*=n+1
a=a/(n-1) a/=n-1
a=a%b a%=b
Assignment operator
• Shorthand form has following advantages
– LHS need not be repeated on RHS, easier to write
– Shorter statement, easier to read
– Statement is more efficient in terms of processing
value(5*j-2) = value(5*j-2)*k;
can be written as
value(5*j-2)*=k;
Increment and decrement operator
• ++ & --
• ++ adds 1 to the operand, -- subtracts 1 from
the operand
• Both are called unary operators, because they
require only one operand
• Can be used as postfix operator, m++ and also
as prefix operator, ++m
++ and --
int b, c;
b=6;
c=3;
c=c*b++;
y=b+--c;
printf(“%d, %d, %d”, b, c, y);
• Postfix: operand++ (or operand--), the expression is
evaluated using old value of operand and then the operand
is incremented by 1.
• Prefix: ++operand (or --operand), the operand is
incremented by 1, first, then the expression is evaluated
using new value of operand.
Conditional operator
• For conditional expressions, a ternary operator(3
operands), is used as follows
exp1 ? exp2 : exp3;
• exp1, exp2 and exp3 are expressions
• exp1 is a relational expression(comparison) and is
evaluated first. If TRUE, then exp2 is evaluated or
otherwise exp3 is evaluated.
a=10; b=15;
x = (a>b) ? (a-b) : (b-a);
Bitwise operators
• These are used for manipulating data at bit level
• Used for testing bits, shifting bits left or right
• Cannot be used with float or double
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left
>> shift right
Special operators
• comma operator
• sizeof operator
• pointer operators, & and *
• member selection operators, . and ->
comma operator
• Used to link expressions together
• Comma linked expressions are evaluated left to
right, and value of the right most expression is
the value of the combined expression, eg.
value = (x=10, y=5, x+y);
• Also used for interchanging values
t=x, x=y, y=t;
• Also used in for and while loops
sizeof operator
• Gives the number of bytes that the operand occupies
• It is a compile time operator
• The operand may be a variable, constant or datatype
qualifier
• Used to find out size of arrays and structures.
• Also used to allocate memory space dynamically
• Egs.
int sum;
m = sizeof(sum);
y = sizeof(short int);
k = sizeof(67L);
Arithmetic expressions
• Combination of variable, constants and operators
axb–c a*b-c

a*b/c
3x2+2x+1 3*x*x+2*x+1
x/y+c
Evaluation of Expressions
variable = expression;
• When above statement is encountered, first
the expression on the right hand side is
evaluated, and then the result is assigned to
the variable on the left hand side
x = a*b-c;
z = a – b / c + d;
Precedence of operators
• Arithmetic expressions are evaluated from left to
right based on priority of operators
• High priority : * / %
• Low priority : + -
• Precedence can be changed by using parenthesis
()
9 – 12 / 3 + 3 * 2 – 1
9 – 12 / (3 + 3) * (2 – 1)
9 – (12 / (3 + 3) * 2) – 1
Operator Precedence, Associativity
• All operators have a level of precedence. This
decides which operators will be evaluated before
others.
– Higher level precedence operators are evaluated first,
followed by a lower level precedence operators
• There is also associativity property of operators
– where operators of same precedence are evaluated
either from ‘left to right’ or ‘right to left’ depending
on associativity
Decision making and branching
• Usually, C programs are a set of statements
executed sequentially in serial order
• Sometimes, it is necessary to change the order of
execution based on some condition
• This involves decision making to see if a particular
condition has occurred or not
• Decision making statements in C are
– If statement
– Switch statement
– Conditional operator statement
– Goto statement
If statement
• Two way decision making statement used with an
expression
if (test-expression)

• Some examples
1. if (bank balance is zero)
borrow money
2. if (room is dark)
Test No(False)
put on lights expression?
3. if (code is 1)
person is male Yes(True)
4. if (age is more than 55)
person is retired
Different forms of if statement
• Simple if statement
• if …… else statement
• Nested if …. else statement
• else if ladder
Simple if statement
Entry

if (test expression) Test True


{ expression?
statement-block; False Statement-block
} statement-x
statement-x;
• statement-block may be a single statement or a group of
statements
• If test expression is true, statement-block will be executed,
otherwise statement-block will be skipped and execution
will jump to statement-x
• When test expression is true, both statement-block and
statement-x will be executed.
Example of simple if statement
• Students who take part in sports should be
given bonus marks
if(category==‘S’)
{
marks = marks+bonus_marks;
}
printf(“%d”, marks);
Read percentage of marks and to display appropriate
message
/*prg to print result*/
#include<stdio.h>
float percent;
main()
{
printf(“enter the percentage”);
scanf(“%f”,&percent);
if(percent>=40.0)
printf(“Pass”);
if(percent<40.0)
printf(“Fail”);
}
The if…. else statement
• Used for two-way decision making
• Syntax of the if statement

if (test expression)
{
true block statement(s);
}
else
{
false block statement(s);
}
statement-x;
The if….. else statement
• If the condition
evaluates to true, one
set of instructions will
be executed.
• If the condition
evaluates to false,
another set of
instructions will be
executed.
Program to find largest of three
numbers
#include<stdio.h>
int a,b,c,big;
main()
{
printf(“Enter three numbers:”);
scanf(“%d%d%d”, &a,&b,&c);
if(a>b)
big=a;
else
big=b;
if(c>big)
big=c;
printf(“The largest number is %d”, big);
}
Nested if statement
• Used when a series of decisions are involved
• Nested means one inside the other
• Also called as cascaded if statement
• If the condition is evaluated to true in the
first if statement, then the condition in the
second if statement is evaluated and so on.
Nested if statement
Nested if statement
• Syntax is as follows
if (test condition1)
{
if (test condition2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
statement-x;
Example of nested if statement
• Bank announces if (sex is female)
bonus policy as {
follows if (balance>5000)
– A bonus of 2% of the bonus = .05*balance;
balance is given to else
everyone, for any
amount of balance bonus = .02*balance;
}
– A bonus of 5% is
else
given to female
account holders if {
balance if greater bonus = 0.02*balance;
than Rs. 5000 }
balance = balance + bonus;
Program to calculate percentage of marks
and display appropriate message
else if… ladder
• This is a multi-way selection statement
• Similar to the logical OR
• If first condition is true, then corresponding
set of instructions will be executed.
• If the condition is false, then the next
condition is checked and so on.
• If all the conditions fail, the statements in the
default block will be executed.
else if… ladder
else if … ladder
if (condition1)
{
statement(s);
}
else if (condition2)
{
statement(s);
}
else if (condition3)
{
statement(s);
}
else
{
default statement(s);
}
switch statement
• The else if… ladder becomes difficult to
understand if there are many conditions
• The switch statement is also a multi-way
selection statement but easier to understand
and implement when there are 3 or more
than 3 alternatives
switch statement
switch statement
• The switch statement switches between the
blocks based on the value of the expression. Each
block will have a value associated with it.
• The expression in the switch statement must
always reduce to an integer value.
• The expression in the switch statement can be
either an integer value or a character constant or
an expression which reduces to an integer value.
• The label for each block can be either an integer
value or a character constant.
switch statement
switch(expression)
{
case label1 : statement(s);
break;
case label2 : statement(s);
break;
case label3 : statement(s);
break;
.
.
.
default: statement(s);
break;
}
switch statement
switch statement
• Each block is represented using the case keyword
and the case keyword follows with the label of
the block.
• In a switch statement, both the default block and
the break statement are optional.
• If none of the blocks are matched, then the
statements in the default block are executed.
• Every block is ended with a break statement.
• If we remove the break statement from a
particular block, all the subsequent blocks are
also executed until the next break statement is
encountered.
Rules for switch statement
• The switch expression must be of an integer or character
type.
• The case value must be an integer or character constant.
• The case value can be used only inside the switch
statement.
• The break statement in switch case is not must. It is
optional. If there is no break statement found in the case,
all the cases will be executed present after the matched
case. It is known as fall through the state of C switch
statement.
• default label is optional. It will be executed if none of the
case values match.
• Only one default is allowed. And it can be placed anywhere.
• Nesting of switch statements is allowed
Program to calculate percentage of marks
and display appropriate message
Conditional operator
• For conditional expressions, a ternary operator(3
operands), is used as follows
exp1 ? exp2 : exp3;
• exp1, exp2 and exp3 are expressions
• exp1 is a relational expression(comparison) and is
evaluated first. If TRUE, then exp2 is evaluated or
otherwise exp3 is evaluated.
a=10; b=15;
x = (a>b) ? (a-b) : (b-a);
goto statement
• All branching statements seen so far are based
on some condition
• The goto statement is an unconditional
branching statement.
• By using goto statement, we can
– skip some instructions and jump forward in the
program or
– jump back and again repeat a set of instructions
goto statement - syntax
• goto statement can be used either for forward
jump or backward jump as shown below
Read numbers continuously from
keyboard until 999 is pressed (goto)
Factorial of a number (goto)
Decision Making and Looping
• goto statement(backward jump) allows
repetition of steps in a program.
• goto statement does not implement
structured programming, and care has to be
taken while using this for looping.
• There are other convenient methods for
performing looping
Looping
• Loop means a sequence of statements are
executed repeatedly till some condition is
satisfied.
• Looping has two segments
– body of loop: steps to be repeated
– Control statement: contains test condition
• Two kinds of looping:
– Entry controlled loop
– Exit controlled loop
Entry controlled loop
• Control conditions are tested
before the start of the loop
• If conditions are not satisfied,
the body of the loop will not
be executed
• Also called as pre-test loop
Exit controlled loop
• Control conditions are tested at
the end of the body of the loop
– This means that the statements in
the body of loop are executed
atleast once
• If conditions are satisfied, the
body of the loop will be executed
again and again
• Also called as post-test loop
Steps involved in looping
• 4 steps
– Setting and initialising condition variable
– Execution of statements in the loop
– Test for specified value of condition variable in order
to repeat loop
– Incrementing or updating the condition variable
• The test may be to
– Find whether loop has been repeated a number of
times (Counter-controlled loop)
– Determine whether a particular condition has been
met (sentinel-controlled loop)
Types of looping constructs in C
• The while statement
– Entry-controlled
• The do statement
– Exit-controlled
• The for statement
The while loop
• Simplest of all looping statements
• Syntax
while(test condition)
{
body of the loop
}
• Entry controlled loop – as test condition is
checked before executing the body of loop
• Test condition is evaluated, if true, then the
body of the loop is executed
• Then test condition is evaluated again, if
found true, the body of the loop is repeated
until the test condition becomes false
Program to evaluate y = xn, where n is non-negative
/* program to find y =xn*/
#include<stdio.h>
int x, n, y, i;
main()
{
printf(“Enter the values of x and n”);
scanf(“%d %d”, &x, &n);
y=x;
i = 1;
while(i<n)
{
y=y*x;
i=i+1;
}
printf( “The power of %d raised to %d is %d”, x, n, y);
}
Program to find factorial of a number
/* program to find factorial of a number*/
#include<stdio.h>
int n, i=1, fact=1;
main()
{
printf(“Enter the number:”);
scanf(“%d”, &n);
if (n<0)
printf(“No negative numbers allowed.”);
else
if(n==0)
printf(“The factorial of 0 is 1”);
else
{
while(i<=n)
{
fact=fact*i;
i=i+1;
}
printf(“The factorial of %d is %d”, n, fact);
}
}
The do… while loop
• The body of the loop will be executed at least one,
because this is a exit-controlled loop
• Syntax
do
{
body of loop
} while(test-condition);
• The body of the do…while loop is executed first and
then the condition is evaluated.
• If the value is true, the body of the loop is executed
again
• If the value is false, the execution of the body of the
loop stops.
The for loop
• Entry controlled loop
• Syntax
for (initiliazation; test-condition; incr/decr)
{
body of loop
}
• First the counter is initialized, and then the
condition is evaluated.
• If the value of the condition is true, the body
of the for loop is executed. Otherwise, the
body of the loop is not executed.
• After the execution of the for loop’s body, the
counter is either incremented or
decremented. Then the condition is
evaluated again and so on.
Program to display fibonacci series
1. Start
2. Read n
3. f1=0, f2=1
4. Print f1, f2
5. f3=f1+f2
6. if (f3<=n)
1. Print f3
2. f1=f2
3. f2=f3
4. f3=f1+f2
5. Goto 6
7. Stop
break statement
• Unconditional branch statement
• The break statement is used inside the looping statements
to break the execution of the loop.
• When the break statement is encountered inside the loop,
the execution of the body of the loop stops and the control
is given to the next instruction after the body of the loop.
• Syntax
loop
{
-----;
-----;
break;
-----;
}
statement;
continue statement
• The continue statement is used inside the looping
statements to skip the execution of a set of instructions and
return the control back to the loop.
• When a continue statement is encountered within the
body of the loop, the statements after
the continue statement are skipped and the control is
passed back to the loop.
• Syntax
loop
{
-----;
-----;
continue;
-----;
}
statement;
Program to check if a number is a
palindrome

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy