Program Control: Repetition

Sebelumnya apa itu Repetition? Dalam bahasa indonesia adalah pengulangan. 

Repetition adalah satu atau lebih intruksi yang diulang yang sudah ditentukan. Jumlah repetition bisa ditentukan sebelumnya ataupun setelah program dijalankan.


Repetition / Looping Operations, ada :

- For

- While

- Do-While



Repetition : FOR

Syntax ex :

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;
    }
}
• Infinite Loop
Loop dengan tidak adanya kondisi yang memberhentikannya, bisa menggunakan "for-loop" dengan menghilangkan semua parameters  (exp1, exp2, exp3). Untuk menghentikannya bisa menggunakan break.

• Nested Loop

Loop di dalam loop. Repetition akan dimulai dari loop yang paling dalam pertama.

Repetition : WHILE
Syntax Ex :
while (exp) statements;
or:
while(exp){
  statement1;
  statement2;
   …..
}
Example :
int counter = 1;
while ( counter <= 10 ) {
     printf( "%d\n", counter );
     ++counter;
}

while (exp) statements;
• exp adalah Boolean. Hasilnya adalah benar (1) atau salah (0).
• Statement akan di execute, while the exp tidak sama dengan zero.
• exp evaluation sudah selesai sebelum statements di execute.

Repetition : DO-WHILE
Syntax Ex. :
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);


• Di while operation, statements di dalam block tidak akan dijalankan ketika value dari exp itu false (0). Namun statements di dalam block akan dijalankan setidaknya 1 kali.

• Untuk mengakhiri repetisi, ada berbagai cara :

– Sentinel

– Question, should the repetition continue?



Example Using 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 for Sentinel :

Used 0 for width and 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);

}



*References :

Session 9 SlideShow

Paul Deitel & Harvey Deitel. (2016). C how to program : with an introduction to C++. 08. Pearson Education. Hoboken. ISBN: 9780133976892. Chapter 3 & 4 

Doing the Same Thing Over and Over: http://aelinik.free.fr/c/ch07.htm 




Nama : Reza Arief Setianto
NIM : 1801427040
Teknik Industri / Algorithm and Programming
binus.ac.id skyconnectiva.com

Komentar

Postingan populer dari blog ini

Sorting and Searching

Algoritma - Week 1 Pengenalan Bahasa C (Ringkasan)