반복문, 분기문, 배열 (retr0 강의노트)

Se0ng_1l·2022년 7월 21일
0
post-thumbnail

반복문, 분기문

분기문 : switch
반복문 : for, while, do-while
배열 : arr

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //switch 분기문
        //변수에 따라서 만족하는 조건문을 실행시킨다.
        int year = 2017;
        switch (year)
        {
            case 2012:
                Debug.Log("레미제라블");
                break;
            
            case 2015:
                Debug.Log("쥬라기월드");
                break;
            
            case 2016:
                Debug.Log("곡성");
                break;
            
            case 2017:
                Debug.Log("트랜스포머5");
                break;
            
            default:
                Debug.Log("년도가 해당사항 없음");
                break;
        }
        
        // 반복문
        
        // for
        for (int i = 0; i < 10; i++) // 초기값; 조건문; 업데이트
        {
            Debug.Log("현재 순번: " + i);
        }
        Debug.Log("루프 끝");
        
        // while
        bool isShot = false;
        int index = 0;
        int luckyNumber = 4;
        
        while (!isShot)
        {
            Debug.Log("현재시도: " + index);
            if (index == luckyNumber)
            {
                Debug.Log("총알에 맞았다.");
                isShot = true;
            }
            else
            {
                Debug.Log("총알에 맞지않았다.");
            }
            index++;
        }

        // 무조건 한번은 실행하는 while문
        do 
        {
            Debug.Log("do-While");
        } while (!isShot);
    }
}

배열

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // 배열 선언
        int[] scores = new int[10];
        // scores [0][1][2][3][4][5][6][7][8][9]

        scores[0] = 5;
        scores[1] = 15;
        scores[2] = 25;
        scores[3] = 35;
        scores[4] = 45;
        scores[5] = 55;
        scores[6] = 65;
        scores[7] = 75;
        scores[8] = 85;
        scores[9] = 95;
        
        for(int i = 0; i < 10; i++)
            Debug.Log("scores[" + i + "] = " + scores[i]);

        scores = new int[20]; // 방크기 늘리기 단, 이 경우 기존에 있던값 다 사라짐
    }
}
profile
치타가 되고 싶은 취준생

0개의 댓글