본문 바로가기

c# ☃️

[C#] 삼항 연산자 - ?/:/if

namespace EnhanIf;
class Program
{
    static void Main(string[] args)
    {
        int temp = -5;
        string stateOfMatter;

        //삼항연산자 조건 ? true 일때 실행 : false 일때 실행  
        stateOfMatter = temp < 0 ? "solid" : "liquid";
        Console.WriteLine("state of matter is {0}", stateOfMatter);

        temp += 30;

        stateOfMatter = temp < 0 ? "solid" : temp>100 ? "gas" : "liquid";
        Console.WriteLine("state of matter is {0}", stateOfMatter);


    }
}

실행결과