ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C#] 반복문 (while, do while, for, foreach)
    C# 2023. 5. 29. 14:13

    1. while

    형식
    while ( 조건식 )
        반복실행 할 코드

    예시

    /*
     * 출력
     * i : 10
     * i : 9
     * i : 8
     * i : 7
     * i : 6
     * i : 5
     * i : 4
     * i : 3
     * i : 2
     * i : 1
     * 
     */
    namespace While
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                int i = 10;
    
                while (i > 0)
                {
                    Console.WriteLine($"i : {i--}");
                }
            }
        }
    }

    2. DoWhile

    형식
    do
    {
        반복 실행 할 코드 블록
    }
    while ( 조건식 );

    예시

    /*
    실행
    a) i : 10
    a) i : 9
    a) i : 8
    a) i : 7
    a) i : 6
    a) i : 5
    a) i : 4
    a) i : 3
    a) i : 2
    a) i : 1
    b) i : 0
     */
    namespace DoWhile
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                int i = 10;
    
                do
                {
                    Console.WriteLine("a) i : {0}", i--);
                }
                while (i > 0);
    
                do
                {
                    Console.WriteLine("b) i : {0}", i--); // i는 이미 0 이지만 코드는 한 차례 실행 됨
                }
                while (i > 0);
            }
        }
    }

    3. For

    형식
    for( 초기화식; 조건식; 반복식)
        반복실행할 코드;
    초기화식 반복을 실행하기 전에 가장 먼저, 딱 한 번만 실행되는 코드입니다.
    for 반복문에서 사용할 변수 등을 이곳에서 초기화 합니다.
    조건식 반복을 계속 수행할지를 결정하는 식
    조건식의 결과가 false가 되면 반복을 종료
    반복식 반복이 끝날 때맏 실행 됨
    반복식이 끝난뒤 조건식이 실행됨

    예제

    /*
    실행
    0
    1
    2
    3
    4
     */
    namespace For
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine(i);
                }
            }
        }
    }

    중첩 for문 예시

    /*
    실행
    *
    **
    ***
    ****
    *****
     */
    namespace ForFor
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                for ( int i = 0; i < 5; i++ )
                {
                    for ( int j = 0; j <= i; j++ )
                    {
                        Console.Write("*");
                    }
                    Console.WriteLine();
                }
            }
        }
    }

    4.foreach

    설명 foreach 문은 배열 또는 컬렉션을 순회하며 각 데이터 요소에 차례대로 접근함
    배열의 끝에 도달하면 자동으로 반복이 종료됨
    형식 foreach ( 데이터형식 변수명 in 배열_또는_컬렉션)
        코드_또는_코드블록

    예시

    /*
    출력
    0
    1
    2
    3
    4
     */
    namespace ForEach
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                int[] arr = new int[] { 0, 1, 2, 3, 4 };
    
                foreach (int a in arr)
                {
                    Console.WriteLine(a);
                }
            }
        }
    }

    댓글