#include<stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
printf("\n================== Ascending Order========================\n");
i=1;
do
{
printf("\n %d",i);
//i++;
i=i+2;
}while(i<=10);
printf("\n================== Descending Order========================\n");
i=10;
do
{
printf("\n %d",i);
i--;
}while(i>=1);
getch();
}
-------------------------------------------------------------------
Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. (They may be executing a small number of tasks, but in principle, to produce a list of messages only requires repeating the operation of reading in some data and displaying it.) Now, think about what this means: a loop lets you write a very simple statement to produce a significantly greater result simply by repetition.
A loop statement allows you to execute a block of code repeatedly. The C while loop is used when you want to execute a block of code repeatedly with checked condition before making an iteration. The following flow chart illustrates the while loop in C:
The syntax of while loop in C is as follows:
1 while (expression) {
2 // statements
3 }
This loop executes as long as the given logical expression between parentheses afterwhile is true. When expression is false, execution continues with the statement following the loop block. The expression is checked at the beginning of the loop, so if it is initially false, the loop statement block will not be executed at all. And it is necessary to update loop conditions in loop body to avoid loop forever. If you want to escape loop body when a certain condition meet, you can use break statement
vies source:--http://cprogramminglanguage.net/c-while-loop-statement.aspx
No comments:
Post a Comment