Program Control : Repetition (10 October 2018)
Definition of Repetition
=> 1 or more instruction repeated for certain amount of time.
=> Number of repetition can be predefined or defined later when running the program.
Repetition or Looping operation consist of :
1. For
2. While
3. Do - While
For = Syntax : for(exp1; exp2; exp3) statement;
or
for(exp1; exp2; exp3){
statement1;
statement2;
}
exp1 : initialization
exp2 : conditional
exp3 : increment/decrement
statement : what the program should do if condition for "For" true
Example :
void reverse(char ss[]){
int c, i, j;
for(i=0, j=strlen(ss)-1; i<j; i++; j--){
c=ss[i];
ss[i]=ss[j];
ss[j]=c;
}
}
//Reverse used to reverse words from a string. Example :Hello => Olleh
> Infinite Loop
A loop without any stop condition by removing all parameters.
To stop the loop, click the box and then Ctrl+C to break the loop.
> Nested Loop
A loop inside a loop. The loop will start from the inner side loop.
=> 1 or more instruction repeated for certain amount of time.
=> Number of repetition can be predefined or defined later when running the program.
Repetition or Looping operation consist of :
1. For
2. While
3. Do - While
For = Syntax : for(exp1; exp2; exp3) statement;
or
for(exp1; exp2; exp3){
statement1;
statement2;
}
exp1 : initialization
exp2 : conditional
exp3 : increment/decrement
statement : what the program should do if condition for "For" true
Example :
void reverse(char ss[]){
int c, i, j;
for(i=0, j=strlen(ss)-1; i<j; i++; j--){
c=ss[i];
ss[i]=ss[j];
ss[j]=c;
}
}
//Reverse used to reverse words from a string. Example :Hello => Olleh
> Infinite Loop
A loop without any stop condition by removing all parameters.
To stop the loop, click the box and then Ctrl+C to break the loop.
> Nested Loop
A loop inside a loop. The loop will start from the inner side loop.
Komentar
Posting Komentar