while, do while and for loops in C
By: Ram
While
Statement:
The while
-loop
has a condition and the statements in the curly braces are executed while the
condition has the value "true".
while (condition)
{
The conditional expression is
evaluated every time the loop is executed, if the value of the expression is
true, then it will carry on with the instructions in the curly braces. If the
expression evaluates to false
(or 0) then the instructions in the braces are ignored and the entire while
loop ends. The computer then moves onto the next statement in the program.
Moreover, if the condition has
the value false before the loop has been executed even once, the statements
inside the braces will not get executed at all.
Example:
/*Program to reverse a digit*/
main()
{
int
n,s,d;
printf(“enter
a numberâ€);
scanf(“%dâ€,
&n);
s=0;
while(n!=0)
{
d=n%10;
s=s*10+d;
n=n/10;
}
printf(“reverse
= %d†, s);
}
Do-While statement:
In
a do..while loop the condition is at the end of this loop. This means that a
condition will always be executed at least once, before the test is made to
determine whether it should continue. This is the only difference between while
and do..while
.
do
{
while (condition)
Example:
/*To print all divisors of a given number*/
main()
printf(“%4d\nâ€,a);
For Statement:
The ‘for’ loop statement is used to perform repeated processing. For all
values of variable from value1 to value2 in
steps of value3
,
repeat the following sequence of commands....
Syntax:
for
(initialisation ; condition ; increment)
statement;
or
{
statements;
}
Example:
/* to find factorial of a number
as in 1!+2!+3!........n! */
main()
{
int s,n,f,a;
printf(“enter a numberâ€);
scanf(“%dâ€,&n);
s=0;
f=1;
for(a=1;a<=n;a++)
{
f=f*a;
s=s+a;
}
printf(“1!+2!+3!........%d=%dâ€,n,s);
}
Nested
Loops:
A nested loop is a loop within another
loop. It is mostly used in case of for loops.
A for
loop controls
the number of times a particular set of statements will be carried out. Another
outer loop could be used to control the number of times that a whole loop is
carried out.
We shall try to print the following using the nested for loops.
1
1 2
1 2 3
1 2 3 4
……….
………..
………
10 times
main()
{
int i,a;
for(a=1;a<=10;a++)
{
for(i=1;i<=a;i++);
printf(“%3dâ€,i);
printf(“\nâ€):
}
}
Continue
and Break Statements:
When a continue
statement is encountered, a loop will stop whatever it is doing and will go
straight to the start of the next loop pass. It to avoid executing a lot of
irrelevant statements.
Example:
for (i = -10; i <= 10; i++)
{
if (i == 0)
{
continue;
}
printf ("%d", 20/i);
}
Example:
main()
{
x=5;
do
{
printf(“%dâ€,x);
if (x>7)
break;
x=x+1;
}
while(x<=20)
}
Archived Comments
1. sir please check the program in for loop
View Tutorial By: vc ramreddy at 2009-09-28 23:35:10
2. how to create a do while,the output wil show the multiplacation table???
View Tutorial By: waa at 2009-09-02 19:23:45
3. the best!! it helps me a lot.....
View Tutorial By: shin at 2009-02-15 18:12:31
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C