#include <iostream>
// 문자열 관련 라이브러리
#include <string>
// C++ 표준 라이브러리
using namespace std;
// 조건문
// 1. if문
// 2. switch문
// if (조건식)
// {
// // 조건이 참일 경우 실행될 문장
// }
int main()
{
int intValue1 = 50;
int intValue2 = 100;
if (intValue1 > intValue2) {
cout << intValue1 << "is greater tham " << intValue2 << endl;
}
if (intValue1 < intValue2) {
cout << intValue1 << " is less than " << intValue2 << endl;
}
cout << endl;
// if( 조건식)
//{
// // 조건식이 참일 경우 실행될 문장
//}
//else
//{
// // 위의 조건식에 아무것도 해당하지 않을 때 실행될 문장
//}
if (intValue1 > intValue2)
{
cout << intValue1 << " is greater than " << intValue2 << endl;
}
else
{
cout << intValue1 << " is less than or equal to " << intValue2 << endl;
}
cout << endl;
// if(조건식)
//{
// // 조건식이 참일 때 실행될 문장
//}
//else if(조건식)
//{
// // 조건식이 참일 경우 실행 문장
//}
//else
//{
// // 위에 조건식에 아무것도 해당하지 않을 때 실행 문장
//}
if (intValue1 > intValue2) {
cout << intValue1 << " is greater than " << intValue2 << endl;
}
else if(intValue1 == intValue2)
{
cout << intValue1 << " is equal to " << intValue2 << endl;
}
else
{
cout << intValue1 << " is less than " << intValue2 << endl;
}
cout << endl;
// 다음과 같이 문장을 표현할 수 있다.
if (intValue1 > intValue2)
{
cout << intValue1 << "is greater than" << intValue2 << endl;
}
else
{
if (intValue1 == intValue2)
{
cout << intValue1 << " is equal than " << intValue2 << endl;
}
else
{
cout << intValue1 << "is less than " << intValue2 << endl;
}
}
cout << " " << endl;
// 프로그래밍은 경우의 수를 코딩한다. 다양한 조건이 더 있을 수 있다.
// else문 보다는 else if 문을 쓰는게 좋다.
if (intValue1 > intValue2)
{
cout << intValue1 << " is greater than " << intValue2 << endl;
}
else if (intValue1 == intValue2)
{
cout << intValue1 << "is equal to " << intValue2 << endl;
}
else if (intValue1 < intValue2)
{
cout << intValue1 << " is less than" << intValue2 << endl;
}
cout << endl;
// if문 중첩 (Nestring if statements)
// if문을 다른 if문 내에 중첩 가능하다.
if (intValue1 >= 10)
{
if (intValue1 <= 20)
{
cout << intValue1 << "is between 10 and 20 " << endl;
}
else
{
cout << intValue1 << "is greater than " << endl;
}
}
cout << endl;
// switch문
// switch문은 if문과 같이 조건 제어문에 속한다.
// 하지만 switch문은 if문처럼 조건식이 <, <=, >, >= 과 같이 이상 이하 초과 미만 같은 부동식이 사용될 수 없다.
// 모든 switch문은 if문으로 변경이 가능하다.
// 모든 if문은 switch문으로 변경 불가능하다.
// 만일 if문을 switch문으로 변경이 가능하다면 switch문으로 변경하는 것이 좋다. if문보다 가독성이 더 좋다.
// switch (변수)
// {
// case 값1:
// break;
// case 값2:
// break;
// defailt:
// break;
// }
int intValue3 = 100;
switch (intValue3)
{
case 100:
cout << "intValue3 : " << intValue3 << endl;
break;
case 200:
cout << "intValue3 : " << intValue3 << endl;
break;
case 300:
cout << "intValue3 : " << intValue3 << endl;
break;
default: // 조건에 맞는 값이 없으면 default쪽이 실행된다.
cout << "맞는 값이 없습니다." << endl;
break;
}
cout << endl;
// 스터디 편의상 코드에 수치값을 넣고 있다.
// 코드에 수치값을 넣는 것을 하드코딩이라고 한다.
// 하드코딩을 언제 터질지 모르는 지뢰밭이라고 한다.
// 같은 결과는 같은 케이스로 묶을 수 있다.
int month = 6;
switch (month)
{
case 1:
case 2:
case 12:
cout << "winter" << endl;
break;
case 3:
case 4:
case 5:
cout << "spring" << endl;
break;
case 6:
case 7:
case 8:
cout << "summer" << endl;
break;
case 9:
case 10:
case 11:
cout << "autumn" << endl;
break;
default:
cout << "Wrong choice" << endl;
break;
}
cout << endl;
// 삼항연산자(조건연산자)에 의한 조건문
//if ~ else문은 삼항연산자로 변경이 가능하다
// (조건식) ? 표현식1 : 표현식2;
// 삼항연산자는 조건식이 참일 경우에는 표현식1을 거짓일 때는 표현식2를 수행하는 연산자이다.
// 표현식에는 상수 변수 수식 함수 호출등이 올 수 있다.
// if문을 삼항 연산자로 변경하기
if (intValue1 > intValue2)
{
cout << intValue1 << "is greater than" << intValue2 << endl;
}
else
{
cout << intValue1 << " is less than or equal to " << intValue2 << endl;
}
cout << endl;
// c++ 에서는 수치값과 문자열을 바로 섞어 쓸 수 없다.
// to_string() : 수치값을 문자열로 변경해 주는 함수이다.
string greaterMessage = to_string(intValue1) + "is greater than " + to_string(intValue2);
string lessMessage = to_string(intValue1) + "is less than or equal to " + to_string(intValue2);
string resultString = (intValue1 < intValue2) ? greaterMessage : lessMessage;
cout << "resultString : " << resultString << endl;
cout << endl;
// 두 수 중에서 큰 수를 구해 보기.[
int intValue4 = 15;
int intValue5 = 17;
int bigNum;
if (intValue4 > intValue5)
{
bigNum = intValue4;
}
else
{
bigNum = intValue5;
}
cout << "if문 bigNum : " << bigNum << endl;
cout << endl;
// 삼항연산자로 구해보기
bigNum = (intValue4 > intValue5) ? intValue4 : intValue5;
cout << "삼항연산자 bigNum : " << bigNum << endl;
// 문장을 추가 하기 문장은 스토리 이다.
// 프로그래밍을 한다는 것은 스토리 텔링이다.
}