Repitition means one or more instruction repeated for
certain amount of time.
There are 3 kinds of it :
- for
- while
- do-while
#include<stdio.h>
int
main(){
int x;
for( x = 1
; x <= 10
; x++ )
printf( "%d\n", x );
return 0;
}
And here it is the example of using 'for' to print out number 10 to 1
#include<stdio.h>
{
int x;
for( x = 10 ; x >= 10 ; x-- ) printf("%d\n", x );
return 0;
}
Infinite loop => looping that done until countless times.
Repitition : WHILE
Example :
#include<stdio.h>
int main(){
int counter = 1;
Repitition : WHILE
Example :
#include<stdio.h>
int main(){
int counter = 1;
while ( counter <= 10 ) {
printf( "%d\n", counter
);
++counter;
return 0;
}
Repitition : DO-WHILE
Example :
#include<stdio.h>
int main(){
}
Break vs Continue
Break => is to force finish the loop
Example :
#include<stdio.h>
int main(){
int x = 1;
while (x<=10) {
printf("%d\n", x);
x++;
break;
}
return 0;
}
Continue => skip all the rest of statement inside a repetition, and continue normally to the next loop.
Example :
Example :
#include<stdio.h>
int main(){
int counter=0;
do {
printf( "%d", counter );
++counter;
} while (counter <= 10);
return 0;}
Break vs Continue
Break => is to force finish the loop
Example :
#include<stdio.h>
int main(){
int x = 1;
while (x<=10) {
printf("%d\n", x);
x++;
break;
}
return 0;
}
Continue => skip all the rest of statement inside a repetition, and continue normally to the next loop.
Example :
#include
<stdio.h>
int
main() {
int x;
for(x=1; x<=10; x++) {
if (x == 5) continue;
printf("%d ", x);
}
return 0;
}
-
Comments
Post a Comment