High-level Programming Language (Pascal)
Selection Structure
- the Boolean expression Score <= 50 is evaluated, and if it is true, the word Failed is displayed. Otherwise, the writeln statement is bypassed. In either case, execution continues with the statement following this if statement (i.e. statement_A)
¡@if Score <= 50 then
¡@¡@writeln ('Failed')
¡@else
¡@¡@writeln ('Passed');
¡@<statement_A>;
Nested if statement
if Y > 0 then
¡@writeln ('Positive')
else
¡@if Y < 0 then
¡@¡@writeln ('Negative')
¡@else
¡@¡@writeln ('Zero');
When there are many alternatives in the selection process, it may be unwise to use the if statements. This is because the program would be lengthy. The case statement can be used to replace any if statement and the layout is more present looking.
¡@case refno of
¡@¡@0¡@¡@¡@ :¡@zerocount := zerocount + 1 ;
¡@¡@1, 2¡@¡@:
¡@¡@3 .. 6¡@ :¡@writeln ('The reference number is 3 to 6');
¡@¡@7¡@¡@¡@ :¡@begin
¡@¡@¡@¡@¡@¡@¡@¡@ writeln;
¡@¡@¡@¡@¡@¡@¡@¡@ writeln ('Processing finished')
¡@¡@¡@¡@¡@¡@¡@ end
¡@end;
- subrange is written as two constants separated by the subrange delimiter '..'
- same value must not appear more then once in the case label lists
¡@case operator of
¡@¡@'*'¡@: result := A * B;
¡@¡@'/'¡@: result := A / B;
¡@¡@'+' : result := A + B;
¡@¡@'-'¡@: result := A - B;
¡@¡@else invalid_operator := true
¡@end;
- using else part of the case statement is an extension to standard Pascal