본문 바로가기
Software/Unity

[Unity] 6. 반복문

by yunaoh2 2021. 8. 25.

1. while문

while(조건){
   로직
}


*break : 반복 중지
while (health)>0) {
   health - - ;
   if (health > 0 )
      Debug.Log("독 데미지를 입었습니다." + health);
   else
      Debug.Log("사망하였습니다.);
   if (health == 10) {
      Debug.Log("해독제를 사용합니다.");
      break;
}

2. for문

for(연산될 변수 ; 조건 ; 연산) { 
    로직
}

변수를 가지고 연산을 통해 조건으로 몇번을 한다.
for (int count = 0 ; count < 10 ; count++ ) {
   health ++ ;
   Debug.Log("붕대로 치료 중..." + health);
}

#count가 0에서 9까지 다음 로직을 출력해라.

 

for문은 그룹형 변수에 좋다. string[] monsters = {"슬라임", "사막뱀", "악마"};
for (int index = 0 ; index < monsters.Length; index++){
   Debug.Log("이 지역에 있는 몬스터:"+monsters[index]);
}

# index가 0부터 몬스터 길이만큼의 몬스터를 출력하라.
foreach문
그룹형 변수에 깔끔하게 적용함.
string[] monsters = {"슬라임", "사막뱀", "악마"};

foreach(string monster in mosters){
   Debug.Log("이 지역에 있는 몬스터:" + monster);
}

#monsters에 있는 모든 변수를 출력하라.

 

 

##참고영상

https://www.youtube.com/watch?v=j6XLEqgq-dE&list=PLO-mt5Iu5TeYI4dbYwWP8JqZMC9iuUIW2&index=5 

 

 

'Software > Unity' 카테고리의 다른 글

[Unity] 8. 게임오브젝트의 흐름  (0) 2021.08.31
[Unity] 7. 함수  (0) 2021.08.31
[Unity] 5. 조건문  (0) 2021.08.25
[Unity] C#파일 생성하고 적용하기  (0) 2021.08.25
[Unity] 03. C# 언어기초 (연산자)  (0) 2021.05.21