Looping in C
in loop, statement will be executed sequentially. there are some syntax in loop. they are :
- for loop
is a repetation control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
example :
#include <stdio.h>
int main ()
{
int a;
for (a=0; a<=10; a++) {
printf ("%d ", a);
}
return 0;
}
the result will be "0 1 2 3 4 5 6 7 8 9 10" - While loop
while loop statement in c repeatdly executes a target statement as long as a given condition is teur.
example :
#include <stdio.h>
int main ()
{
int a = 20;
while (a<30)
{
printf ("%d ",a); a++;
}
return 0;
}
the output will be "20 21 22 23 24 25 26 27 28 29". - Do while loop
do while will check its condition at the bottom of the loop. but for and while will check the loop condition at the top of the loop.
example :
#include <stdio.h>
int main ()
{
int a = 0;
do
{
printf ("%d ",a); a++;
}
while (a<10);
return 0;
}
the output will be "0 1 2 3 4 5 6 7 8 9".
Tidak ada komentar:
Posting Komentar