산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C#의 마스터 중첩 루프:For, While 및 Do-While 설명

다른 루프 내의 루프를 중첩 루프라고 합니다. 중첩 루프는 다음과 같습니다:

Outer-Loop 
{
 // body of outer-loop
 Inner-Loop
 {
 // body of inner-loop
 }
... ... ...
}

보시다시피 외부 루프 내부 루프를 포함합니다. . 내부 루프는 외부 루프의 일부이며 외부 루프 본문 내에서 시작하고 끝나야 합니다.

외부 루프가 반복될 때마다 내부 루프가 완전히 실행됩니다.

중첩된 for 루프

다른 for 루프 안에 있는 for 루프를 중첩 for 루프라고 합니다.

예:

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
}

예 1:중첩 for 루프

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);
 }
 }
}

프로그램을 실행하면 다음과 같은 결과가 출력됩니다:

Outer Loop runs 5 times
Inner Loop runs 25 times

이 프로그램에서는 외부 루프가 5번 실행됩니다. 외부 루프가 실행될 때마다 내부 루프가 5번 실행되어 총 25번 실행됩니다.

예 2:패턴 인쇄를 위해 중첩된 for 루프

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();
 }
 }
 }
}

프로그램을 실행하면 다음과 같은 결과가 출력됩니다:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

중첩 while 루프

다른 while 루프 안에 있는 while 루프를 중첩 while 루프라고 합니다.

예:

while (condition-1)
{
 // body of outer while loop
 while (condition-2)
 {
 // body of inner while loop
 }
 // body of outer while loop
}

예 3:중첩된 while 루프

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();
 }
 }
 }
}

프로그램을 실행하면 다음과 같은 결과가 출력됩니다:

(0,0) (0,1)
(1,0) (1,1)

중첩된 do-while 루프

다른 do-while 루프 안에 있는 do-while 루프를 중첩된 do-while 루프라고 합니다.

예:

do
{
 // body of outer while loop
 do
 {
 // body of inner while loop
 } while (condition-2);
 // body of outer while loop
} while (condition-1);

예 4:중첩된 do-while 루프

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);
 }
 }
}

프로그램을 실행하면 다음과 같은 결과가 출력됩니다:

(0,0) (0,1)
(1,0) (1,1)

다른 내부 및 외부 중첩 루프

동일한 유형의 루프를 중첩할 필요는 없습니다. while 루프 안에 for 루프를 넣거나 for 루프 안에 do-while 루프를 넣을 수 있습니다.

예 5:C# 중첩 루프:서로 다른 내부 루프와 외부 루프

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++;
 }
 }
 }
}

프로그램을 실행하면 다음과 같은 결과가 출력됩니다:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

위 프로그램에서는 for 루프가 while 루프 내에 배치됩니다. 루프 내에서는 다양한 유형의 루프를 사용할 수 있습니다.


C 언어

  1. C++ 프로그래밍:C++란 무엇입니까 | C++의 기본 개념 배우기
  2. C 표준 라이브러리 함수
  3. 참조에 의한 C++ 호출:포인터 사용 [예제 포함]
  4. C 구조체와 포인터
  5. C 튜토리얼
  6. C# 부분 클래스 및 부분 메서드
  7. while과 do-while의 차이점:예제와 함께 설명
  8. C# Hello World - 첫 번째 C# 프로그램
  9. C++ 날짜 및 시간
  10. C++ 루프 유형