| HTML Tutorials |
|
|
| XML Tutorials |
|
|
| Browser Scripting |
|
|
| Server Scripting |
|
|
| .NET (dotnet) |
|
|
| Multimedia |
|
|
| Web Building |
|
|
| Java Tutorials |
|
|
| Programming Langauges |
|
|
| Soft Skills |
|
|
|
|
Looping
|
|
By using special loop keywords,you can loop (jumping for those assembly junkies) through your code.
These include following categories:
- for loop
- while loop
- do while.
|
|
For Loop
|
|
The for statement allows for the controlled loop.
The syntax for for loop is as follows:
|
for (start condition; continue condition; re-evaluation)
program statement;
|
|
|
The foollowing program shows the use of for loop which prints number from 0 to 10
|
* Sample program using a for statement */
#include <stdio.h>
int main(void)
{
int count;
for (count = 1; count <=10; count = count + 1)
printf("%d", count);
printf(\n);
return 0;
}
|
|
|
O/P:
|
|
|
|
While Loop
|
|
For repeating C statements whiles a condition is true,the while provides a the necessary mechanism.
The syntax for while loop is as follows:
|
while (condition)
program statement;
|
|
|
The foollowing program shows the use of while loop which prints number from 0 to 10
|
/* Sample program including the while loop */
#include <stdio.h>
int main(void)
{
int loop = 0;
while (loop <=10)
{
printf("%d", loop);
++loop
}
return 0;
}
|
|
|
The do while statement
|
|
The do {} while statement allows a loop to continue whilst a condition evaluates as TRUE (non-zero value).
The loop will exacute at least once
The syntax is as follows
|
do {
/* do stuff */
} while (statement);
|
|
|
The following program reverse a number using do while loop
|
/* Demonstration of the do..while loop */
#include <stdio.h>
int main(void)
{
int value, r_digit;
printf(Enter a number to be reversed.\n);
scanf(%d, &value);
do
{
r_digit = value % 10;
printf("%d", r_digit);
value = value / 10;
} while (value != 0);
printf(\n);
return 0;
}
|
|
|
Difference between while and do while loop
|
|
The do statement is similar to the while statement except that its termination condition is at the end of the body of the loop only.
Thus, you want to use a do statement,if you want to perform the body of the loop at least once, regardless of the condition.
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
Looping Statement in C,
while loop statement,
for loop statement,
c tutorial,
c syntax,
c array,
values statement,
c examples,
nested statement,
looping tutorial,
return c,
null statement,
c language,
sql statement,
looping array,
statement date,
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
|