C 언어
다른 루프 내의 루프를 중첩 루프라고 합니다. 중첩 루프는 다음과 같습니다:
Outer-Loop
{
// body of outer-loop
Inner-Loop
{
// body of inner-loop
}
... ... ...
}
보시다시피 외부 루프 내부 루프를 포함합니다. . 내부 루프는 외부 루프의 일부이며 외부 루프 본문 내에서 시작하고 끝나야 합니다.
외부 루프가 반복될 때마다 내부 루프가 완전히 실행됩니다.
다른 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
}
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번 실행됩니다.
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 (condition-1)
{
// body of outer while loop
while (condition-2)
{
// body of inner while loop
}
// body of outer 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();
}
}
}
}
프로그램을 실행하면 다음과 같은 결과가 출력됩니다:
(0,0) (0,1) (1,0) (1,1)
다른 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);
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 루프를 넣을 수 있습니다.
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 언어
구성품 및 소모품 SparkFun Arduino Pro Mini 328 - 5V/16MHz × 5 5 초음파 센서 × 5 Perfboard × 5 진동 모터 × 5 부저 × 5 5mm LED:빨간색 × 5 슬라이드 스위치 × 5 암컷 헤더 8 위치 1 행(0.1) × 2 남성 헤더 40 위치 1 행(0.1) × 2 점퍼 와이어(일반) × 4
벡터 FL90 파이버 레이저 절단기 영국에서 가장 인기 있는 산업용 광섬유 레이저 시스템 중 하나입니다. , 교육, 제조 및 제조 부문 모두에서 신뢰를 받고 있습니다. 정밀 금속 절단용으로 설계된 이 제품은 스테인리스강, 연강, 알루미늄, 황동 및 구리에 탁월한 결과를 제공합니다. 작은 설치 공간, 최대 3kW의 강력한 레이저 소스 옵션, 사용자 친화적인 제어 기능을 갖춘 FL90은 현대적인 금속 레이저 절단 기계로 업그레이드하는 기업에 이상적입니다. 또는 학생들에게 고급 제조를 소개하는 교육 환경. Mantech Machinery