CP Unit 2
CP Unit 2
Conditional statements
Goto statements in C
Control Structures
Program Control Structure: The structure of the program which decides the order of
program statements execution is called ‘control structure’. There are three types of
control structures.
1. Sequence: In this control structure, the instructions are executed linearly from
top to bottom
2. Selection: This control structure makes decision according to some predefined
condition.
3. Iteration: This control structure executes certain instructions repeatedly
until the desired condition is satisfied.
Conditional (selection) Statements:
The conditional statements are mainly used for Decision Making. There are 3 ways
of statements
a). Single –way selection b). Two-way selection c). Multi-way selection
Next statements
Properties of if statement:
*. If the condition is true, then the simple or compound condition statements are
executed.\
*. If the condition is false, it does not do anything.
*. The condition is parenthesis and must be evaluated as true (non-zero value) or
false (zero).
*. If the compound structure is provided, it must be enclosed in opening and closing
braces.
The following program illustrates the use of if statement.(null else)
/* Program to check whether the entered number is less
than 25*/ #include<stdio.h>
main()
{
int i;
printf("\nEnter the number < 10. ");
scanf("%d",&i);
if(i<10)
printf("\nThe entered number %d is < 10", i);
}
if(conditional-statement)
False
{ Te True
statement-block1; st
} conditional
Statement block
else expression
{
Statement-xStatement block
statement-block2;
}
statement x;
Next statements
Example : Program to find floating point input using if-else statement.
#include<stdio.h>
main()
{
float years,
seconds; int life;
printf("Input you age in years : ");
life=scanf("%f",&years
); if(life==0)
printf("The input is not a floating-point
number.\n"); else
{
seconds=years*365*24*60*60;
printf("You have lived for %f seconds\n",seconds);
}
getch();
}
if(conditional-expression 1)
{
if(conditional-expression 2) False T True
{
statement-1;
e
} st
else conditional
{ expression-1
statement-2;
}
}
else
{
statement-2; F T True
}
al e
statement x;
se st
Statement block
conditional
expression-2
Statement-x
Next statements
/* Write a Program to read experience and cadre and display salary through
Monitor */ #include<stdio.h>
main()
{
int ch, expr;
printf("Enter 1 for principal: ");
printf("Enter 2 for Professor: ");
printf("Enter 3 for Asst-
professor: "); scanf("%d”,&ch);
printf("Enter your experience:");
scanf("%d”,&exp);
if(ch==1)
if(exp>10)
printf("Salry is
30000:"); else
printf("Salary is 25000\
n"); else if(ch==2)
if(exp>6)
printf("Salry is
23000:"); else
printf("Salary is 20000\
n"); else if(ch==3)
if(exp>3)
printf("Salry is
16000:"); else
printf("Salary is 12000\n");
else
printf(“Entered choice is
Invalid”); getch();
}
statement--x;
/* Program that reads marks in 3 subjects, calculate average marks and assigns
grade as per following specifications */
If marks Then Grade
>=90 A
75-90 B
60-75 C
50-60 D
<50 Fail
#include<stdio.h>
main()
{
float m1 ,m2 ,m3 ,average;
printf(“Enter the marks in three
subjects\n”); scanf(“%d%d%d”,&m1,
&m2, &m3); average=(m1+m2+m3);
printf (“Average marks are:%f\n”,
average); if(average>=90)
printf (“Grade is A \n”);
else if (average>=75 &&
average<90) printf (“Grade is B \
n”);
else if (average>=60 &&
average<75) printf (“Grade is C \
n”);
else if (average>=50 &&
average<60) printf (“Grade is
D \n”);
else
printf(“Fail \
n”); getch();
}
case 5
p
r
i
n
t
f
(
"
T
o
d
a
y
i
s
T
h
u
r
s
d
a
y
”
)
;
b
r
e
a
k
;
printf("Today is Friday”);
brea
k; case 3 :
printf("Today is Saturday”);
break;
default:
printf("Invalid
Day”); break;
}
getch();
}
'C' supports the nested switch( ) statements. The inner switch( ) statement can be a
part of an outer switch( ) statement. The inner and outer switch ( ) case constants
may be same. No conflict arises even if they are same.
Rules for writing switch() statement:
*. The expression in switch statement must be an integer value or a character
constant.
*. No real numbers are used in an expression.
*. Each case and default blocks must be terminated with break statements.
*. The default is optional and can be placed anywhere, but usually placed at end.
*. The case keyword must terminate with colon (:)
*. No two case constants are identical.
*. The case labels must be constants.
*. The switch can be nested.
while #include<stdio.h>
{ False main()
............. Condition {
Body of the int i,
loop sum=0;
True
............. while(i<=
} 5)
Body of the Loop
{
sum=sum+
i; i++;
}
printf("The sum of numbers upto 5 is %d:",sum);
}
Explanation: Here, first evaluates whether 'i' is less than or equal to 5. At first i=1 so the
codition is true, now sum is added with the value of 'i' and get sum as one, then 'i' is
incremented by 1, then it evaluates the condition again. This process is going on till the
condition is false. When the condition is false the printf() statement is executed and
displays the result.
do-while Loop / Posttest Loop :
It is also repetitive control structure and executes the body of the loop once
irrespective of the condition, and then it checks the condition and continues the
execution until the condition becomes false.
The statements within the body of the loop is executed once, then it evaluates for
the condition, if it is true, then it executes body until the condition becomes false.
Explanation: In the above program the value of i is evaluated as 1 and value of sum is
initialized as 0. In this program the do..while is executed tje body and calculates the sum and
increment the i value by 1 until the condition satisfies i<=10. Then prints sum value as
output.
The do...while command strcure
for Loop
The for loop is another repetitive control structure, and is used to execute set
of instructions repeatedly until the condition becomes false.
The assignment, incrementation or decrementation and condition checking
is done in for statement only, where as other control structures are not
offered all these features in one statement.
For loop has three parts
1. Initialize counter.
2. Test condition is used to initialize counter variable.
3. Increment/decrement is used to increment or decrement counter variable.
S. While do..While
No
1 This is the top tested This is the bottom tested
loop loop
2 The condition is first It executes body once,
tested if the condition after it checks the
os true then the block condition, if it is true the
is executed until the body is executed until
condition becomes the condition becomes
false false.
3 Loop will not be Loop is executed atleast
executed if the once even through the
condition is false condition is false.
for(i=1;i<=10;i++)
{
if(i==6)
break; /* break statement
*/ printf("%d", i);
}
}
}
n;
#include <stdio.h>
int main () {
for( ; ; ) {
printf("This loop will run forever.\n");
}
return 0;
}
When the conditional expression is absent, it is assumed to be true. You may have
an initialization and increment expression, but C programmers more commonly
use the for(;;) construct to signify an infinite loop.