본문 바로가기
카테고리 없음

[코딩일기] C# break

by mania2321 2024. 7. 2.

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;


// 종이를 몇 번 접으면 두께가 1m가 넘을까?


public class HelloWorld : MonoBehaviour
{
    void Start()
    {
        float thickness = 0.0001f;    // 종이 한 장의 두께(m 단위)
        int count = 0;     // 종이를 접는 횟수

        while (thickness <= 1)    // 종이 두께가 1m보다 크거나 같을 때까지
        {
            count++;                 // 계속 더한다
            thickness *= 2;       // 2씩 곱한 후 적용

            if(thickness >= 0.5f)        // 두께가 0.5보다 커지면 
            {
                break;                  // 그냥 나가라(그러면 while 밖으로 나감)
            }                           // break ==> 어떤 반복문에서 중간에 내보내는 역할.
        }

        Debug.Log(count + "번 접었습니다.");
    }
}

댓글