C Programming Control Statements Notes
C Programming Control Statements Notes
Control statements
Control statements are used to execute/transfer the control from one part of
the program to another depending on a condition.
if-else statement
switch statement
if-else statement
if ( condition )
{ Example
} da = 0.72 * bpay;
hra = 0.12 * bpay ;
cca = 250;
where <condition> is a
logical expression which will }
have the value true or false.
When this statement is executed, the computer evaluates/checks the value of the
condition.
If the value of the condition is true, then the statements in <true block> will be
executed.
Otherwise the statements in the <false block> will be executed.
The following points in relation to the use of if-else statement are to be taken
care of.
if(condition 1)
if(condition 2)
{
Example
<true block 1>
}
else
if(a > b)
{
<false block 1>
}
if(a > c)
else
big = a;
if(condition 3)
else
{
big = c;
<true block 2>
}
else
else
{
<false block 2>
if(b > c)
}
big = b;
It is to be noted that any one of the
else
statement blocks in the nested
big = c;
if statement will be executed
depending on the value of the
conditions.
switch Statement
Example
switch ( <expression>)
switch (sex)
{
{
case <label 1>: {
case ‗m‘:
<statement block 1>
{
break;
male++ ;
}
total++;
case <label 2>: {
break;
<statement block 1>
}
break ;
case ‗f‘:
}
{
case <label n>: {
female++;
<statement block n>
total++;
break;
break;
}
}
default:
default:
{
printf(―\n Error in sex
<default statement block>
code‖);
break;
break;
}
}
}
When this statement is
where <expression> refers any int or char
executed, the computer
expression or variable.
checks the values of the
<label 1>, <label 2>...<label n> are values
expression or variable. If this
which will match with the value of the
value matches with any one of
expression.
the labels given in
break is a statement which will transfer the
case <value>, then that
control to the end of switch statement.
statement block will be
executed.
Note:
The braces{} can be omitted when there is only one statement available in the
statement block.
The break statement is used to transfer the control to the end of switch statement.
The default block is executed when none of the case labels matches with the
value of the expression/variable.
The default block is optional; i.e., this may be omitted while writing a program.
It should also be noted that a switch statement is compact and can be used to
replace a nested if statement.
Loop control structures are used to execute and repeat a block of statements
depending on the value of a condition. There are three types of loop control
structures/statements in C language.
The statement block of a for loop lies completely inside the block of another for
loop.
This is referred as nested for loop or nested for statement. Any number of for
statements can be nested.
------------ |
}——————————————————————
When this statement is executed, the computer assigns i = 1 and the inner loop is
executed 5 times for j values from 1 to 5 in steps of 1.
Now i is incremented by 1 (i.e. i = 2) and the inner loop is executed 5 times for j
values from 1 to 5. Then i becomes 3 and the inner loop is executed 5 times.
Note that the statement block is executed and repeated 15 times (3 × 5 = 15).
Example
i = 1;
while(<condition>)
while(i <= 10)
{
{
s = s + i;
––––––––––––––––
p = p * i;
<statement block>
i++;
––––––––––––––––
}
When this statement is executed, the
}
computer evaluates the value of the
where <condition> is a relational
condition. If the value is true, the
or logical expression which will
statement block is executed and is
have the value true or false.
repeated until the value of the
condition is false.
_________________________________________________________________
There must be a statement written inside the statement block to change the
value of the condition, otherwise the loop is infinity.
_________________________________________________________________
Example
i=1;
do
do
{
{
s = s + i;
------------
p = p * i;
<statement block>
i++;
------------
}
}
while(i <= 10);
while (<condition>);
When this statement is executed, the
where <condition> is
computer will execute the statement block
a relational or logical
irrespective of the value of the condition.
expression which will
At the end of statement block, the
have the value true or
condition is evaluated. If the value of the
false.
condition is true the statement block is
executed again and is repeated until the
condition is false.
__________________________________________________________________
Note that the statement block is executed at least once for any value (true or
false) of the condition. It is necessary to have a statement inside the block to
change the value of the condition, otherwise the loop is infinity.
goto Statement
goto label;
where label is the statement label which is available anywhere in the program.
Example
------------;
goto display;
------------;
------------;
display:
------------;
break Statement
The break statement is used to transfer the control to the end of a statement block
in a loop. It is an unavoidable statement to transfer the control to the end of a
switch statement after executing any one statement block.
break;
continue Statement
continue;
When this statement is executed, the control is transferred to the beginning of the
loop such that the loop is repeated 80 times irrespective of the key pressed.
Note that the message ―C for continue is pressed‖ is displayed whenever the key
C is pressed.
exit( ) Statement
The exit() function is used to transfer the control to the end of a program (i.e. to
terminate the program execution). It uses one argument in () and the value is zero
for normal termination or non-zero for abnormal termination.
if (n < 0)
{
printf("\n Factorial is not available for negative numbers");
exit(0);
}
------------;
Note that the program execution is terminated when the value of the variable
n is negative.
The compiler directive #include <stdlib> is used when this function is
used in a program.
int a,b,c,big;
clrscr();
printf("\n Enter three numbers : ");
scanf("%d %d %d",&a,&b,&c);
if(a > b)
if(a > c)
big = a;
else
big = c;
else
if(b > c)
big = b;
else
big = c;
printf("\n Biggest number is %d",big);
getch();
y(x,n) = 1 + x when n = 1
1 + x/n when n = 2
1 + xˆn when n = 3
1 + nx when n > 3 or n < 1
Solution
int n;
float x,y;
clrscr();
printf("\n Enter value to x and n : ");
scanf("%f %d",&x,&n);
if(n == 1)
y = 1 + x;
else
if(n == 2)
y = 1 + x/n;
else
if(n == 3)
y = 1 + pow(x,n);
else
y = 1 + n*x;
printf("\n Value of y(x,n) = %6.2f",y);
getch();
Solution
int sales;
float comm;
clrscr();
printf("\n Sales amount ? : ");
scanf("%d",&sales);
if(sales <= 500)
comm = 0.05*sales;
else
if(sales <= 2000)
comm = 35 + 0.10*(sales - 500);
else
if(sales <= 5000)
comm = 185 + 0.12*(sales - 2000);
else
comm = 0.125*sales;
printf("\n Commission Amount Rs.%0.2f",comm);
getch();
Solution
float a,b,c,d,x1,x2,x;
clrscr();
printf("\n Enter coefficients a,b and c : ");
scanf("%f %f %f",&a,&b,&c);
d = b*b - 4*a*c;
if(d > 0)
{
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b - sqrt(d))/(2*a);
printf("\n Roots are real and unequal");
printf("\n %6.2f %6.2f",x1,x2);
}
else
if(d == 0)
{
x = -b/(2*a);
printf("\n Roots are real and equal");
printf("\n %6.2f",x);
}
else
printf("\n No Real roots,roots are complex");
getch();
Solution
Solution
A nested if statement is used here to calculate and print the amount to be paid by
the consumer.
Solution
int ts,rh,cc;
clrscr();
printf(―\n Enter tensile strength : ―);
scanf(―%d‖,&ts);
printf(―\n Enter rockwell hardness : ―);
scanf(―%d‖,&rh);
printf(―\n Enter carbon content : ―);
scanf(―%d‖,&cc);
if(ts >= 700)
if(rh >= 200)
if(cc <= 6)
printf(―\n Grade is A‖);
else
printf(―\n Grade is B‖);
else
if(cc <= 6)
printf(―\n Grade is C‖);
else
printf(―\n Grade is E‖);
else
if(rh >= 200)
if(cc <= 6)
printf(―\n Grade is D‖);
else
printf(―\n Grade is E‖);
else
if(cc <= 6)
printf(―\n Grade is E‖);
else
printf(―\n Grade is F‖);
getch();
Solution
int n,i;
clrscr();
printf(―\n Enter value to n : ―);
scanf(―%d‖,&n);
/* loop to generate and print natural numbers */
for(i = 1; i <= n; i++)
printf(―%8d‖,i);
getch();
float x,y;
clrscr();
printf(―\n-----------------‖);
printf(―\n X Y ―);
printf(―\n-----------------‖);
/* loop to generate and print X and Y */
for(x = 1.0; x <= 3.0; x = x + 0.2)
y = 1.36*sqrt(1+x+x*x*x)+pow(x,1.0/4)+exp(x);
printf(―\n %6.2f %6.2f ― ,x,y);
printf(―\n-----------------‖);
getch();
Solution
Solution
A nested for loop can be written to find the sum of the series. The
inner loop is used to find the term values 1, (1+2), (1+2+3) and so
on. These are added in outer loop to get the sum of the series.
Solution
Solution
Solution
Solution
Note that the digits separated are used to get the cubes and the
cubes are added to get the sum. The sum obtained is compared
with the given number and the result in printed.
Solution
First of all the given number is reversed. Then the digits are
separated to get the first digit of the number. Finally the
corresponding word will be printed.
Solution
A for loop can be used to find the divisor of the given number
between 2 and n/2 ( for example, when n = 23, it has divisors upto
11 [i.e. 23/2 =11]).
An identification variable flag is used to identify any
divisors for the given number other than 1. It is assumed flag = 0
initially; when a divisor is identified it will be changed to flag = 1.
Based on the value of flag, the loop will be terminated and
the prime number is identified.
Solution
Note that a term in the series is obtained by adding previous two
terms. Various terms are generated and printed using the steps given
below.
Step 1 : Read n.
Step 2 : Assume n1=0; n2=1 ;
Step 3 : Print n1, n2
Step 4 : newterm = n1 + n2 ;
Step 5 : Print newterm
Step 6 : n1 ← n2
Step 7 : n2 ← newterm ;
Step 8 : Repeat step 4 to step 7 until newterm > n ;
Solution
The nested for loops can be used to print the multiplication tables.
(i) The outer loop is written to print tables from 1 to 5.
(ii) The inner loop is written to generate and print products from 1 to
10.
Using nested for loop, a C program is written as follows to print the
multiplication tables.