|
|
| Line 1: |
Line 1: |
| ==case .. of 구문== | | ===case .. of 구문=== |
|
| |
|
| 상태 분기에 대한 또 다른 방법이 있는데, 그것이 바로 case .. of 구문 입니다. case 순서 값에 따라 실행을 나눕니다. 식당 프로그램은 case of 구문의 사용을 나타낼 것입니다. | | 상태 분기에 대한 또 다른 방법이 있는데, 그것이 바로 case .. of 구문 입니다. case 순서 값에 따라 실행을 나눕니다. 식당 프로그램은 case of 구문의 사용을 나타낼 것입니다. |
|
| |
| ===음식점 프로그램===
| |
|
| |
| <syntaxhighlight lang="pascal">
| |
| var
| |
| Meal: Byte;
| |
| begin
| |
| Writeln('Welcome to Pascal Restaurant. Please select your order');
| |
| Writeln('1 – Chicken (10$)');
| |
| Writeln('2 – Fish (7$)');
| |
| Writeln('3 – Meat (8$)');
| |
| Writeln('4 – Salad (2$)');
| |
| Writeln('5 - Orange Juice (1$)');
| |
| Writeln('6 – Milk (1$)');
| |
| Writeln;
| |
| Write('Please enter your selection: ');
| |
| Readln(Meal);
| |
|
| |
| case Meal of
| |
| 1: Writeln('You have ordered Chicken,',
| |
| ' this will take 15 minutes');
| |
| 2: Writeln('You have ordered Fish, this will take 12 minutes');
| |
| 3: Writeln('You have ordered meat, this will take 18 minutes');
| |
| 4: Writeln('You have ordered Salad, this will take 5 minutes');
| |
| 5: Writeln('You have ordered Orange juice,',
| |
| ' this will take 2 minutes');
| |
| 6: Writeln('You have ordered Milk, this will take 1 minute');
| |
| else
| |
| Writeln('Wrong entry');
| |
| end;
| |
|
| |
| Write('Press enter key to close');
| |
| Readln;
| |
| end.
| |
| </syntaxhighlight>
| |
|
| |
| if 조건문을 사용하여 같은 프로그램을 작성한다면, 더 복잡하게 될 것이고, 복사한 코드를 포함하게 될 것입니다.
| |
|
| |
|
| |
|
| |
| ===if 조건문을 사용한 레스토랑 프로그램===
| |
|
| |
| <syntaxhighlight lang="pascal">
| |
| var
| |
| Meal: Byte;
| |
| begin
| |
| Writeln('Welcome to Pascal Restaurant. Please select your order');
| |
| Writeln('1 – Chicken (10$)');
| |
| Writeln('2 – Fish (7$)');
| |
| Writeln('3 – Meat (8$)');
| |
| Writeln('4 – Salad (2$)');
| |
| Writeln('5 - Orange Juice (1$)');
| |
| Writeln('6 – Milk (1$)');
| |
| Writeln;
| |
| Write('Please enter your selection: ');
| |
| Readln(Meal);
| |
|
| |
| if Meal = 1 then
| |
| Writeln('You have ordered Chicken, this will take 15 minutes')
| |
| else
| |
| if Meal = 2 then
| |
| Writeln('You have ordered Fish, this will take 12 minutes')
| |
| else
| |
| if Meal = 3 then
| |
| Writeln('You have ordered meat, this will take 18 minutes')
| |
| else
| |
| if Meal = 4 then
| |
| Writeln('You have ordered Salad, this will take 5 minutes')
| |
| else
| |
| if Meal = 5 then
| |
| Writeln('You have ordered Orange juice,' ,
| |
| ' this will take 2 minutes')
| |
| else
| |
| if Meal = 6 then
| |
| Writeln('You have ordered Milk, this will take 1 minute')
| |
| else
| |
| Writeln('Wrong entry');
| |
| end;
| |
|
| |
| Write('Press enter key to close');
| |
| Readln;
| |
| end.
| |
| </syntaxhighlight>
| |
|
| |
| 다음 예제에서는, 프로그램이 학생들의 성적을 계산해서 A 그리고 B, C, D, E, F 등급으로 변환해줍니다.
| |
|
| |
|
| |
|
| |
| ===학생 등급 프로그램===
| |
|
| |
| <syntaxhighlight lang="pascal">
| |
| var
| |
| Mark: Integer;
| |
| begin
| |
| Write('Press enter student mark: ');
| |
| Readln(Mark);
| |
| Writeln;
| |
|
| |
| case Mark of
| |
| 0 .. 39: Writeln('Student grade is: F');
| |
| 40 .. 49: Writeln('Student grade is: E');
| |
| 50 .. 59: Writeln('Student grade is: D');
| |
| 60 .. 69: Writeln('Student grade is: C');
| |
| 70 .. 84: Writeln('Student grade is: B');
| |
| 85 .. 100: Writeln('Student grade is: A');
| |
| else
| |
| Writeln('Wrong mark');
| |
| end;
| |
|
| |
| Write('Press enter key to close');
| |
| Readln;
| |
| end.
| |
| </syntaxhighlight>
| |
|
| |
| 위의 예시에서 (0 .. 39)와 같은 범위를 사용했으며, ''Mark'' 값이 이 범위에 존재하면 ''True''를 되돌린다는 의미를 지닙니다.
| |
|
| |
| '''참고'''<br>
| |
| ''Case'' 구문은 ''정수형''들과 ''문자형''과 같은 서수형에만 동작하고, ''문자열''과 ''실수''와 같은 다른 형식에는 동작하지 않습니다.
| |
|
| |
|
| |
|
| |
| ===키보드 프로그램===
| |
|
| |
| 이 예제에서는 키보드로부터 문자를 받아서 프로그램이 키보드의 입력 받은 키에 대한 줄 번호를 알려줄 것입니다.
| |
|
| |
| <syntaxhighlight lang="pascal">
| |
| var
| |
| Key: Char;
| |
| begin
| |
| Write('Please enter any English letter: ');
| |
| Readln(Key);
| |
| Writeln;
| |
|
| |
| case Key of
| |
| 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p':
| |
| Writeln('This is in the second row in keyboard');
| |
| 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l':
| |
| Writeln('This is in the third row in keyboard');
| |
| 'z', 'x', 'c', 'v', 'b', 'n', 'm':
| |
| Writeln('This is in the fourth row in keyboard');
| |
| else
| |
| Writeln('Unknown letter');
| |
| end;
| |
|
| |
| Write('Press enter key to close');
| |
| Readln;
| |
| end.
| |
| </syntaxhighlight>
| |
|
| |
| 참고로, case 조건문에 값 들의 집합이라는 새로운 기술을 사용했습니다.
| |
| <syntaxhighlight lang="pascal">
| |
| 'z', 'x', 'c', 'v', 'b', 'n', 'm':
| |
| </syntaxhighlight>
| |
|
| |
| 이는 Key가 이 값들의 집합 ('''z''', '''x''', '''c''', '''v''', '''b''', '''n''', '''m''') 중 하나라면 case 분기 구문을 실행한다는 의미입니다.
| |
|
| |
| 또한 다음과 같이 범위와 값을 혼용할 수 있습니다.
| |
| <syntaxhighlight lang="pascal">
| |
| 'a'.. 'd', 'x', 'y', 'z':
| |
| </syntaxhighlight>
| |
|
| |
| 이는 값이 '''a'''와 '''d''' 사이에 있거나 '''x''', '''y''', '''z'''와 같을 경우 구문을 실행한다는 의미입니다.
| |
|
| |
|
| [[Category:StartprogrammingusingObjectPascal]] | | [[Category:StartprogrammingusingObjectPascal]] |
case .. of 구문
상태 분기에 대한 또 다른 방법이 있는데, 그것이 바로 case .. of 구문 입니다. case 순서 값에 따라 실행을 나눕니다. 식당 프로그램은 case of 구문의 사용을 나타낼 것입니다.