A looping statement is used to allow us to execute a statement or group statements multiply times.
This is the looping diagram !
Looping is divided on :
1. While Loop
Repeats a statement or group of statements while a given condition is true.
Example :
#include <stdio.h>
int main () {
int n = 1000;
while ( n > 0 ;) {
printf (" %d ",n);
n = n - 7;
}
printf (" \n ");
}
2. For Loop
Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
Example :
#include <stdio.h>
int main () {
int n = 1;
for (; n <= 10 ;) {
printf (" %d ",n);
++n;
}
printf (" \n ");
}
3. Do ...... While Loop
Like a while statement, except that it tests the condition at the end of the loop body.
Example :
#include <stdio.h>
int main () {
int n = 1;
do {
printf (" %d ",n);
++n;
}
while ( n < 10 );
printf (" %d ",n);
}
0 komentar:
Posting Komentar