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);
}
}
}
}