불리언 자료형

위키백과, 우리 모두의 백과사전.
(Bool에서 넘어옴)

컴퓨터 과학에서 불리언 자료형(Boolean Data Type) 또는 참거짓논리 자료형이라고도 하며, 참과 거짓을 나타내는 데 쓰인다. 주로 참은 1, 거짓은 0에 대응하나 언어마다 차이가 있다. 숫자를 쓰지 않고 참과 거짓을 나타내는 영단어 true와 false를 쓰기도 한다. 불리언(Boolean)이라는 말은 영국의 수학자 겸 논리학자인 조지 불(George Boole)의 이름에서 따온 것이다.

에이다(Ada)[편집]

type Boolean is (False, True);

p : Boolean := True;
...
if p then
  ...
end if;

알골(Algol)[편집]

op ∨ = (bool a, b) bool:( a | true | b );
op ∧ = (bool a, b) bool: ( a | b | false );
op ¬ = (bool a) bool: ( a | false | true );
op = = (bool a, b) bool:( a∧b ) ∨ ( ¬b∧¬a );
op ≠ = (bool a, b) bool: ¬(a=b);
op abs = (bool a)int: ( a | 1 | 0 );

C[편집]

C99 표준부터는 bool 자료형을 지원하나 그 이전에는 불리언 자료형을 지원하지 않았기 때문에 다음과 같은 방법을 사용하였다.

if (my_variable) {
  printf("True!\n");
} else {
  printf("False!\n");
}

위의 공식은 다음과 똑같이 동작한다 : 'boolean'에 주목.

if (my_variable != 0) {
  printf("True!\n");
} else {
  printf("False!\n");
}
typedef enum _boolean { FALSE, TRUE } boolean;
...
boolean b;
#define FALSE 0
#define TRUE 1
...
int f = FALSE;
#include <stdbool.h>
bool b = false;
...
b = true;

C++[편집]

C++은 bool의 true, false 키워드를 사용한다.

C#[편집]

bool myBool = (i == 5);
System.Console.WriteLine(myBool ? "I = 5" : "I != 5");

포트란[편집]

LOGICAL 키워드와 그와 관련한 NOT, AND, OR가 사용된다.

자바[편집]

자바에서는 boolean이라는 자료형을 지원한다. C 같은 언어와 달리 자바에서는 boolean 자료형에 관계된 형 변환을 지원하지 않기 때문에 아래와 같은 코드는 컴파일할 수 없다.

int i = 1;
if (i)
  System.out.println("i is not zero.");
else
  System.out.println("i is zero.");

if 구문에서는 괄호 안에 boolean 조건이 와야 하는데 int형 변수 i가 자바에서는 boolean형으로 변환되지 않기 때문이다.


아래와 같은 코드는 컴파일 할 수 있다.

int num=1;

boolean search=false;

while(num<100) {

if(((num%5)==0)&&((num%7)==0)) {

search=true;

break;

}

num++;

}

if(search)

System.out.println("찾는 정수 : "+num);

else

System.out.println("5의 배수이자 7의 배수인 수를 찾지 못했습니다.");

Ocaml[편집]

# 1 = 1 ;;
- : bool = true

ML[편집]

- fun isittrue x = if x then "YES" else "NO" ;
> val isittrue = fn : bool -> string
- isittrue true;
> val it = "YES" : string
- isittrue false;
> val it = "NO" : string
- isittrue (8=8);
> val it = "YES" : string
- isittrue (7=5);
> val it = "NO" : string

파스칼[편집]

var
  value: Boolean;

...

value := True;
value := False;

if value then
begin
...
end;

[편집]

타입(bool/Boolean) 변수는 따로 없어서 일반변수에 특정값을 지정하여 Boolean형 변수로 인식.

1. 숫자 0은 거짓이고 0 이외의 수(마이너스 포함)은 모두 참.

2. 빈 문자열 '' 또는 ""는 거짓이고 이외의 문자열은 모두 참.

3. undef 값은 거짓.

PHP[편집]

$var = true;
$var = false;
print $var == true ? "T" : "F";
print $var === true ? "T" : "F";
print is_bool($var) ? "T" : "F";
print gettype($var);

파이썬[편집]

>>> class spam: pass # spam is assigned a class object.
...
>>> eggs = "eggs" # eggs is assigned a string object.
>>> spam == eggs # (Note double equals sign for equality testing).
False
>>> spam != eggs # != and == always return bool values.
True
>>> spam and eggs # and returns an operand.
'eggs'
>>> spam or eggs # or also returns an operand.
<class __main__.spam at 0x01292660>
>>>

루비[편집]

a = 0
if (a)
  print "true"
else
  print "false"
end
p false.class
p true.class
p nil.class

SQL[편집]

CREATE TABLE test1
(
  a int,
  b boolean
);

INSERT INTO test1
VALUES (1, true);

INSERT INTO test1
VALUES (2, false);

INSERT INTO test1
VALUES (3, null);

-- The SQL:1999 standard says that vendors can use null in place of the
-- SQL Boolean value unknown. It is left to the vendor to decide if
-- null should be used to completely replace unknown. The standard also
-- says that null should be treated as equivalent to unknown, which is an
-- inconsistency. The following line may not work on all SQL:1999-compliant
-- systems.

INSERT INTO test1
VALUES (4, unknown);

SELECT *
FROM test1;

비주얼 베이직[편집]

Dim isSmall As Boolean
isSmall = intMyNumber < 10 ' Expression evaluates to True or False
If isSmall Then
   MsgBox("The number is small")
End If

Dim hellFreezesOver As Boolean ' Boolean variables are initialized as False
hellFreezesOver = False ' Or you can use an assignment statement
Do
   Call CheckAndProcessUserInput()
Loop Until hellFreezesOver
Sub Voo(ByRef v As Variant)
   v = 1
End Sub

Sub Bar(ByRef b As Boolean)
   b = 1
End Sub

Dim b1 As Boolean, b2 As Boolean
b1 = True
b2 = True
Debug.Print (b1 = b2) 'True
Call Voo(b2)
Debug.Print (b1 = b2) 'False
Call Bar(b2)
Debug.Print (b1 = b2) 'True