불린 자료형

위키백과, 우리 모두의 백과사전.
이동: 둘러보기, 찾기

컴퓨터 과학에서 불린(boolean) 자료형논리 자료형이라고도 하며, 참과 거짓을 나타내는 데 쓰인다. 주로 참은 1, 거짓은 0에 대응하나 언어마다 차이가 있다. 숫자를 쓰지 않고 참과 거짓을 나타내는 영단어 true와 false를 쓰기도 한다.

목차

에이다 [편집]

에이다(Ada)의 불린의 예는 다음과 같다.

type Boolean is (False, True);
 
p : Boolean := True;
...
if p then
  ...
end if;

알골 [편집]

알골(Algol)의 불린의 예는 다음과 같다.

  1. op ∨ = (bool a, b) bool:( a | true | b );
  2. op ∧ = (bool a, b) bool: ( a | b | false );
  3. op ¬ = (bool a) bool: ( a | false | true );
  4. op = = (bool a, b) bool:( a∧b ) ∨ ( ¬b∧¬a );
  5. op ≠ = (bool a, b) bool: ¬(a=b);
  6. 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형으로 변환되지 않기 때문이다.

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;

[편집]

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