• 회원가입
  • 로그인
  • 구글아이디로 로그인

[basic] C# - Break/Continue (브레이크/컨티뉴) 키워드 - 반복문 빠져나가기 / 특정 조건 건너띄기

8,391  
목차
  1. break 키워드 - (switch 조건문, 반복문) 탈출
  2. continue 키워드 - 해당 조건만 건너뛰기

 

break 키워드 - (switch 조건문, 반복문) 탈출

 

(switch 조건문, for/while반복문) 탈출/중단.

※ break 위치에 따라 출력 결과 달라질 수 있음.

 


[예제1] switch 조건문 탈출

 

https://homzzang.com/b/cs-16

 


[예제2] for 반복문 탈출

 

using System;

namespace Homzzang

{

  class Program

  {

    static void Main(string[] args)

    {

      for (int i = 0; i < 5; i++) 

      {

        if (i == 3) 

        {

          break;

        }

        Console.WriteLine(i);

      }    

    }

  }

}

 

결과값:

0

1

2


[예제3] while 반복문 앞쪽에서 탈출

 

using System;

namespace Homzzang

{

  class Program

  {

    static void Main(string[] args)

    {

      int i = 0;

      while (i < 5) 

      {

        if (i == 3) 

        {

          break;

        }

        Console.WriteLine(i);

        i++;

      }    

    }

  }

}

 

결과값:

0

1

2


[예제4] while 반복문 뒤쪽에서 탈출.

 

using System;

namespace Homzzang

{

  class Program

  {

    static void Main(string[] args)

    {

      int i = 0;

      while (i < 5) 

      {

        Console.WriteLine(i);

        i++;

        if (i == 3) 

        {

          break;

        }

      }    

    }

  }

}

 

결과값:

0

1

 

continue 키워드 - 해당 조건만 건너뛰기

 

for/while 반복문에서 해당 조건만 건너뛰기.

 


[for 반복문 경우]

 

using System;

namespace Homzzang

{

  class Program

  {

    static void Main(string[] args)

    {

      for (int i = 0; i < 5; i++) 

      {

        if (i == 3) 

        {

          continue;

        }

        Console.WriteLine(i);

      }    

    }

  }

}

 

결과값:

0

1

2

4


[while 반복문 경우]

 

using System;

namespace Homzzang

{

  class Program

  {

    static void Main(string[] args)

    {

      int i = 0;

      while (i < 5) 

      {

        if (i == 3) 

        {

          i++;

          continue;

        }

        Console.WriteLine(i);

        i++;

      }    

    }

  }

}

 

결과값:

0

1

2

4

PS. 핑크색 코드가 없을 시 결과값:  0 1 2



분류 제목
method C# - Methods (메서드) - 정의/호출
method C# - Method Parameters (메서드 매개변수)
method C# - Method Overloading (메서드 오버로딩)
class C# - OOP (객체 지향 프로그래밍)
class C# - Class (클래스) / Object (객체)
class C# - Class Members (클래스 멤버) - 속성(=필드), 메서드
class C# - Constructors (생성자)
class C# - Access Modifiers (접근 수정자)
class C# - Encapsulation (캡슐화) ※ Getter (게터) / Setter (세터)
class C# - Inheritance (상속)
class C# - Polymorphism (다형성)
class C# - Abstraction (추상화)
class C# - interface (인터페이스) ★
class C# - enums (이넘) - 상수 열거형 클래스
class C# - Files (파일) - 파일생성/파일읽기/파일쓰기
class C# - Exceptions (= Try...Catch..) - 에러 제어
howto C# - 사용자 입력값 더하기 ★ (Add Two Numbers)
2/2
목록
찾아주셔서 감사합니다. Since 2012