반응형

자료형 bool

 

참을 의미하는 true, 거짓을 의미하는 false 중 하나의 값을 가진다.

 

**

 

1과 0을써도되는데 굳이 true,false를 쓰는 이유는 가독성을 늘리기 위해서이다.

 

1과 0이 아니라, 논리적인 참과 거짓을 의미하는 키워드로 생각하자.

 

**

 

int형 데이터로 형 변환 시 1과 0이 된다.

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
 
using std::cin;
using std::cout;
using std::endl;
 
// using namespace std;  
// 이 구문은 추천하지 않는다. 
 
bool IsPositive(int i)
{
 
 if(i<0)
  {
   return false;
  }
 else
  {
   return true;
  }
 
}
 
int main(void)
{
 
 int num;
 bool result;
 
 cout<<"숫자 입력 : ";
 cin >> num;
 
 result = IsPostive(num);
  if(result == true)
  {
    cout << "Positive number " << endl;
  }
  else
  {
    cout << "Negative number " << endl;
  }
 
 return 0
 
}

Crocus

 

 

 

==================

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
 
using std::cout;
using std::endl;
 
int main(void)
{
 
 int BOOL = true;
 cout << "BOOL : " <<BOOL <<endl;
 
 BOOL = false;
 cout << "BOOL : "<<BOOL <<endl;
 
 return 0;
 
}
 
//int형으로 변환시 true와 false의 값

Crocus


 

반응형