Looping concept
The program that we have developed so far used either a sequential or a decision control instruction.
The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through a loop control instruction.
Loops can be created to executed a block of code for a fixed number of times. Alternatively, loops can be created to repetitively execute a block of code until a Boolean condition changes state.
For instance, the loop may continue until a condition changes from false to true, or from true to false. In this case, the block o code being executed must update the condition being tested in order for the loop to terminate at some point. If the test condition is not modified somehow within the loop, the loop will never terminate. This created a programming bug known as an infinite loop.
Loops can be created to executed a block of code for a fixed number of times. Alternatively, loops can be created to repetitively execute a block of code until a Boolean condition changes state.
For instance, the loop may continue until a condition changes from false to true, or from true to false. In this case, the block o code being executed must update the condition being tested in order for the loop to terminate at some point. If the test condition is not modified somehow within the loop, the loop will never terminate. This created a programming bug known as an infinite loop.
There are three method by way to which we can repeat a part of a program. They are:
- Using a for statement
- using a while statement
- Using a do-while statement
for Loop
At this point you think, there is already while loop function then what we need one more loop function? The answer is that for is easy to use and allows us to specify three things in single line, these three things are:
- Setting a loop counter to an initial value
- Testing the loop counter to determine whether its value has reached the number of repetitions desired.
- Increasing or Decreasing the value of loop counter.
The general form of for statement is as under:
for(initialise counter; test counter;increment/decrement counter)
{
do this;
and this;
and this;
and that;
}
It is important to note that the initialization,testing and incrementation part of a for loop can be replaced by any valid expression, thus the following for loops are valid:
for(i=5; i; i--)
printf("%d",i);
for(i<=5; j=10; j=0)
printf("%d",i);
for(i=1; i<=10; printf("%d",i++))
;
for(scanf("%d",&i); i<=5; i++)
printf("%d",i);
Demonstration of for loop
/*demonstration for loop program*/
/*program to print cprogrammingcodes in five times*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1; i<=5; i++)
{
printf("cprogrammingcodes");
printf("\n");
}
getch();
}
Output of above program:
cprogrammingcodes
cprogrammingcodes
cprogrammingcodes
cprogrammingcodes
cprogrammingcodes
Versions of for loops
Now we learn how many different types for uses to print one to ten numbers.
(a) #include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1; i<=10; i++)
printf("%d\n",i);
}
(b) #include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1; i<=10;)
printf("%d\n",i);
i++;
}
(c) #include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
i=1;
for(; i<=10; i++)
printf("%d\n",i);
}
(d) #include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
i=1;
for(; i<=10 ;)
printf("%d\n",i);
i++;
}
(e) #include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1; i++<=10;)
printf("%d\n",i);
}
(f) #include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=0; ++i<=10;)
printf("%d\n",i);
}
(g) #include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
i=1
for(; i++<=10;)
printf("%d\n",i);
}
Multiple Initialisations and increment/decrement in the for loop
The initialisation expression of the for loop can contain more than one statement by a comma.For example:
for(i=0, j=10; i<=j ; i++,j--)
Thus we see, that multiple statement can uses to initialisation and increment(or decrement). But notice there must be only one statement allowed in the test expression. This expression may contain several condition linked using logical operators as:
for(i=0, j=10; i<=j || i==j ; i++,j--)
The way if statement can be nested, similarly whiles and fors can also be nested. Let's understand the nesting of for loops through program:
for(i=0, j=10; i<=j || i==j ; i++,j--)
Nesting of for Loops
The way if statement can be nested, similarly whiles and fors can also be nested. Let's understand the nesting of for loops through program:
/*demonstration of nested for loop*/
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,s;
clrscr();
for(r=1; r<=2; r++)/*outer loop*/
{
for(c=1; c<=2; c++) /*inner loop*/
{
s=r+c;
printf("r=%d c=%d sum=%d\n",r,c,s);
}
}
getch();
}
for(c=1; c<=2; c++) /*inner loop*/
{
s=r+c;
printf("r=%d c=%d sum=%d\n",r,c,s);
}
}
getch();
}
output of above program
r=1 c=1 sum=2
r=1 c=2 sum=3
r=2 c=1 sum=3
r=2 c=2 sum=4
Thus we see in above program that, the body of outer loop is indented, and the body of the inner for loop is further indented. These multiple indentations make the program easier to understand.
while loop
It is often the case in programming that we want to do something a fixed number of times. Perhaps we want to calculate gross salaries, or convert temperatures form centigrade to Fahrenheit for 20 different cities. Thewhile loop is ideally suited f or such cases.
Syntax of using while loop:
initialise loop counter
while(valid condition)
{
statement1;
statement2;
statement3;
statement4;
increment or decrement loop counter;
}
some basic rule of while loop:
- The statements within the while loop would keep in getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop.
- The condition being tested may use relational or logical operators as shown in the following examples:
while(x<=20)
while(x>=20 && x<=50)
while(x>=10 || (i>=10 && i<=20)||y==15) - Almost always, the while must test a condition that will eventually become false, otherwise the loop would be executed forever,indefinitely.
- It is not necessary that a loop counter must only be an int. It can even be a float.
- Example of while loop:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=5)
{
printf("%d\n",i);
i=i+1; //i++;
}
getch();
}
Output of above program:
1
2
3
4
5
Nested while loop
One or more loop which require execute again and again then these loop and loops place in separate block is known as nested loop.
Example of nested while loop:
/*demonstration of nested while loop*/
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,s;
clrscr();
r=1;
clrscr();
r=1;
while(r<=5) /*outer loop*/
{
c=1;
while(c<=2) /*inner loop*/
{
s=r+c;
printf("r=%d c=%d sum=%d\n",r,c,s);
c++;
}
printf("\n");
r++;
}
getch();
}
c=1;
while(c<=2) /*inner loop*/
{
s=r+c;
printf("r=%d c=%d sum=%d\n",r,c,s);
c++;
}
printf("\n");
r++;
}
getch();
}
Output of above program:
r=1 c=1 sum=2
r=1 c=2 sum=3
r=2 c=1 sum=3
r=2 c=2 sum=4
r=3 c=1 sum=4
r=3 c=2 sum=5
r=4 c=1 sum=5
r=4 c=2 sum=6
r=5 c=1 sum=6
r=5 c=2 sum=7
do-while loop
Now we know about for, while loop that executed the statement within them finite number of times. However, in real life programming, one comes across a situation when it is not known beforehand how many times the statement in the loop are to be executed. In this situation we used do-while loop.
do-while tests the condition after having executed the statement within the loop i.e. do-while would executed its statement at least once, even if the condition fails for the first time. For example notice in following program:
/*demonstration of do-while*/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
do
{
printf("Its work!!");
}while(10<1);
getch();
}
Output of above program:
Its work!!
In above program, the printf() would be executed once, since first the body of loop is executed and then the condition tested, 10<1 condition false so loop terminate and go to next statement.
/*program to find factorial value of any number, and it is execute unknown number of times when user enter no, then program should be terminate.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f=1;
char choice;
clrscr();
do
{
printf("Enter number : ");
scanf("%d",&n);
while(n>=1)
{
f=f*n;
n--;
}
printf("Factroial value of %d is %d",n,f);
printf("\nCalculate another value y/n :");
scanf("%c",&choice);
}while(choice=='y');
}
output of above program:
Enter number :5
Factorial value of 5 is 120
Calculate another value y/n :y
Enter number :4
Factorial value of 4 is24
Calculate another value y/n :n
In above program, the do-while loop would keep getting executed till the user continues to answer y. When user enter answer n,the loop terminate, since the condition fails.
Odd Loop
What is odd loop?
In real life programming, there are many times comes a situation when we don't know how many times the statements in the loop are to be executed.
There is comes concept of odd loop.
Execution of loop an unknown number of times can be done by - while, for and do...while loops.
/*A demonstration of odd loop using do...while*/
#include<stdio.h>
int main()
{
int n;
char answer;
do
{
printf("Enter any number : ");
scanf("%d", &n);
printf("Square of %d is %d",n,n*n);
fflush(stdin);
printf("\nWant to calculate more square y/n: ");
scanf("%c", &answer);
}while(answer=='y');
return 0;
}
The output of above program would be:
The above odd loop program we can write using for loop as:
/*odd loop using for loop of calculate square number C program*/
#include<stdio.h>
int main()
{
int n;
char ans='y';
for(; ans=='y' ; )
{
printf("Enter any number : ");
scanf("%d", &n);
printf("Square of %d is %d",n,n*n);
fflush(stdin);
printf("\nWant to calculate more square y/n: ");
scanf("%c", &ans);
}
return 0;
}
The output of above program would be:
The odd loop program we can write using while loop as:
/*odd loop using while loop of calculate square number C program*/
#include<stdio.h>
int main()
{
int n;
char ans='y';
while(ans=='y')
{
printf("Enter any number : ");
scanf("%d", &n);
printf("Square of %d is %d",n,n*n);
fflush(stdin);
printf("\nWant to calculate more square y/n: ");
scanf("%c", &ans);
}
return 0;
}
The output of above program would be:
In real life programming, there are many times comes a situation when we don't know how many times the statements in the loop are to be executed.
There is comes concept of odd loop.
Execution of loop an unknown number of times can be done by - while, for and do...while loops.
/*A demonstration of odd loop using do...while*/
#include<stdio.h>
int main()
{
int n;
char answer;
do
{
printf("Enter any number : ");
scanf("%d", &n);
printf("Square of %d is %d",n,n*n);
fflush(stdin);
printf("\nWant to calculate more square y/n: ");
scanf("%c", &answer);
}while(answer=='y');
return 0;
}
The output of above program would be:
![]() |
| Figure: Screen shot of odd loop (do...while) to calculate square of number C program |
The above odd loop program we can write using for loop as:
/*odd loop using for loop of calculate square number C program*/
#include<stdio.h>
int main()
{
int n;
char ans='y';
for(; ans=='y' ; )
{
printf("Enter any number : ");
scanf("%d", &n);
printf("Square of %d is %d",n,n*n);
fflush(stdin);
printf("\nWant to calculate more square y/n: ");
scanf("%c", &ans);
}
return 0;
}
The output of above program would be:
![]() |
| Figure: Screen shot of odd loop (for) to calculate square of number C program |
The odd loop program we can write using while loop as:
/*odd loop using while loop of calculate square number C program*/
#include<stdio.h>
int main()
{
int n;
char ans='y';
while(ans=='y')
{
printf("Enter any number : ");
scanf("%d", &n);
printf("Square of %d is %d",n,n*n);
fflush(stdin);
printf("\nWant to calculate more square y/n: ");
scanf("%c", &ans);
}
return 0;
}
The output of above program would be:
![]() |
| Figure: Screen shot of odd loop ( while ) to calculate square of number C program |



No comments:
Post a Comment