C#

[C#] break, continue, goto ๋ฐ˜๋ณต๋ฌธ ์ œ์–ด

์กฐ๋ฐˆ๋ฐ 2022. 9. 13. 16:04

๐Ÿ’ก break ๋ฌธ

: ๋ฐ˜๋ณต๋ฌธ (for, while, do), switch ์„ ๋น ์ ธ๋‚˜์˜ฌ ๋•Œ ์‚ฌ์šฉ

โ— ๋ฌดํ•œ๋ฃจํ”„ ๋น ์ ธ๋‚˜์˜ค๊ธฐ

: ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ ๋ฃจํ”„ ๋น ์ ธ์˜ค๋Š” ๊ตฌ๋ฌธ, break

 

๐Ÿ’ก continue ๋ฌธ

: ์ผ๋ถ€ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ๊ฑด๋„ˆ๋›ด๋‹ค.

: ๋ฐ˜๋ณต๋ฌธ์—์„œ continue ๋งŒ๋‚˜๋ฉด continue ์•„๋ž˜ ์ฝ”๋“œ๋Š” ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ๋ฐ˜๋ณต๋ฌธ์˜ ๋‹ค์Œ ๋ฐ˜๋ณต์œผ๋กœ ์ด๋™.

ex) for๋ฌธ์—์„œ continue ๋งŒ๋‚˜๋ฉด ์•„๋ž˜ ์ฝ”๋“œ ์‹คํ–‰X -> ์ฆ๊ฐ€์‹์œผ๋กœ ๋„˜์–ด๊ฐ

 

๐Ÿ’ก goto ๋ฌธ 

: ํŠน์ • ๋ ˆ์ด๋ธ”๋กœ ์ด๋™

โ— ๋ ˆ์ด๋ธ” == ๋ ˆ์ด๋ธ”๋ช… + ์ฝœ๋ก (:) 

   : ํ‰์ƒ์‹œ์—๋Š” ์ฃผ์„์ฒ˜๋Ÿผ ์•„๋ฌด ์˜๋ฏธ ์—†๋Š” ์ฝ”๋“œ๋กœ ์‚ฌ์šฉ, goto ๊ตฌ๋ฌธ ๋’ค์— ๋ ˆ์ด๋ธ” ์ง€์ •ํ•˜๋ฉด ํ•ด๋‹น ๋ ˆ์ด๋ธ”๋กœ ์ด๋™

๋ ˆ์ด๋ธ” : 
goto ๋ ˆ์ด๋ธ”;
using System;

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("์‹œ์ž‘");
        Start:
            Console.Write("0 1 2 ์ค‘ ํ•˜๋‚˜ ์ž…๋ ฅ >> ");
            int num = Convert.ToInt32(Console.ReadLine());

            if (num == 1)
            {
                goto num1;
            }
            else if (num == 2)
            {
                goto num2;
            }else
            {
                goto END;
            }

        num1:
            Console.WriteLine("1์žฅ ์ž…๋‹ˆ๋‹ค.");
            goto Start;
        num2:
            Console.WriteLine("2์žฅ ์ž…๋‹ˆ๋‹ค.");
            goto Start;
        END:
            Console.WriteLine("์ข…๋ฃŒ");
        }
    }
}