Menguasai Nested Loops di C#: For, While, dan Do-While Dijelaskan
Loop di dalam loop disebut nested loop. Inilah tampilan nested loop:
Outer-Loop { // body of outer-loop Inner-Loop { // body of inner-loop } ... ... ... } Seperti yang Anda lihat, outer loop melingkupi inner loop. Inner loop adalah bagian dari outer loop dan harus dimulai dan selesai dalam tubuh outer loop.
Pada setiap iterasi outer loop, inner loop dieksekusi sepenuhnya.
Nested for loop
For loop di dalam for loop lain disebut nested for loop.
Contoh:
for (int i=0; i<5; i++) { // body of outer for loop for (int j=0; j<5; j++) { // body of inner for loop } // body of outer for loop } Contoh 1: Nested for Loop
using System; namespace Loop { class NestedForLoop { public static void Main(string[] args) { int outerLoop = 0, innerLoop = 0; for (int i=1; i<=5; i++) { outerLoop ++; for (int j=1; j<=5; j++) { innerLoop++; } } Console.WriteLine("Outer Loop runs {0} times", outerLoop); Console.WriteLine("Inner Loop runs {0} times", innerLoop); } } }
Ketika kita menjalankan program, outputnya akan menjadi:
Outer Loop runs 5 times Inner Loop runs 25 times
Dalam program ini, outer loop berjalan 5 kali. Setiap kali outer loop berjalan, inner loop berjalan 5 kali sehingga berjalan 25 kali secara keseluruhan.
Contoh 2: Nested for Loop untuk Mencetak Pola
using System; namespace Loop { class NestedForLoop { public static void Main(string[] args) { for (int i=1; i<=5; i++) { for (int j=1; j<=i; j++) { Console.Write(j + " "); } Console.WriteLine(); } } } }
Ketika kita menjalankan program, outputnya akan menjadi:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Nested while loop
While loop di dalam while loop lain disebut nested while loop.
Contoh:
while (condition-1) { // body of outer while loop while (condition-2) { // body of inner while loop } // body of outer while loop } Contoh 3: Nested while Loop
using System; namespace Loop { class NestedWhileLoop { public static void Main(string[] args) { int i=0; while (i<2) { int j=0; while (j<2) { Console.Write("({0},{1}) ", i,j); j++; } i++; Console.WriteLine(); } } } }
Ketika kita menjalankan program, outputnya akan menjadi:
(0,0) (0,1) (1,0) (1,1)
Nested do-while loop
Do-while loop di dalam do-while loop lain disebut nested do-while loop.
Contoh:
do { // body of outer while loop do { // body of inner while loop } while (condition-2); // body of outer while loop } while (condition-1); Contoh 4: Nested do-while Loop
using System; namespace Loop { class NestedWhileLoop { public static void Main(string[] args) { int i=0; do { int j=0; do { Console.Write("({0},{1}) ", i,j); j++; } while (j<2); i++; Console.WriteLine(); } while (i<2); } } }
Ketika kita menjalankan program, outputnya akan menjadi:
(0,0) (0,1) (1,0) (1,1)
Nested loop dengan jenis inner dan outer yang berbeda
Tidak wajib untuk menest loop dengan jenis yang sama. Kita dapat menempatkan for loop di dalam while loop atau do-while loop di dalam for loop.
Contoh 5: C# Nested Loop: Inner dan outer loop berbeda
using System; namespace Loop { class NestedLoop { public static void Main(string[] args) { int i=1; while (i<=5) { for (int j=1; j<=i; j++) { Console.Write(i + " "); } Console.WriteLine(); i++; } } } }
Ketika kita menjalankan program, outputnya akan menjadi:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Dalam program di atas, for loop ditempatkan di dalam while loop. Kita dapat menggunakan berbagai jenis loop di dalam loop.