불린 자료형
위키백과, 우리 모두의 백과사전.
컴퓨터 과학에서 불린(boolean) 자료형은 논리 자료형이라고도 하며, 참과 거짓을 나타내는 데 쓰인다. 주로 참은 1, 거짓은 0에 대응하나 언어마다 차이가 있다. 숫자를 쓰지 않고 참과 거짓을 나타내는 영단어 true와 false를 쓰기도 한다.
목차
에이다(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형으로 변환되지 않기 때문이다.
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. 빈 문자열 ’’or ""는 거짓이고 이외의 문자열은 모두 참.
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
|
자료형 |
|
|---|---|
| 공통 | |
| 수치 | |
| 플레인 텍스트 | |
| 포인터 | |
| 복합 자료형 | |
| 기타 | |
| 관련 항목 | |
| 이 글은 컴퓨터에 관한 토막글입니다. 서로의 지식을 모아 알차게 문서를 완성해 갑시다. |