ICT
the blog for ICT
ken013194
暱稱: ホロ
性別: 男
國家: 香港
地區: 葵青區
« July 2026 »
SMTWTFS
1234
567891011
12131415161718
19202122232425
262728293031
最新文章
Text File
Arrays
String Procedures
String Functions
String Operation
文章分類
全部 (23)
Programming Language (23)
訪客留言
最近三個月尚無任何留言
每月文章
日誌訂閱
尚未訂閱任何日誌
好友名單
尚無任何好友
網站連結
尚無任何連結
最近訪客
最近沒有訪客
日誌統計
文章總數: 23
留言總數: 0
今日人氣: 0
累積人氣: 67
站內搜尋
RSS 訂閱
RSS Feed
2009 年 9 月 11 日  星期五   晴天


Selection Structure 分類: Programming Language

High-level Programming Language (Pascal)

Selection Structure

  • makes possible the selection of one of a number of alternative actions, depending on the value of a Boolean expression
  • can be implemented in Pascal using a If statement or a Case statement

    If statement

    • can be used to implement decisions involving two alternatives
    • can also be used to implement decision involving several alternatives
    • the syntax of the if-then statement (the selection statement with two alternatives)

         if <Boolean expression> then
          <statement> ;

      • a sequence of statement is executed or by-passed depending on whether a given Boolean expression is true or false
      • if the Boolean expression is true, the specified statement is executed; otherwise it is bypassed
      • in either case, execution continues with the next statement in the program
      • for example,

           if Score <= 50 then
            writeln ('Failed');
           <statement_A>;

      • 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)

    • the syntax of the if-then-else statement

         if <Boolean expression> then
          <statement_1>
         else
          <statement_2>;

      • if the Boolean expression is true, <statement_1> is executed and <statement_2> is bypassed; otherwise <statement_1> is bypassed and <statement_2> is executed
      • in either case, execution continues with the next statement in the program
      • for example,

         if Score <= 50 then
          writeln ('Failed')
         else

          writeln ('Passed');
         <statement_A>;

        • the Boolean expression Score <= 50 is evaluated, and if it is true, the word Failed is displayed and the else clause writeln ('Passed') is bypassed. Otherwise, the writeln ('Failed') is bypassed and writeln ('Passed') is executed. In either case, execution continues with the statement following this if statement (i.e. statement_A)

    • the statement that appears in an if statement may be any statement (e.g. compound statement, if statement, case statement, repetition statements)

      Nested if statement

    • is an if statement whose then clause or else clause are also if statements
    • for example, the following program segment tests whether the number Y is positive, negative or zero

      if Y > 0 then
       writeln ('Positive')
      else
       if Y < 0 then
        writeln ('Negative')
       else
        writeln ('Zero');

    Case statement

    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.

    • can be used to implement decisions involving multiple alternatives
    • the syntax of the case statement

       case <selector> of
        <case_label_list_1> : <statement_1>;
        <case_label_list_2> : <statement_2>;
             :          :
        <case_label_list_N> : <statement_N>
       end;

    • the selector must be an ordinal data type
      • has a finite set of value that can be all listed (e.g. integer, Boolean, char, etc.)
      • real and string are not ordinal data type because they cannot be all listed
      • if the alternatives are chosen according to the values of a real or string variable, nested if statement should be used instead

    • the case label list consists of any number of constants or subranges, separated by commas and followed by a colon; for example
       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

    • when the case statement is executed, the selector is evaluated

      • if this value is in the <case_label_list_i>, then <statement_i> is executed. After the execution, control will be passed to the statement following the reserved word end which marks the end of the case statement
      • if none of the case label lists contains the value of the selector, then either no statement is executed or, optionally, the statements following the reserved word else are executed, for example


       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

         
      • in standard Pascal failure to match a case label would cause a runtime error, therefore, it may be prudent to guard against exceptional values by preceding the case with an if statement to catch values not covered by the case





訪客留言 (返回 ken013194 的日誌)

訪客名稱:
電郵地址: (不會公開)
驗證碼:  按此更新驗證碼 (如看不清楚驗證碼請點擊圖片刷新)
俏俏話: (必需 登入 後才能使用此功能)
[ 開啟多功能編輯器 ]