Skip to main content

Rangkuman Kelas Besar 10 Oct 2018

Repetition

Repetition Definition

Repetition is the act of repeating, or doing, saying, or writing something again; repeated action, performance, production or presentation. In this case, repetition is when one or more instruction repeated for certain amount of time. The number of repetition can be predefined (hard-coded in program) or defined later at run time. There are 3 repetition/looping operations, which are for, while, do-while.


Repetition: FOR

Syntax:
for(exp1; exp2; exp3) statement;
or:
for(exp1; exp2; exp3){
  statement1;
  statement2;
  …….
 }
exp1 :  initialization
exp2 :  conditional
exp3 :  increment or decrement
exp1, exp2 and exp3 are optional


exp1 and exp3 can consist of several expression separated with comma
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;
    }
}

·Flow Chart of FOR Statement
Example:
Program to print out numbers from 1 to 10
#include<stdio.h>
int main()
{
    int x;
    for( x = 1 ;  x <= 10 ;  x++ ) printf( "%d\n", x );
    return(0);
}
Program to print out numbers from 10 to 1
#include<stdio.h>
int main()
{
    int x;
    for( x = 10 ;  x >= 1 ;  x-- ) printf( "%d\n", x);
    return(0);
}



  • Infinite loop = Loop with no stop condition can use "for-loop" by removing all parameters (exp1, exp2, exp3). To end the loop use break.
  • Nested Loop = Loop in a loop. The repetition operation will start from the inner side loop.


Repetition: WHILE

Syntax :
while (exp) statements;
or:
while(exp){
  statement1;
  statement2;
   …..
}

Example :
int counter = 1;
while ( counter <= 10 ) {
     printf( "%d\n", counter );
     ++counter;

}

·Flow Chart of WHILE Statement

while (exp) statements;
exp is Boolean expression. It will result in true (not zero) or false (equal to zero).
•Statement will be executed while the exp is not equal to zero.
exp evaluation is done before the statements executed.



Repetition: DO-WHILE

Syntax :
do{
    < statements >;
} while(exp);

•Keep executing while exp is true
exp evaluation done after executing the statement(s)

Example :
int counter=0;
do {
     printf( "%d  ", counter );
  ++counter;
} while (counter <= 10);

·Flow Chart of DO-WHILE Statement




Repetition Operation

•In while operation, statement block of statements may never be executed at all if exp value is false
•In do-while on the other hand statement block of statements will be executed min once
•To end the repetition, can be done through several ways:
Sentinel
Question, should the repetition continue?
Example: (Question)
#include <stdio.h>
int main()
{
         int width, height, area; char repeat;
         printf(”Continue ? (Y/N) :”);
         scanf(”%c”,&repeat);
         while((toupper(repeat)) == ’Y’) {
  printf(” Width : ”);
  scanf(”%d”,&width);
  printf(” Height : ”);
  scanf(”%d”,&height);
  area = width*height;
  printf(” Area = %d\n\n”,area);
  printf(”Continue ?(Y/N):”);
  scanf(”%c”,&repeat);
         }
         return(0);
}
Example: (Sentinel)
 As sentinel, used 0 for width or height
#include <stdio.h>
int main()
{
        int width, height, area;
        do{
  printf(” Width : ”);
  scanf(”%d”,&width);
  printf(” Height : ”);
  scanf(”%d”,&height);
  area = width*height;
  printf(” Area = %d\n\n”,area);
        } while((width != 0) && (height != 0));
        return(0);
}

Break vs Continue

break:
–ending loop (for, while and do-while)
–end the switch operation
continue:
  skip all the rest of statements (subsequent to the skip statement) inside a repetition, and continue normally to the next loop


Example using continue:
#include <stdio.h>
int main() {
        int x;
        for(x=1; x<=10; x++) {
      if (x == 5) continue;
       printf("%d ", x);
        }
        return 0;
}

Output : 1 2 3 4 6 7 8 9 10

•Example using break:
#include <stdio.h>
int main() {
        int x;
        for(x=1; x<=10; x++) {
      if (x == 5) break;
       printf("%d ", x);
        }
        return 0;
}

Output : 1 2 3 4


Summary

·Repetition is a condition which is one or more instruction repeated for certain amount of time
·3 types of repetition/looping in C:
for
while
do-while



2201732645
Binus.ac.id
Skyconnectiva.com
Devin Christoforus Suherman


Reference


Comments

Post a Comment