파스칼 (프로그래밍 언어): 두 판 사이의 차이

내용 삭제됨 내용 추가됨
Choboty (토론 | 기여)
TedBot (토론 | 기여)
잔글 봇: 틀 이름 및 스타일 정리
39번째 줄:
=== Hello world ===
 
<sourcesyntaxhighlight lang="pascal">
program HelloWorld(output);
begin
writeln('Hello, World!')
end.
</syntaxhighlight>
</source>
 
=== 콘솔 입출력 ===
<sourcesyntaxhighlight lang="Pascal">
program WriteName;
 
58번째 줄:
WriteLn('Hello ', Name);
END.
</syntaxhighlight>
</source>
 
=== 자료 구조 ===
<sourcesyntaxhighlight lang="pascal">
var
r: Real;
68번째 줄:
b: Boolean;
e: (apple, pear, banana, orange, lemon);
</syntaxhighlight>
</source>
 
일반 유형의 하부 범위는 다음과 같이 짤 수 있다:
 
<sourcesyntaxhighlight lang="pascal">
type
fruit=(apple, pear, banana, orange, lemon);
80번째 줄:
y: 'a'..'z';
z: pear..orange;
</syntaxhighlight>
</source>
 
다른 프로그래밍 언어와 달리, 파스칼은 집합 유형을 지원한다:
 
<sourcesyntaxhighlight lang="pascal">
var
set1: set of 1..10;
set2: set of 'a'..'z';
set3: set of pear..orange;
</syntaxhighlight>
</source>
 
집합은 현대 수학에 있어 기본적인 개념이며 수많은 알고리즘이 집합을 사용하여 정의된다. 그러므로 이러한 알고리즘을 추가하는 것은 파스칼에 매우 알맞다고 볼 수 있다. 집합형 연산은 효율적이기도 하다.
95번째 줄:
많은 파스칼 컴파일러가
 
<sourcesyntaxhighlight lang="pascal">
if i in [5..10] then
...
</syntaxhighlight>
</source>
 
 
<sourcesyntaxhighlight lang="pascal">
if (i>4) and (i<11) then
...
</syntaxhighlight>
</source>
 
보다 더 빠르게 수행한다.
111번째 줄:
형(type)은 선언을 통해 다른 형으로부터 정의될 수 있다:
 
<sourcesyntaxhighlight lang="pascal">
type
x = Integer;
y = x;
...
</syntaxhighlight>
</source>
 
또한, 복잡한 형은 단순한 형으로 구성할 수 있다:
 
<sourcesyntaxhighlight lang="pascal">
type
a = Array [1..10] of Integer;
128번째 줄:
end;
c = File of a;
</syntaxhighlight>
</source>
 
==== 포인터 ====
파스칼은 [[포인터 (프로그래밍)|포인터]] 사용을 지원한다:
<sourcesyntaxhighlight lang="pascal">
type
a = ^b;
142번째 줄:
var
pointer_to_b: a;
</syntaxhighlight>
</source>
 
가변 <code>''pointer_to_b''</code>가 자료형 <code>''b''</code> 레코드의 포인터이다. 포인터들은 선언 전에 사용할 수 있다. 새로운 레코드를 만들어 값<code>''10''</code>과 <code>''A''</code>를 레코드 안의 필드 <code>''a'', ''b''</code>로 할당하려면, 다음과 같은 명령어를 사용하면 된다:
 
<sourcesyntaxhighlight lang="pascal">
new(pointer_to_b);
pointer_to_b^.x := 10;
152번째 줄:
pointer_to_b^.z := nil;
...
</syntaxhighlight>
</source>
 
한정자를 보다 효과적으로 통제하기 위해서는 <code>''with''</code> 문을 써서 다음과 같은 방식을 사용할 수도 있다.
<sourcesyntaxhighlight lang="pascal">
new(pointer_to_b);
with pointer_to_b^ do
164번째 줄:
end;
...
</syntaxhighlight>
</source>
 
=== 제어 구조 ===
파스칼은 [[구조 프로그래밍]] 언어이며, 제어 흐름을 goto 문 없이 표준화할 수 있다.
 
<sourcesyntaxhighlight lang="pascal">
while a <> b do writeln('Waiting');
 
180번째 줄:
 
repeat a := a + 1 until a = 10;
</syntaxhighlight>
</source>
 
=== 절차 및 함수 ===
파스칼 구조는 절차와 함수로 짜여진다.
 
<sourcesyntaxhighlight lang="pascal">
program mine(output);
var i : integer;
205번째 줄:
while i <= 10 do print(i)
end.
</syntaxhighlight>
</source>
 
절차와 함수는 어느 깊이로든 놓일 수 있으며, 프로그램 구조는 논리 외부 블록이다.