Unit-2-Cprg-ppt
Unit-2-Cprg-ppt
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)
{
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