while, do while and for loops in C
By: Ram in C Tutorials on 2008-09-16
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) { 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); }
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 { 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) }
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.
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"): } }
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); }
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) }
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
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:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
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 ) |
Latest Articles (in C) |
- Data Science
- Android
- React Native
- 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
Comments