1 / 19

STATEMENT whil e

STATEMENT whil e Statement while digunakan untuk memproses suatu perintah atau beberapa perintah dalam beberapa kali. Bentuk pernyataan:. while (kondisi) perintah;. while (kondisi) { perintah1; perintah2; ... perintahN; }.

yestin
Download Presentation

STATEMENT whil e

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. STATEMENT while • Statement while digunakan untuk memproses suatu perintah atau beberapa perintah dalam beberapa kali. • Bentuk pernyataan: while (kondisi) perintah; while (kondisi) { perintah1; perintah2; ... perintahN; }

  2. Perintah-perintah yang berada dalam blok while akan dieksekusi terus menerus selama kondisi benar. • Perlu diperhatikan bahwa pengujian terhadap kondisi dilakukan sebelum perintah dalam blok while dieksekusi.

  3. Contoh 1: #include <iostream.h> #include <iomanip.h> void main() { int i; i:=1; while (i<=10) { cout<<setw(3)<<i; i++; } } 1 2 3 4 5 6 7 8 9 10

  4. Contoh 2: #include <iostream.h> void main() { char lagi=‘’; while ((lagi==‘y’)||(lagi==‘Y’) { cout<<“Selamat”; cout<<“Ulangi lagi ? “;cin>>lagi; } } Selamat Ulangi lagi ?

  5. STATEMENT do...while • Statement do...while juga digunakan untuk melakukan perulangan terhadap suatu statement atau beberapa stament. • Bentuk pernyataan: do { perintah1; perintah2; ... perintahN; } while (kondisi);

  6. Bagian perintah1 sampai dengan perintahN akan dieksekusi secara terus menerus sampai kondisi yang mengikuti while salah. • Oleh karena pengujian kondisi dilakukan di akhir, maka perintah yang berada dalam blok do...while minimal akan dijalankan satu kali. Sedangkan pada statement while ada kemungkinan perintah dalam blok while tidak dieksekusi sama sekali.

  7. Contoh 1: #include <iostream.h> #include <iomanip.h> void main() { int i; i:=1; do { cout<<setw(3)<<i; i++; }while (i<10); } 1 2 3 4 5 6 7 8 9 10

  8. Contoh 2: #include <iostream.h> void main() { char lagi; do { cout<<“Selamat”; cout<<“Ulangi lagi ? “;cin>>lagi; } while ((lagi==‘y’)||(lagi==‘Y’); } Selamat Ulangi lagi ?

  9. STATEMENT for • Statement for digunakan untuk mengulang suatu perintah atau beberapa perintah yang berada dalam blok for. • Bentuk pernyataan: for (kondisi_awal;kondisi_akhir;pengatur) perintah; for (kondisi_awal;kondisi_akhir;pengatur) { perintah1; perintah2; ... perintahN; }

  10. Contoh 1: #include <iostream.h> #include <iomanip.h> void main() { int i; for (i=1;i<=10;i++) cout<<setw(3); } 1 2 3 4 5 6 7 8 9 10

  11. Contoh 2: #include <iostream.h> void main() { char huruf; for (huruf=‘A’;huruf<=‘W’;huruf++) cout<<huruf<<‘ ‘; } A B C D E F G H I J K L M N O P Q R S T U V W

  12. Contoh 3: #include <iostream.h> #include <iomanip.h> void main() { int angka; for (angka=1;angka<=20;angka+=2) cout<<setw(3)<<angka; } 1 3 5 7 9 11 13 15 17 19

  13. STATEMENT break • Pernyataan break telah diperkenalkan pada perintah switch. Selain digunakan pada perintah switch, break juga digunakan untuk memaksa keluar dari perulangan. • for( ; ; ) • { • if (...) • break; • ... • ... • }

  14. Contoh: • #include <iostream.h> • #include <iomanip.h> • void main() • { int i=1; • while(i<=10) • { if (bil>5) • break; • cout<<setw(2)<<i; • } • } 1 2 3 4 5

  15. STATEMENT continue • Kegunaan pernyataan continue merupakan kebalikan dari pernyataan break. Perintah continue digunakan untuk mengarahkan perulangan ke iterasi berikutnya. • Efek pada pernyataan for: • Ungkapan ke tiga pada (ungkapan terkanan) pernyataan for akan dijalankan dan kemudian ungkapan ke dua (ungkapan di tengah) akan diuji kembali.

  16. Efek pada pernyataan while atau do...while • Pengujian terhadap kondisi while akan dilakukan kembali. while(kondisi) { continue; } do { continue; while(kondisi);

  17. Contoh 1: • #include <iostream.h> • void main() • { int bil; • for(bil=0;bil<10;bil++) • { cout<<bil<<‘ ‘; • continue; • cout<<“Selamat”; • } • } 0 1 2 3 4 5 6 7 8 9

  18. #include <iostream.h> • void main() • { int i; • float nilai,total; • cout<<“Menghitung nilai rata-rata”; • i=0;total=0; • while(1) • { cout<<“Nilai ke-”<<i+1<<“ = “;cin>>nilai; • if (nilai>100) • { cout<<“Nilai diabaikan!”; • continue; • } • if (nilai<0) • break; • i++; • total+=nilai; • } • cout<<“Nilai rata-rata = ”<<total/i; • }

More Related