Back again to tell you the story of my journey in a place so called “class”. What kind sorcery that they have implanted into my knowledge? Let’s find out Shall we.
At that session, we talked about a topic called “Repetition” with these sub topics:
1. Repetition Definition
2. For
3. While
4. Do-While
5. Repetition Operation
6. Break vs Continue
So you might be wandering what is the definition of “repetition”. …. Wait seriously? You don’t know? Wow. Might as well explain. Imagine your life, Done.
But in programming the definition of repetition is, 1 or more set of instructions that is being repeated. Quite self-explanatory indeed.
There is three types of repetition/looping in programming:
– For, usually written in this kind of form:
for(expr1; expr2; expr3){
statement1;
statement2;
…….
}
expr1 : Initiation
expr2 : Condition
expr3 : Increment/Decrement
– While
while(exp){
statement1;
statement2;
…..
}
– Do – While
do{
;
} while(exp);
There are certain ways to tell the loop to stop. One of them is break.
Example on using break:
int main() {
int x = 1;
while (x<=10) {
printf( “%d\n”, x );
x++;
break;
}
return 0;
}
That means the program will only finish one loop.
Another Operator quite helpful is called Continue
Example of Continue:
int main() {
int x;
for(x=1; x<=10; x++) {
if (x == 5) continue;
printf(“%d “, x);
}
return 0;
}
That means the program will skip the number five.
Well that’s the end of my lecture. Thanks for reading ^^