for
Loop
For
loop is most popular and concise loop. It is also simplified version of loop.
It takes care of initialization of loop control, evaluating the loop condition
and proper increment or decrement.
for(initialization;condition;increment/decrement)
{
Loop body;
}
The
programs done in previous lesson (while loop) are easily converted to for loop.
E.g. take a previously done program of factorial.
/*
PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER */
#include<stdio.h>
#include<conio.h>
void
main()
{
int
n,c;
long
fact; //fact = 1 because this value use for
multiplication and update during program ans c is used to count from 1 to
number
clrscr();
printf(“\n
Enter number to find factorial: “);
scanf(“%d”,&n);
for(c=1;c<=n;c++) //initialization,
condition and increment section is combined. It is starting from
initialization and after checking condition increment section gets executed.
Later during the program initialization is not perform again where as only
increment section continue till the condition is true.
{
fact=fact*c; //main process
for calculating fact
c++; //for
proper execution the value of c should be increment
}
printf("\nFactorial of number =
%ld",fact);
getch();
}
|
o/p:
Enter
number to find factorial: 5
Factorial
of number = 120
|
In
for loop section break is done by semicolon (;) and only two semicolons are
allowed. If you have more than one initialization, condition and increment or
decrement then also you have to break section by semicolon and partition of
section is done by coma (,).
Have
a look at following program:
//prog
to print number in ascending and descending order by for loop
#include<stdio.h>
#include<conio.h>
void
main()
{
int a,b;
clrscr();
for(a=1,b=10;a<=10,b>=1;a++,b--) //for has more
than one variable. Separation done by coma and for section break semicolon is
used
{
printf(“\n a = %d \t b = %d”,a,b);
}
getch();
}
|
o/p:
a
= 1 b= 10
a
= 2 b= 9
a
= 3 b= 8
a
= 4 b= 7
a
= 5 b= 6
a
= 6 b= 5
a
= 7 b= 4
a
= 8 b= 3
a
= 9 b= 2
a
= 10 b= 1
|
This
is only introduction of for loop we will see detail programming of it in the
lesson of nested loop. For loop mostly use while creating set (array, matrix,
structure) programs.
do
while Loop
The
loop statements discussed previously, must evaluate condition at first and then
body of loop gets executed. But in some cases, it may be necessary to execute
the body of loop before the condition evaluated. In such situation you can use
do while loop.
do
while loop executes the body of loop at least once before evaluating condition.
do
{
Body of loop;
}
while(condition);
Let us check the following program that accept single
alphabet from user and continue the execution if user entered ‘y’ else
execution will over.
//program
to check working of do-while
#include<stdio.h>
#include<conio.h>
void
main()
{
char ans;
//variable for accepting single character
as input (single alphabet)
clrscr();
do
{
printf("Do you want to print
message?\n");
flushall(); //it is mostly
use while accepting single character from user to clean buffer(temporary
memory storage)
scanf("%c",&ans);
printf("Welcome\n");
}
while(ans=='y'); //post
condition checked
getch();
}
|
o/p:
Do
you want to print message?
y
Welcome
Do
you want to print message?
n
Welcome
|
If you execute the above program then you will find
that it first execute do part and
then continue process by evaluating while condition every time. The program
execution will terminate when user entered other than ‘y’ alphabet.
//program
to print Fibonaci's series up to n steps
#include<stdio.h>
#include<conio.h>
void
main()
{
int f0=0,f1=1,f2,c=1,steps;
clrscr();
printf("\nEnter steps for series:
");
scanf("%d",&steps);
printf("\nFibonaci's series up to %d
steps\n",steps);
printf("%d\t%d\t",f0,f1);
do
{
f2 = f0 + f1; //simple
formula for calculating Fibonaci’s series
printf("%d\t",f2);
f0=f1; //swap
the value of f1 to f0 and f2 to f1
f1=f2;
c++; //counts
the steps
}
while(c<=steps-2); //we have
performed two steps before beginning loop
getch();
}
|
o/p:
Enter
steps for series: 7
Fibonaci's
series up to 7 steps
0 1
1 2 3
5 8
|
Do
while loop useful in case of switch (menu driven programming) where we were
used goto in C Programming Language
Tutorial VI. Let us do it.
//program using
switch statement
#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2,choice;
clrscr();
printf("\nEnter two numbers: ");
scanf("%d%d",&no1,&no2);
// program
will repeat from this point
do
{
printf("\n=================================="); //menu list
printf("\n1 . Addition");
printf("\n2 . Subtraction");
printf("\n3 . Multiplication");
printf("\n4 . Division");
printf("\n5 . Exit");
printf("\n===================================");
printf("\nEnter your choice: "); //accepts choice from user
scanf("%d",&choice);
switch(choice) //switch starts
working by checking value of choice
{
case 1:
printf("\nAddition =
%d", no1+no2);
break;
case 2:
printf("\nSubtraction =
%d",no1-no2);
break;
case 3:
printf("\nMultiplication
= %d",(no1+no2)/2);
break;
case 4:
printf("\nDivision =
%d",no1/no2);
break;
case 5:
exit(0); //library
function for exiting program
default :
printf("\nWrong
Entry");
}
While(choice<=5); //continue
execution till you select exit case
getch();
}
|
In next some topics we will use do-while as per requirement. This loop is rarely use in programming.
No comments:
Post a Comment