if(조건)
{
실행내용
}
e.g.,
if(3>1)
{
Console.WriteLine("3은 1보다 크다");
}
if(0>1)
{
Console.WriteLine("0은 1보다 크다.");
}
if(조건)
{
}
else if(조건)
{
실행 내용
}
e.g.,
int x = 20;
if(x > 10)
{
Console.WriteLine("x는 10보다 크다."); // 실행
}
else if(x < 5) // 실행 X
{
Console.WriteLine("x는 10보다는 작지만 5보다는 크다.");
}
int x = 8;
if(x > 10) // 실행 X
{
Console.WriteLine("x는 10보다 크다.");
}
else if(x > 5) // 실행
{
Console.WriteLine("x는 10보다는 작지만 5보다는 크다.");
}
if(조건)
{
}
else
{
실행 내용
}
e.g.,
if(3 > 1)
{
Console.WriteLine("3은 1보다 크다."); // 실행
}
else
{
Console.WriteLine("틀렸습니다.");
}
if(0 > 1)
{
Console.WriteLine("0은 1보다 크다.");
}
else
{
Console.WirteLine("틀렸습니다."); // 실행
}
if(조건)
{
실행내용
}
else if(추가 조건) // 여러번 사용 가능!
{
실행내용
}
else
{
실행내용
}
e.g.,
int score = 100;
if (score > 90)
{
Console.WriteLine("성적이 매우 우수합니다.");
}
else if (score > 70)
{
Console.WriteLine("성적이 우수합니다.");
}
else if (score > 50)
{
Console.WriteLine("합격은 했지만 좀 더 노력하세요.");
}
else
{
Console.WriteLine("불합격 입니다.");
}
swich( 조건 변수 )
{
case 상수조건1:
실행내용
break;
case 상수조건2:
실행내용
break;
case 상수조건3:
실행내용
break;
default: // case를 실행하지 않을 때 반드시 실행하는 내용.
실행내용
break;
}
case에 들어가는 조건은 고정된 상수!
(e.g., 새로운 변수 사용, 변할 수 있는 값 절대 사용 X,)
case에 들어가는 조건 중복 X!
if ~ else 로 대체 할 수 있지만, 한가지 조건을 더 정교하게 체크할 수 있다!
int x = 5;
switch(x)
{
case 1:
}
// 갈림길 선택에 따라 여러가지 스크립트 표현
// 길을 갈 확률이 주사위로 정해짐
using System;
Random rand = new Random();
string boss = "Sgt. Munch";
string sqd = "Squads";
int percent = rand.Next(0, 2);
if( percent == 0)
{
Console.WriteLine("숨겨진 땅에 도착했습니다. 긴장하세요! 강력한 적이 나타나지만, 보상은 확실합니다.");
Console.WriteLine(boss + ": Hi, I eagerly waited for you.");
Console.WriteLine(boss + ": You can never escape from here!");
}
else if (percent == 1)
{
Console.WriteLine("안타깝게도, 함정에 빠졌습니다. 대원 모두 공포에 빠집니다.");
Console.WriteLine(sqd + ": [Scream...]");
}
else
{
Console.WriteLine("마을에 도착했습니다.");
}
뭔가 어설프다. 대사를 한글로 쓰는게 아직 오글 거린다. 하지만 많이 써봐야지!
int percent= rand.Next(0,2);
switch(percent)
{
case 1:
Console.WriteLine("숨겨진 땅에 도착했습니다. 긴장하세요! 강력한 적이 나타나지만, 보상은 확실합니다.");
Console.WriteLine(boss + ": Hi, I eagerly waited for you.");
Console.WriteLine(boss + ": You can never escape from here!");
break;
case 2:
Console.WriteLine("안타깝게도, 함정에 빠졌습니다. 대원 모두 공포에 빠집니다.");
Console.WriteLine(sqd + ": [Scream...]");
break;
default:
Console.WriteLine("마을에 도착했습니다.");
break;
}
확실히 switch case가 가독성이 좋다.
훌륭한 글이네요. 감사합니다.