For loop is great for loops that you know how many times to loop. The others are good for if you don't know or don't need to know how many the program goes to the loop for example when a user want to continue or to quit when they are prompted. There is no need for a counter.

The syntax:
while (condion)
{
statement 1
statement 2
}

do
{
statement 1
statement 2
} while (condition)

Simple as that. Now here is an example for a while loop.

#include<stdio.h>
#include<conio.h>

char yorn;

void main()
{
clrscr();
printf("Do you want to go through the loop? (y or n):");
yorn=getch();
printf("%c", yorn);
while (yorn=='y')
{
printf("\nDo you still want to go through the loop? (y or n):");
yorn=getch();
}
getch();
}
And for a do while loop
#include<stdio.h>
#include<conio.h>

char yorn;

void main()
{
clrscr();
do {
printf("\nDo you want to go through the loop? (y or n):");
yorn=getch();
printf("%c", yorn);
} while (yorn=='y');
getch();
}

It's not that hard to figure out. Next is we move on to Arrays.

<<back<< >>Next>>