Unit 2 Control Flow
1. while loop:- The while statement can be written as:
}
LOOPS
Loops are used when we want to execute a part of the program or a block of
statements several times. For example, suppose we want to print "C is best" 10 times.
One way to get the desired output is we write 10 printf statements, which is not preferable. Other way out is - use loop. Using loop we can write one loop statement and only one printf statement, and this approach is definitely better than the first one.
With the help of loop we can execute a part of the program repeatedly till some condition is true. There are three loop statements in C.
while
do while
for
1. while loop:- The while statement can be written as:
while(condition) while(condition)
statement; {
statement;
statement;
...
}
First the condition is evaluated; if it is true then the statements in the body of loop are executed . After the execution. again the condition is checked and if it is found to be true then again the statements in the body of loop are executed. This means that these statements are executed continuously till the condition is true and when it becomes false, the loop terminates and the control comes out of the loop. Each execution of the loop body is known as iteration.
/* program to find the product of digits of any number
#include<stdio.h>
#include<conio.h>
int main() {
int n,prod=1,rem;
printf("Enter the number ");
scanf("%d",&n);
while(n>0) {
rem=n%10;
prod=prod*rem;
n=n/10;
}
printf("prod of digits = %d \n",prod);
getch();
return 0;
}
/* program to find the factorial of any number */
#include<stdio.h>
#include<conio.h>
void main() {
int n,num;
long fact=1;
printf("Enter the number");
scanf("%d", &n);
if(n<0)
printf("No factorial of negative number\n");
else {
while(n>1) {
fact=fact *n;
n- -;
}
printf("Factorial of %d = %ld\n",num, fact);
} }
do...while loop
The 'do...while' statement is also used for looping. The body of this loop may contain a single statement or a block of statements. The syntax for writing this loop is
do do
statment; {
while(condition); statement;
statement;
...
}while(condition);
Here firstly the statements inside loop body are executed and then the condition is evaluated. If the condition is true, then again the loop body is executed and this process continues until the condition becomes false. Note that unlike while loop, here a semicolon is placed after the condition.
In a 'while' loop, first the condition is evaluated and then the statements are executed whereas in a do while loop, First the statements are executed and then the condition is evaluated. So if initially the condition is false the while loop will not executed at all, whereas the do while loop will always execute at least once.
/* program to count the digits in any number */
#include<stdio.h>
#include<conio.h>
void main() {
int n,count=0, rem;
printf("Enter the number : ");
scanf("%d",&n);
do {
n=n/10;
count++;
}while(n>0);
printf("Number of digits = %d\n ",count);
getch();
}
for loop
The 'for ' statement is very useful while programming in c. It has three expressions and semicolons are used for separation these expressions. The 'for' statement can be written
as-for(expression1;expression2;expression3) statement;
for(expression1;expression2;expression3) {
statement;
statement;
...
}
the loop body can be a single statement or block of statements.
expression1 is an initialization expression, expression2 is a test expression or
condition and expression3 is an update expression. expression1 is executed only onc when the loop starts and is used to initialize the loop variables. This expression is generally an assignment expression. expression2 is a condition and is tested before each iteration of the loop. This condition generally uses relational and logical operators. expression2 is an update expression and is executed each time after the body of the loop is executed.
Now let us see how this loop works. Firstly the initialization expression is executed and the loop variables are initialized, and then the condition is checked, if the
condition is true then the body of loop is executed. After executing the loop body, control transfers to expression3(update expression)and it modifies the loop variables and then again the condition is checked, and if it is true, the body of loop is executed.
this process continues till the condition is true and when the condition becomes false the loop is terminated and control is transferred to the statements following the loop.
/* program to generate Fibonacci series 1,1,2,3,5,8,13,34,55,89...
In this series each number is a sum of the previous two numbers */
#include<stdio.h>
#include<conio.h>
void main() {
long x, y, z;
int i,n;
x=0;
y=1;
printf("Enter the number of terms ");
scanf("%d",&n);
printf("%ld",y);
for(i=1;i<n;i++) {
z=x+y;
printf("%ld",z);
x=y;
y=z;
}
printf("\n");
}
Nesting of loops:- when a loop is written inside the body of another loop then it is known as nesting of loops. any type of loop can be nested inside any other type of loop. For example a for loop may be nested inside another for loop or inside a while or do while loop. Similarly while and do while loops can be nested.
/* program to understand nesting in for loop. The program prints Armstrong number.
Armstrong number is a three digit number in which the sum of cube of all digits is equal to the number, for example 371 is an Armstrong number since
371=33+73+13=27+343+1. */
#include<stdio.h>
#include<conio.h>
void main() {
int num, n, cube, d, sum;
printf("Armstrong numbers are :\n");
for(num=100;num<=999;num++) {
n=num;
sum=0;
while(n>0) /* inner loop */
{
d=n%10;
n/=10;
cube=d*d*d;
sum=sum + cube;
}/* end of while loop */
if(num= =sum) printf("%d\n", num);
}/* end of for loop */
getch();
}
Infinite loops:-The loops that go on executing infinitely and never terminate are called infinite loops. Sometimes we write these loops by mistake while sometimes we deliberately make use of these loops in our programs. Let us take some examples and see what type of mistakes lead to infinite loops.
A) for(i=0;i<=5;i--) printf("%d",i);
this loop will execute till the value of i is less then or equal to 5 i.e the loop will terminate only when i becomes grater then 5. the initial value of i is 0 and after each iteration its value is decreasing, hence it will never become greater than 5. So the loop condition will never become false and the loop will go on executing infinitely. For the loop to work correctly we should write i++ instead of i-- .
B) There should be a statement inside the loop body that changes the value of loop variable after each iteration. In for loop this work is done by the update expression but in while and do while we may forget to change the loop variable and this can lead to infinite loop.
int k=1;
do {
printf("%d",k);
sum=sum+k;
}while(k<5);
here we are not changing the value of k inside the loop body and hence the loop becomes infinite.
C) A common mistake made by beginners is to use the assignment operator(=) where equality operator(= = ) should have been used. if this mistake is made in the loop condition then it may cause the loop to execute infinitely. for example consider this
loop: while(n=2)
{
---}
here we wanted the loop to executed till the value of n is equal to 2. So we should have written n==2 but mistakenly we have written n =2. Now n=2 is an assignment expression and the value of this expression is 2. which is a nonzero (true )value and hence the loop condition is always true.
break statement
Break statement is used inside loops and switch statements. Sometimes it becomes necessary to come out of the loop even before the loop condition becomes false. In such a situation, break statement is used to terminate the loop. this statement exit from that loop in which this statement appears/ It can be written
as-break;
when break statement is encountered, loop is terminated and the control is transferred to the statement immediately after the loop. the break statement is generally written along with a condition. If break is written inside a nested loop structure then it causes exit from the innermost loop.
/* program to understand the use of break */
#include<stdio.h>
#include<conio.h>
void main() {
int n;
for(n=1;n<=5;n++) {
if(n = = 3) {
printf("I understand the use of break\n");
break;
}
printf("Number = %d\n",n);
}
printf("out of for loop\n");
getch();
}
output:- number =1 number = 2
I understand the use of break Out of for loop
continue statement
The continue statement is used when we want to go to the next iteration of the loop after skipping some statement of the loop. This continue statement can be written simply
as-continue;
it is generally used with a condition. When continue statement is encountered all the remaining statements (statement after continue )in the current iteration are not executed and the loop continues with the next iteration.
The difference between break and continue is that when break is encountered the loop terminates and the control is transferred to the next statement following the loop. but when a continue statement is encountered the loop is not terminated and the control is transferred to the beginning of the loop. In while and do- while loops, after continue statement the control is transferred to the test condition and then the loop continues, whereas in for loop after continue statement the control is transferred to update expression and then the condition is tested.
/* program to understand the use of continue statement */
#include<stdio.h>
#include<conio.h>
void main() {
int n;
for(n=1;n<=5;n++) {
if(n==3) {
printf("I understand the use of continue\n");
continue;
}
printf("number = %d\n", n);
}
printf("out of for loop\n");
getch();
}
output:-number =1 number = 2
I understand the use of continue number = 4
number = 5 out of for loop