
👇코드 예제
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
int age = 10;
void Start()
{
if(age == 10)
{
Debug.Log("나이는 10 입니다");
}
}
}
👇코드 예제
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
int age = 10;
void Start()
{
if(age == 20)
Debug.Log("나이는 10 입니다");
Debug.Log("안녕하세요");
Debug.Log("날씨가 참 좋죠?");
}
}
👇 실행 결과

if (조건) {내용...} 과 같이 실행 범위를 중괄호 "{" 로 묶지 않으면 그림과 같이 if 조건 뒤의 한문장만 if에 포함되는 것으로 간주한다
"안녕하세요 날씨가 참 좋죠?" 는 조건에 관계없이 실행된 모습
만약 조건 연산 식 뒤에 세미콜론(;) 을 붙였을 때는❓
👇코드 예제
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
int age = 10;
void Start()
{
if (age == 20) ;
{
Debug.Log("나이는 20 입니다");
}
}
}
👇 실행 결과

👇코드 예제
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private int x = 7;
void Start()
{
if (x % 2 == 0)
{
Debug.Log("x는 짝수다");
}
// 앞에서 배운 연산자를 이용해 조건문 내부에
// 여러개의 조건식을 동시에 만족하도록 설정 가능
if ( x > 5 && x < 10 )
{
Debug.Log("x는 5보다 크고 10보다 작다");
}
// 조건문 내부에 중첩해서 조건문 작성 가능
// 위의 if 조건문과 같은 결과를 출력할 것이다
if (x > 5)
{
if ( x < 10 )
{
Debug.Log("x는 5보다 크고 10보다 작다");
}
}
Debug.Log($"x의 값은 {x}");
}
}
👇 실행 결과

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
private int x = 7;
void Start()
{
if (x % 2 == 0)
{
Debug.Log("x는 짝수다");
}
else
{
Debug.Log("x는 홀수다");
}
}
}
👇 조건문 문법 규칙

👇 코드 예제
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public int x = 80;
void Start()
{
if (x >= 90)
{
Debug.Log("학점 : A+");
}
else if ( x >= 80 )
{
Debug.Log("학점 : B+");
}
else if ( x >= 70 )
{
Debug.Log("학점 : C+");
}
else if ( x >= 60 )
{
Debug.Log("학점 : D");
}
else
{
Debug.Log("학점 : F");
}
}
}
👇 실행 결과

👇 코드 예제
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public int x = 80;
void Start()
{
x /= 10;
switch ( x )
{
case 10:
case 9:
Debug.Log("학점 : A+");
break;
case 8:
Debug.Log("학점 : B+");
break;
case 7:
Debug.Log("학점 : C+");
break;
case 6:
Debug.Log("학점 : D");
break;
default:
Debug.Log("학점 : F");
break;
}
}
}
👇 실행 결과
