Dev A.i 2023. 9. 3. 16:58

C#에서 반복문을 사용하는 예시는 다음과 같습니다:

 

for 반복문 예시:

using System;

namespace ForLoopExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1부터 10까지 출력
            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine(i);
            }
        }
    }
}

이 예제에서는 for 반복문을 사용하여 1부터 10까지의 정수를 출력합니다.

 

while 반복문 예시:

using System;

namespace WhileLoopExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;

            // i가 10 이하인 동안 반복
            while (i <= 10)
            {
                Console.WriteLine(i);
                i++;
            }
        }
    }
}

이 예제에서는 while 반복문을 사용하여 1부터 10까지의 정수를 출력합니다.

 

do-while 반복문 예시:

using System;

namespace DoWhileLoopExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;

            // i가 10 이하인 동안 반복
            do
            {
                Console.WriteLine(i);
                i++;
            } while (i <= 10);
        }
    }
}

이 예제에서는 do-while 반복문을 사용하여 1부터 10까지의 정수를 출력합니다. do-while 반복문은 루프 내의 코드를 최소 한 번 실행합니다