Hai vòng lặp while C++

C hỗ trợ lồng các vòng lặp trong C. Lồng các vòng lặp là tính năng trong C cho phép lặp các câu lệnh bên trong một vòng lặp khác. Hãy quan sát một ví dụ về vòng lặp lồng nhau trong C

Bất kỳ số lượng vòng lặp nào cũng có thể được xác định bên trong một vòng lặp khác, tôi. e. , không có giới hạn nào đối với việc xác định bất kỳ số lượng vòng lặp nào. Mức độ lồng nhau có thể được xác định tại n lần. Bạn có thể xác định bất kỳ loại vòng lặp nào bên trong một vòng lặp khác;

Cú pháp của Vòng lặp lồng nhau

Outer_loop và Inner_loop là các vòng lặp hợp lệ có thể là vòng lặp 'for', vòng lặp 'while' hoặc 'do-while'

Một vòng lặp lồng nhau có nghĩa là một câu lệnh lặp bên trong một câu lệnh vòng lặp khác. Đó là lý do tại sao các vòng lặp lồng nhau còn được gọi là “vòng lặp bên trong vòng lặp“. Chúng ta có thể xác định bất kỳ số vòng lặp nào bên trong một vòng lặp khác

1. Lồng nhau cho vòng lặp

Vòng lặp for lồng nhau đề cập đến bất kỳ loại vòng lặp nào được xác định bên trong vòng lặp 'for'. Dưới đây là sơ đồ luồng tương đương cho các vòng lặp 'for' lồng nhau

Vòng lặp lồng nhau trong C

cú pháp

for [ initialization; condition; increment ] {

   for [ initialization; condition; increment ] {
      
      // statement of inside loop
   }

   // statement of outer loop
}

Thí dụ. Chương trình bên dưới sử dụng vòng lặp for lồng nhau để in ma trận 3D 2x3x2

C




// C program to print elements of Three-Dimensional Array

// with the help of nested for loop

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
3

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
4

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______6

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______2
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
9

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______11_______1

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2_______11_______3

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______11_______6

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______11_______8
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
Pattern Printing using Nested While loop
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______11_______8
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
Pattern Printing using Nested While loop
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
6

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2_______11_______8
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
do{

   do{
      
      // statement of inside loop
   }while[condition];

   // statement of outer loop
}while[condition];
1

do{

   do{
      
      // statement of inside loop
   }while[condition];

   // statement of outer loop
}while[condition];
2_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
do{

   do{
      
      // statement of inside loop
   }while[condition];

   // statement of outer loop
}while[condition];
5
do{

   do{
      
      // statement of inside loop
   }while[condition];

   // statement of outer loop
}while[condition];
6

do{

   do{
      
      // statement of inside loop
   }while[condition];

   // statement of outer loop
}while[condition];
7_______32_______8

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2______41_______0

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______41_______0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______6
do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
7

do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
0

Đầu ra

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11

2. Vòng lặp lồng nhau

Vòng lặp while lồng nhau đề cập đến bất kỳ loại vòng lặp nào được xác định bên trong vòng lặp 'while'. Dưới đây là sơ đồ luồng tương đương cho các vòng lặp 'while' lồng nhau

Vòng lặp lồng nhau trong C

cú pháp

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}

Thí dụ. In mẫu bằng cách sử dụng các vòng lặp lồng nhau

C




do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
9

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
3

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
4

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______2
3 3 5 7
7

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
* * * 
* * * 
* * * 

* * * 
2
* * * 
* * * 
* * * 

* * * 
3

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______2
* * * 
* * * 
* * * 

* * * 
7

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______271_______0 // C program to print elements of Three-Dimensional Array1

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9// C program to print elements of Three-Dimensional Array5
* * * 
* * * 
* * * 

* * * 
3

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0____1_______2 // C program to print elements of Three-Dimensional Array9

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______271_______0 // with the help of nested for loop2

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9// with the help of nested for loop6// with the help of nested for loop7

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2______272_______9

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______41_______0

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0____1_______03

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______6
do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
7

do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
0

Đầu ra

Pattern Printing using Nested While loop
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

3. Vòng lặp làm trong khi lồng nhau

Vòng lặp do-while lồng nhau đề cập đến bất kỳ loại vòng lặp nào được xác định bên trong vòng lặp do-while. Dưới đây là sơ đồ luồng tương đương cho các vòng lặp 'do-while' lồng nhau

vòng lặp do while trong C

cú pháp

do{

   do{
      
      // statement of inside loop
   }while[condition];

   // statement of outer loop
}while[condition];

Ghi chú. Không có quy tắc nào mà một vòng lặp phải được lồng vào bên trong kiểu của chính nó. Trên thực tế, có thể có bất kỳ loại vòng lặp nào được lồng vào bất kỳ loại nào và ở bất kỳ cấp độ nào.

cú pháp

do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];

Thí dụ. Chương trình dưới đây sử dụng vòng lặp for lồng nhau để in tất cả các thừa số nguyên tố của một số.  

C




Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
10

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
11

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
13

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
16

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
17
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
18
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
20

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
4

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______23

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______271_______0
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
26

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9// with the help of nested for loop6
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
31

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0____1_______33

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______38

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______40

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______11_______8
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
45
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
46
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
47

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______1_______49

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______271_______0
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
52

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9// with the help of nested for loop6
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
57

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2______1_______59

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______41_______0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______66

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
68

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______70
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
71

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9// with the help of nested for loop6
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
76

do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
79

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
3

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
4

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______2
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
85

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
87

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______6
do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
7

do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
0

đầu ra.

3 3 5 7

Phá vỡ các vòng lặp lồng nhau

Bất cứ khi nào chúng ta sử dụng câu lệnh ngắt bên trong các vòng lặp lồng nhau, nó sẽ ngắt vòng lặp trong cùng và điều khiển chương trình nằm bên trong vòng lặp bên ngoài.  

Thí dụ.  

C




Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
92

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
93

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
3

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
4

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______2
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
02

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______11_______8
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
07

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______11_______8
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
12

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2_______11_______14

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2_______1_______70
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
17

do{

   do{
      
      // statement of inside loop
   }while[condition];

   // statement of outer loop
}while[condition];
2_______11_______19
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
20

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2______41_______0

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
26
* * * 
* * * 
* * * 

* * * 
3

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______41_______0

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9// C program to print elements of Three-Dimensional Array5
* * * 
* * * 
* * * 

* * * 
3

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______6
do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
7

do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
0

Đầu ra

* * * 
* * * 
* * * 

* * * 

Trong chương trình trên, vòng lặp đầu tiên sẽ lặp từ 0 đến 5 nhưng ở đây nếu tôi bằng 3 thì nó sẽ bị ngắt và sẽ không in * như trong kết quả.  

Tiếp tục Bên trong các vòng lặp lồng nhau

Bất cứ khi nào chúng ta sử dụng câu lệnh continue bên trong các vòng lặp lồng nhau, nó chỉ bỏ qua bước lặp của vòng lặp trong cùng. Vòng lặp bên ngoài vẫn không bị ảnh hưởng

Thí dụ

C




while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
41

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
93

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
3

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
4

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
1

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______1_______2
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
02

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______11_______8
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
07

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______11_______8
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9
Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
2
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
12

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2_______11_______63

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2_______1_______70
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
66

do{

   do{
      
      // statement of inside loop
   }while[condition];

   // statement of outer loop
}while[condition];
2_______11_______68
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
20

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2______41_______0

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
2_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9// with the help of nested for loop6
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
76

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______41_______0

while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
0_______32_______3
while[condition] {

   while[condition] {
      
      // statement of inside loop
   }

   // statement of outer loop
}
9// C program to print elements of Three-Dimensional Array5
* * * 
* * * 
* * * 

* * * 
3

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______0

Element at arr[0][0][0] = 0
Element at arr[0][0][1] = 6
Element at arr[0][1][0] = 1
Element at arr[0][1][1] = 7
Element at arr[0][2][0] = 2
Element at arr[0][2][1] = 8
Element at arr[1][0][0] = 3
Element at arr[1][0][1] = 9
Element at arr[1][1][0] = 4
Element at arr[1][1][1] = 10
Element at arr[1][2][0] = 5
Element at arr[1][2][1] = 11
5_______41_______6
do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
7

do{

   while[condition] {
      
      for [ initialization; condition; increment ] {
      
         // statement of inside for loop
      }

      // statement of inside while loop
   }

   // statement of outer do-while loop
}while[condition];
0

Trong chương trình trên, vòng lặp bên trong sẽ bị bỏ qua khi j bằng 2. Vòng lặp bên ngoài sẽ không bị ảnh hưởng

Chúng ta có thể sử dụng 2 vòng lặp while trong C không?

Chỉ một trong các vòng lặp while hoạt động .

Chúng ta có thể sử dụng 2 vòng lặp while không?

Một vòng lặp while lồng nhau là một câu lệnh while bên trong một câu lệnh while khác . Trong một vòng lặp while lồng nhau, một lần lặp của vòng lặp bên ngoài được thực hiện đầu tiên, sau đó vòng lặp bên trong được thực hiện. Việc thực hiện vòng lặp bên trong tiếp tục cho đến khi điều kiện được mô tả trong vòng lặp bên trong được thỏa mãn.

Bạn có thể thêm vòng lặp while vào vòng lặp while không?

Vòng lặp chứa vòng lặp bên trong vòng lặp được gọi là vòng lặp lồng nhau. Nó có thể chứa vòng lặp for bên trong vòng lặp for hoặc vòng lặp while bên trong vòng lặp while . Cũng có thể vòng lặp while có thể chứa vòng lặp for và ngược lại.

Chúng ta có thể sử dụng lồng nhau trong khi?

Không bắt buộc phải lồng cùng loại vòng lặp . Chúng ta có thể đặt vòng lặp for bên trong vòng lặp while hoặc vòng lặp do-while bên trong vòng lặp for.

Chủ Đề