C#

[C# ๊ธฐ์ดˆ๊ณต๋ถ€] ์„ ํƒ๋ฌธ (if~else)(switch, case)

zoozoo2 2023. 1. 4. 21:20

๐ŸŸง if ~ else

 

๐ŸŸง switch, case

-์ •์ˆ˜, ๋ฌธ์ž์ƒ์ˆ˜, ๋ฌธ์ž์—ด

- ๋ชจ๋“  case์™€ defaul์—๋Š” break๊ฐ€ ๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผ ํ•จ

 

namespace ConsoleApp4
{
    class Myclass
    {

        static void Main(string[] args)
        {
            int nNum = 1;
            switch(nNum)
            {
                case 1:
                    Console.WriteLine("1 ์ž…๋‹ˆ๋‹ค.");
                    break;
                case 2:
                    Console.WriteLine("2 ์ž…๋‹ˆ๋‹ค.");
                    break;
            }

            string str = "yes";
            switch (str)
            {
                case "yes":
                    Console.WriteLine("yes ์ž…๋‹ˆ๋‹ค.");
                    break;
                case "no":
                    Console.WriteLine("no ์ž…๋‹ˆ๋‹ค.");
                    break;
            }
        }
    }
}

 

 

๐ŸŸช ๋ฐ˜๋ณต๋ฌธ

โ–ถ for

for(; ;) → ๋ฌดํ•œ๋ฐ˜๋ณต

โ–ถ while, do~while

while(true)

โ–ถforeach

์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ๊ฐ’์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ฝ๋Š” ์—ญํ•  → ์ฝ๊ธฐ ์ „์šฉ

foreach(๋ฐ์ดํ„ฐํ˜• ๋ณ€์ˆ˜ in ๋ฐฐ์—ด๋ช…(์ปฌ๋ ‰์…˜๋ช…))

{

 

}

 

namespace ConsoleApp4
{
    class Myclass
    {

        static void Main(string[] args)
        {
            int[] Array1 = { 1, 2, 3, 4 };

            foreach(int nValue in Array1)
            {
                Console.WriteLine(nValue);
            }
        }
    }
}

๋ฌด์กฐ๊ฑด ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ฝ์Œ

foreach๋Š” ์ฝ๊ธฐ ์ „์šฉ!

foreach๋ฌธ ์•ˆ์—์„œ nValue๋Š” ์ฝ๊ธฐ ์ „์šฉ์ด๋ผ 

nValue +1; ๊ณผ ๊ฐ™์€ ๊ณ„์‚ฐ์„ ํ•œ๋‹ค๋ฉด ์—๋Ÿฌ ๋‚จ

namespace ConsoleApp4
{
    class Myclass
    {

        static void Main(string[] args)
        {
            ArrayList List = new ArrayList(); 
            List.Add(1);
            List.Add(2);    
            List.Add(3);

            foreach(int m in List)
            {
                Console.WriteLine(m);
            }

        }
    }
}