By: Ram in C Tutorials on 2008年09月16日 [フレーム]
The while-loop has a condition and the statements in the curly braces are executed while the condition has the value "true".
while (condition)
{
statements;
}
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);
}
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
{
statements;
}
while (condition)
Example:
/*To print all divisors of a given number*/
main()
{
int a,r,n
printf("enter a number");
scanf("%d,&n);
a=2;
do
{
r= n % a;
if(r = = 0)
printf("%4d\n",a);
a=a+1;
}
while(a <= n/2)
}
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);
}
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.
Example:
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"): } }
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);
}
The break statement is used to quit any of the three loops above at any stage, whether it has finished or not.
Example:
main()
{
x=5;
do
{
printf("%d",x);
if (x>7)
break;
x=x+1;
}
while(x<=20)
} This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in C )
Passing double value to a function in C
Sum of the elements of an array in C
Simple arithmetic calculations in C
Printing a simple histogram in C
Passing pointer to a function in C
Find square and square root for a given number in C
Using memset(), memcpy(), and memmove() in C
Latest Articles (in C)
Simple arithmetic calculations in C
Find square and square root for a given number in C
Printing a simple histogram in C
Sum of the elements of an array in C
Passing pointer to a function in C
Passing double value to a function in C
Infix to Prefix And Postfix in C
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
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate