Need of conditional execution:
A C program executes procedurally statement after statement
in a pre-expected order.
In order to develop real-time applications, we need to
conditionally execute the program.
Explanation through example:
If we want to accept any number and want to print whether
the number is a zero or a non-zero, then a procedural program alone can’t
solve.
Example :
#include<stdio.h>
void main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
printf("Zero");
printf("\n Non-zero");
}
Execution 1:
Enter any number: 12
Zero
Non-zero
Execution 2:
Enter any number: 0
Zero
Non-zero
Example explained:
The above example gives the same output for any input
because program is not conditionally executing.
When the input is 12 then printf("Zero"); must be
skipped and printf("Non-zero"); must be executed.
When the input is 0 then printf("Zero"); must be
executed and printf("Non-zero"); must be skipped.
It requires conditional execution.
C language provides number of control structures to promote
conditional execution.
Control structures/ Control Statement:
Control structure or control statement is a statement that
controls the program execution.
Control structures in C language are classified into.
Decision making control structures are used to skip or
execute a statement or a block of statements.
Looping or iterative control structures are used to execute
a statement or a block of statements repeatedly until a condition is false.
Need of condition:
Both the decision and looping control structures controls
the execution of statements according to the condition.
A condition is a relational or logical expression, the
result of which is always either true (1) or false (0).
Relational operators:
Operator |
Meaning |
< |
Less than |
> |
Greater than |
< = |
Less than or
equal to |
> = |
Greater than
or equal to |
= = |
Equal to |
! = |
Not Equal to |
Note:
In C language ‘=’ (assigning operator) is used to assign a
value or result of an expression to the variable.
‘= =" is used to compare any two values, the result of
which is either true (1) or false (0).
Relational expression:
Combination of operands, relational operators and constants
is called a relational expression.
Any relational expression returns (results) either true or
false.
In C language true is represented with 1 and false is
represented with 0.
Example:
#include<stdio.h>
void main()
{
int a,b,c,d,e,f;
a=10<20 b="40">80;
c=20<=30;
d=48>=80;
e=20==30;
f=20!=30;
printf("a=%d \n b=%d \n c=%d \n d=%d \n e=%d \n f=%d",a,b,c,d,e,f);
}
20>
Output:
a=1
b=0
c=1
d=0
e=0
f=1
Example explained:
In C language any relational expression returns either true
(1) or false (0).
if conditional control structure:
It is a basic and fundamental control structure used to
execute or skip a statement or block of statements.
According to the syntax
The condition (relational expression) is placed within ( ).
If the condition is true, then the statement or block of
statements under if conditional statement is executed, otherwise skipped from
the execution.
Multiple statements can be controlled by placing within a
block { }
Example :
#include<stdio.h>
void main()
{
printf("Zero");
if(10<<20 if="" none="" printf="">40)
printf("\n one");
if(10>40)
printf("\n Two");
if(1)
printf("\n three");
20>
if(0)
printf("\n Four");
printf("five");
<20 if="" none="" printf="">}
20>
Output:
One
threeExample explained:
printf("Zero"); is executed as a normal statement.
printf("\nOne"); is executed because 10<40
returns true (1)
printf("\nTwo"); is skipped from execution because
10>40 returns false (0)
printf("\nThree"); is executed because 1 is
considered as true
printf("\nFour"); is executed not executed because
0 is considered as false
printf("\nFive"); is executed as a normal
statement.
Let us practice some examples:
Specification 1:
Accept a number and print whether the number is a positive,
negative or zero.
Program :
#include<stdio.h>
void main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if(n==0)
printf("Zero");
if(n>0)
printf("Positive number");
if(n<0)
printf("negitive number");
Execution 1 :
Enter any number: 25
Positive number
Execution 2 :
Enter any number: -40
Negative number
Execution 3 :
Enter any number: 0
Zero
Example explained:
At any given case only a single condition will be true, left
all the conditions are false.
Specification 2 :
Accept any number and print whether the given number is an
even or odd.
Program :
#include<stdio.h>
void main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
if(n%2==0)
printf("Even number");
if(n%2==1)
printf("Odd number");
}
Execution 1 :
Enter any number: 25
Odd number
Execution 2 :
Enter any number: 24
Even number
Specification 3 :
Accept two numbers and print whether equal, if not then print the
biggest number.
Program :
#include<stdio.h>
void main()
{
int a,b;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
if(a==b)
printf("Equals");
if(a>b)
printf("Biggest number %d",a);
if(b>a)
printf("Biggest number %d",b);
}
Execution 1 :
Enter two numbers: 10
10
Equals
Execution 2 :
Enter two numbers: 40
80
Biggest number 80
Execution 3:
Enter two numbers: 80 20
Biggest number 80
Specification 4:
Accept the price, quantity of a product and print the bill,
discount and the net bill. Allow 5% discount if bill exceeds to 500 else allow
3% discount.
Program :
#include<stdio.h>
void main()
{
int qty;
float price,bill,dis,net_bill;
printf("Price:");
scanf("%f",&price);
printf("Quantity:");
scanf("%d",&qty);
bill=price*qty;
if(bill>500)
dis=bill*5/100;
if(bill<=500)
dis=bill*3/100;
net_bill=bill-dis;
printf("Bill %f",bill);
printf("\n Discount %f",dis);
printf("\n Net bill %f",net_bill);
}
Execution 1:
Price: 20
Quantity: 5
Bill 100
Discount 3
Net bill 97
Execution 2:
Price: 100
Quantity 10
Bill 1000
Discount 50
Net bill 950
Specification 5:
Accept any digit from 0 to 6 and print appropriate day. For
example Sunday for 0, Monday for 1… Saturday for 6
Program:
#include<stdio.h>
void main()
{
int n;
printf("Enter any digit from 0 to 6:");
scanf("%d",&n);
if(n==0)
printf("Sunday");
if(n==1)
printf("Monday");
if(n==2)
printf("Tuesday");
if(n==3)
printf("Wednesday");
if(n==4)
printf("Thursday");
if(n==5)
printf("Friday");
if(n==6)
printf("Saturday");
}
Execution 1:
Enter any digit from 0 to 6 : 3
Wednesday
Execution 2:
Enter any digit from 0 to 6: 1
Monday
Summery :-
A problem may have any number of possibilities; we need to
write a condition for each possibility and solution under every condition to be
executed if the condition is true.
0 comments:
Post a Comment