의사코드

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

의사코드(슈도코드, pseudocode[1])는 프로그램을 작성할 때 각 모듈이 작동하는 논리를 표현하기 위한 언어이다. 특정 프로그래밍 언어의 문법에 따라 쓰인 것이 아니라, 일반적인 언어로 코드를 흉내 내어 알고리즘을 써놓은 코드를 말한다. 의사(擬似)코드는 말 그대로 흉내만 내는 코드이기 때문에, 실제적인 프로그래밍 언어로 작성된 코드처럼 컴퓨터에서 실행할 수 없으며, 특정 언어로 프로그램을 작성하기 전에 알고리즘의 모델을 대략적으로 모델링하는 데에 쓰인다.

의사코드는 실제 프로그래밍 언어처럼 엄밀한 문법을 따를 필요가 없기 때문에 다양한 변종이 존재한다. 그러나 보통 사용자가 많은 C리스프, 포트란 프로그래밍 언어 등의 문법을 본딴 모양이 많다. 엄밀한 묘사가 불필요한 부분에는 자연어가 자유롭게 쓰이기도 한다.

컴퓨터 과학의 전공 서적에서는 다양한 언어 구사자들이 모두 이해할 수 있도록 특히 의사코드를 많이 사용하여 설명한다. 또한 보통 의사코드는 저자마다 그 문법이 다르기 때문에, 의 서두에는 의사코드의 문법이 간략히 설명되어 있기도 하다.

[편집]

의사코드의 예는 다음과 같다.

포트란 스타일 의사코드

program fizzbuzz
Do i = 1 to 100 
    set print_number to true
    If i is divisible by 3
        print "Fizz"
        set print_number to false
    If i is divisible by 5
        print "Buzz" 
        set print_number to false
    If print_number, print i
    print a newline
end do

파스칼 스타일 의사코드

procedure fizzbuzz
For i := 1 to 100 do
    set print_number to true;
    If i is divisible by 3 then
        print "Fizz";
        set print_number to false;
    If i is divisible by 5 then
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
end

C 스타일 의사코드

void function fizzbuzz
For (i = 1; i<=100; i++) {
    set print_number to true;
    If i is divisible by 3
        print "Fizz";
        set print_number to false;
    If i is divisible by 5
        print "Buzz";
        set print_number to false;
    If print_number, print i;
    print a newline;
}

수학 스타일 의사코드:

Return

참고 문헌[편집]

Justin Zobel (2004). "Algorithms" in Writing for Computer Science (second edition). Springer. ISBN 1-85233-802-4.

각주[편집]

  1. BJ Furman (29 December 2010). "Notes on Algorithms, Pseudocode, and Flowcharts" Archived 2016년 12월 13일 - 웨이백 머신. engr.sjsu.edu.