Perulangan (Loop)

17
Perulangan (Loop) Dr. Taufik Fuadi Abidin, M.Tech Irvanizam Zamanhuri, M.Sc Program Studi Teknik Informatika http://www.informatika.unsyiah.ac.id/irvanizam Email: [email protected] [email protected]

Transcript of Perulangan (Loop)

Perulangan (Loop)

Dr. Taufik Fuadi Abidin, M.TechIrvanizam Zamanhuri, M.Sc

Program Studi Teknik Informatikahttp://www.informatika.unsyiah.ac.id/irvanizam

Email: [email protected]@informatika.unsyiah.ac.id

do … while statement Bentuk Umum:

Statement do{}while mengizinkan sebuah perulangan (loop) untuk mengeksekusikan <statement> selama sebuah kondisi expr bernilai TRUE.

Perulangan (loop) akan mengeksekusi paling sedikit sekali.

do { <statement> } while (expr);

/* Demonstration of the do..while loop */#include<stdio.h>#include<stdlib.h>

int main() {

int value, rDigit; printf(Enter a number to be reversed.\n); scanf(“%d”, &value);

do { rDigit = value % 10; printf(“%d”, rDigit); value = value / 10; } while (value != 0); printf(\n); return 0;}

int main() { char Ch; do { printf(“\nUlangi (Y/T) ? “); do { Ch = toupper(getch()); } while(!((Ch==’Y’) || (Ch ==’T’))); printf(“\nAnda harus coba inputkan lagi”); } while(!(Ch == ‘T’)); return 0;}

while Statement Bentuk Umumnya adalah:

Pengulangan dilakukan jika condition bernilai TRUE

<statement> disebut juga badan dari while…loop

while (condition) { <statement>; }

#include<stdio.h>

int main(){ int x = 10, numberFound= 5; int i = 0, j = 1; // using while loop statement while(i < x){ i++; printf("%d\n",i); } // when number 5 found, escape loop body while(j < x){ if(j == numberFound){ printf("number found\n"); break; } printf("%d...keep finding\n",j); j++; } return 0;}

#include<stdio.h>

int main(){ int x = 10, numberFound= 5; int i = 0, j = 1; // using while loop statement while(i < x){ i++; printf("%d\n",i); } // when,escape loop body while(j < x){ if(j == numberFound){ printf("number found\n"); break; } printf("%d...keep finding\n",j); j++; } return 0;}

123456789101...keep finding2...keep finding3...keep finding4...keep findingnumber found

Contoh: (Lanjutan)

int i = 0;while (i++ <= 10) printf(“\n%d’,i);

int i = 0;while (++i <= 10) printf(“\n%d’,i);

int i = 10;while (i-- >= 1) printf(“\n%d’,i);

int i = 10;while (--i >= 1) printf(“\n%d’,i);

int main(void) { double xl= -1.5, xr=l.0, eps=0.00001; double xm, fxl, fxm, epsb, abseps=1, x; xm = 0. 5 * (xl + xr) ;

while(abseps >= eps) { abseps= abs(xm - xl); fx1 = 2*sqr(x1) +6*xl+3; fxm = 2*sqr(xm) +6*xm+3; epsb = fxl * fxm ; if (epsb < 0.0) { xr = xm; }else { xl = xm; } xm = 0.5 * (xl + xr) ;

x=xm; } printf(" Akar persamaan %lf\n", x); return 1;}

int main(void) { double xl= -1.5, xr=l.0, eps=0.00001; double xm, fxl, fxm, epsb, abseps=1, x; xm = 0. 5 * (xl + xr) ;

while(abseps >= eps) { abseps= abs(xm - xl); fx1 = 2*sqr(x1) +6*xl+3; fxm = 2*sqr(xm) +6*xm+3; epsb = fxl * fxm ; if (epsb < 0.0) { xr = xm; }else { xl = xm; } xm = 0.5 * (xl + xr) ;

x=xm; } printf(" Akar persamaan %lf\n", x); return 1;}

for Statement Bentuk Umumnya adalah:

<statement1> adalah inisial dari variable yang digunakan. <statement2> adalah perintah untuk penambahan atau pengurangan nilai

variable counter <condition> adalah suatu kondisi dalam bentuk ekspresi logika. Jika

<condition> bernilai TRUE, maka <stetements3> dieksekusikan, kalau FALSE, maka compiler C akan mengerjakan statements yang dibawah looping for

for (statement1; condition; statement2){ <statements3>}

Contoh#include<stdio.h>#include<stdlib.h>#define N 10int main() { int i, sum=0; float avg; for(i=0; i<=N; i++){ if (i%2==0){ printf(“\n%d”,i); } sum=sum+i; } avg = (float) sum/N; printf(“the average of %d numbers is %f”, N, avg); return 0;}

Bagaimana Outputnya??

Double-for Looping

int main() { int i,j; for(i = 1; i <= 10; i++) { for (j=i ; j <= 10; j++)

printf(“%d\t”,j); printf(“\n”); } return 0;}

int main() { int i,j; for(i = 1; i <= 10; i++) { for (j=i ; j <= 10; j++)

printf(“%d\t”,j); printf(“\n”); } return 0;}

Bagaimana Outputnya??

Double-for Looping (Lanjutan)

int main() { int i,j; for(i = 1; i <= 10; i++) { for (j=1 ; j <= i; j++)

printf(“%d\t”,j); printf(“\n”); } return 0;}

int main() { int i,j; for(i = 1; i <= 10; i++) { for (j=1 ; j <= i; j++)

printf(“%d\t”,j); printf(“\n”); } return 0;}

Bagaimana Outputnya?

??

References http://www.mycplus.com/tutorials/c-programming-

tutorials/loops/ http://www.tenouk.com/Module6a.html http://www.tenouk.com/clabworksheet/labworksheet7.html http://bytes.com/serversidescripting/c++/tutorials/c+

+conditionalstatements/index.html www.informatika.unsyiah.ac.id/tfa