Bersarang while loop dalam bahasa pemrograman PythonIsi Show Bahasa pemrograman Python memungkinkan penggunaan loop while di dalam while loop lainnya, ini dikenal sebagai while loop bersarang dalam bahasa pemrograman Python< Bersarang while loop dengan PythonPernyataanSintaks loop bersarang-sementara di Python sebagai berikut Sintaksiswhile expression: while expression: statement(s) statement(s)
Bagan alir perulangan while bersarangDiagram alir bersarang saat di Python
Cara kerjanya nested while loopDalam perulangan while bersarang di Python, tersedia dua jenis pernyataan while
Awalnya, Outer loop test expression dievaluasi hanya sekali. Saat mengembalikan true , aliran kontrol melompat ke loop sementara. the inner while loop executes to completion. However, when the ekspresi pengujian adalah false , aliran kontrol datang . Alur kontrol ini berlanjut hingga while loop and executes again from the outer while loop only once. This flow of control persists until ekspresi pengujian loop luar false . Setelah itu, jika test ekspresi dari loop luar adalah false, the flow of control skips the execution and goes to rest. ContohProgram 1 i=1 while i<=3 : print(i,"Outer loop is executed only once") j=1 while j<=3: print(j,"Inner loop is executed until to completion") j+=1 i+=1;_ Ketika kode di atas dijalankan, menghasilkan hasil sebagai berikut 1, 'Outer loop is executed only once') (1, 'Inner loop is executed until to completion') (2, 'Inner loop is executed until to completion') (3, 'Inner loop is executed until to completion') (2, 'Outer loop is executed only once') (1, 'Inner loop is executed until to completion') (2, 'Inner loop is executed until to completion') (3, 'Inner loop is executed until to completion') (3, 'Outer loop is executed only once') (1, 'Inner loop is executed until to completion') (2, 'Inner loop is executed until to completion') (3, 'Inner loop is executed until to completion')
Perulangan while tak terbatas dengan Pythonwhile(True): print("Statements of outer loop is executed only once"); //#then flow of control enter the inner loop while(True): print("Statements of inner loop is executed until to complition"); //#the loop executed until the execution is false //#this is a infinite loop Ketika kode di atas dijalankan, menghasilkan hasil sebagai berikut Statements of outer loop is executed only once Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition Statements of inner loop is executed until to complition ..................... ....................._
Lebih Banyak Contoh untuk perulangan while bersarang di PythonProgram 1 Menampilkan tabel perkalian menggunakan nested while dalam bahasa Python //#this is a program to display multiplication table //#example for nested-while loop in Python i=1 while i<=10: j=1 while j<=12: print i*j, j+=1 i+=1 print "\n"
Ketika kode di atas dijalankan, menghasilkan hasil sebagai berikut 1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 4 8 12 16 20 24 28 32 36 40 44 48 5 10 15 20 25 30 35 40 45 50 55 60 6 12 18 24 30 36 42 48 54 60 66 72 7 14 21 28 35 42 49 56 63 70 77 84 8 16 24 32 40 48 56 64 72 80 88 96 9 18 27 36 45 54 63 72 81 90 99 108 10 20 30 40 50 60 70 80 90 100 110 120_
Program 2 Menampilkan tabel perkalian menggunakan nested while dalam bahasa Python #this is a program to display multiplication table #example for nested while loop in Python i=1 while i<=5: j=1 while j<=5: print i,"*",j,'=',i*j j+=1 i+=1 print "\n"
Ketika kode di atas dijalankan, menghasilkan hasil sebagai berikut 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25_
Disarankan untuk AndaWhile loop di Java While loop di C++ While loop di C While loop dengan Python
Pos terkaitLoop dalam bahasa pemrograman C Bersarang while loop dalam bahasa pemrograman Cpp Bersarang do while loop dalam bahasa C Bersarang while loop dalam bahasa pemrograman Java Bersarang while loop dalam bahasa pemrograman Cpp Bersarang do while loop dalam bahasa C TerkaitBisakah saya menjalankan for loop di dalam while loop?Catatan terakhir tentang pengulangan bersarang adalah bahwa Anda dapat meletakkan semua jenis lingkaran di dalam jenis lingkaran lainnya. Misalnya, loop 'for' bisa berada di dalam loop 'while' atau sebaliknya.
Bagaimana Anda menggunakan for loop di dalam loop?Jika sebuah loop ada di dalam tubuh loop lain, itu disebut loop bersarang. Berikut adalah contoh perulangan for bersarang. // loop luar for (int i = 1; i Di sini, kita menggunakan perulangan for di dalam perulangan for lainnya. |