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
累積人氣: 72
站內搜尋
RSS 訂閱
RSS Feed
2009 年 9 月 11 日  星期五   晴天


Repetition Structure 分類: Programming Language

High-level Programming Language (Pascal)

Repetition Structure

  • makes possible repeated execution of one or more statements until a specified condition is satisfied
  • a repetitive technique called looping is used in a repetition structure
  • can be implemented in Pascal using a While statement, a Repeat statement or a For statement

    Conditional Loops (While-do Loop and Repeat-until Loop)

    • it is used when the number of repetition is NOT known in advance

    While-do Loop

      • the syntax of the while statement


       while <Boolean expression> do
         <statement> ;

      • is a pretest loop in which the Boolean expression that controls repetition is evaluated before the body of the loop is executed
      • the loop body will be repeated as long as the loop control condition is tested to be true
      • the loop body may never be executed if the loop control condition is tested to be false
      • an infinite loop (also called endless loop) is a loop that consists of no exit condition. As a result, statements in the loop body will executed continuously or endlessly
      • a zero-repetition loop is just the opposite of the infinite loop. The loop body will not be executed because the loop-control condition is evaluated to be false

      • common usage of while-do loop

        • Counting

        can be done by using a while loop in this way :
        (1) before the loop, initialize Counter to 0 (i.e. Counter := 0), and
        (2) inside the loop, increase Counter by 1 (i.e. Counter := Counter + 1)

        • Summation

        can be done by using a while loop in this way :
        (1) before the loop, initialize Sum to 0 (i.e. Sum := 0), and
        (2) inside the loop, add the number to Sum (i.e. Sum := Sum + Num)

        • Finding the maximum of a set of numbers

        can be done by using a while loop in this way :

        1. before the loop, initialize Max to a number which is smaller than all numbers in the set (e.g. -9999), and
        2. inside the loop: if Max < Num then Max := Num

        • Finding the minimum of a set of numbers

        can be done by using a while loop in this way :

        1. before the loop, initialize Min to a number which is larger than all numbers in the set (e.g. 9999), and
        2. inside the loop: if Min > Num then Min := Num

        Repeat-until Loop

      • the syntax of the repeat statement


       repeat
        <statement>
       until <Boolean expression> ;

      • is a posttest loop in which the Boolean expression that controls repetition is evaluated after the body of the loop is executed
      • the loop body will be repeated as long as the loop control condition is tested to be false
      • the repetition will be terminated when the loop control condition is tested to be true
      • the loop will be executed at least once

      • common usage of repeat-until loop

        • Data Validation

        validate data entries and prevent unfavorable outcomes, for example

         write ('Enter a positive number : ');
         repeat
          readln (Num);
          if Num <= 0 then
          writeln ('Invalid ! Please enter a positive number : ')
         until Num > 0 ;

        • Display Menu

        offer a list of choices, for example

         repeat
          writeln ('1. Add Record');
          writeln ('2. Modify Record');

          writeln ('3. Delete Record');
          writeln ('0. Exit');
          write ('Please enter your choice (0 - 3) : ');
          readln (Choice)
         until Choice = 0

        • Continuing a Program

        continue in running a program, for example

         repeat
          { program content expected to be executed }
          write ('Do you want to continue (Y/N) ? ');
          readln (Choice)
         until (Choice = 'N') or (Choice = 'n')

        • Counting

        can be done by using a repeat loop in this way :

        1. before the loop, initialize Counter to 0, and
        2. inside the loop, increase Counter by 1

        • Summation

        can be done by using a repeat loop in this way :

        1. before the loop, initialize Sum to 0, and
        2. inside the loop, add the number to Sum

        • Finding the maximum of a set of numbers

        can be done by using a repeat loop in this way :

        1. before the loop, initialize Max to a number which is smaller than all numbers in the set (e.g. -9999), and
        2. inside the loop: if Max < Num then Max := Num

           
        • Finding the minimum of a set of numbers
          can be done by using a repeat loop in this way :

          1. before the loop, initialize Min to a number which is larger than all numbers in the set (e.g. 9999), and
          2. inside the loop: if Min > Num then Min := Num

    Count-controlled Loops (For-to-do Loop and For-downto-do Loop)

      • it is used when the number of repetition is known in advance
      • the loop body will be repeated a specified number of times
      • the syntax of the for statement (for-to-do loop and for-downto-do loop)

        for control_variable := initial_value to final_value do
         <statement> ;

        or

        for control_variable := initial_value downto final_value do
         <statement> ;
         

        • in a for-to loop, no iteration is performed if initial_value > final_value
        • similarly,in a for-downto loop, no iteration is done if initial_value < final_value
        • the control_variable is assigned the initial_value when a for statement is executed
        • the control_variable is automatically updated by the for statement itself
        • the statement(s) within a for statement may use the value of the control_variable, but it should not modify this value
        • the control variable in a for statement must be declared within the program in which it is used
        • the type of control_variable must be ordinal data type (e.g. integer, boolean, char)
        • the initial and final values of the control variable are determined before repetitions begins and cannot be changed during execution of the for statement
        • the initial_value and the final_value must be compatible in type with the control_variable

         

      • for example

        to produce the output 1234554321
        the following statements may be used :
         var i := integer;
            …

         for i := 1 to 5 do
          write (i);
         for i := 5 downto 1 do
          write (i);


    Sentinel-controlled Loop

    • sentinel value is an assigned value that can stop the loop repetition
       
    • whenever this value is entered, the looping will stop immediately
    • sentinel value is particularly useful when the number of loop execution is not known in advance
    • when the number of data to be inputted is unknown in advance, we can use an unusual datum to indicate the end of data. The is known as the end-of-data indicator technique and this unusual datum is known as sentinel value

Nested Loops

The loop body of a loop may contain any legal statements. In particular, it is possible to place while-do loop, repeat-until loop or for loop in the loop body of another loop. The resulting control structure is a nesting of loops. Nested loops consist of an outer loop with one or inner loops. Each time the outer loop is repeated, the inner loops are reentered, their exit conditions are reevaluated and all required iterations are performed.






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

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