C++

[C++] 026 조건부 삼항 연산자

qkrwngus 2021. 3. 18. 09:36

Desc :

 

간단한 if문을 한 줄로 대체할 수 있는 연산자 (코드 최적화)

 

if (x>y)           

z=x;

else              

z=y;

z = x>y? x : y;

 

조건 ? true : false

 


Source Code :

#include <iostream>

using namespace std;

int main()
{
	int x = 1;
	int y = 2;
	int z = 0;

	z = x > y ? x : y;

	cout << "x와 y중 더 큰 값은 : " << z << endl;

	return 0;
}

 


Result :