1

下面是一段代码,对循环的实现有疑问

               C := character'last; --'// SO code colorizer hack
               I := 1;
               K : loop
                  Done := C = character'first; --'
                  Count2 := I;
                  Exit K when Done;
                  C := character'pred(c);  --'
                  I := I + 1;
               end loop K;

谁能告诉我'K'代表什么。我猜它不是一个变量。'K'如何控制循环的执行?

4

4 回答 4

4

K是循环的名称。end loopand语句引用该Exit名称,以明确退出哪个循环。

Ada 参考手册称其为“loop_statement_identifier” 。

于 2010-04-23T04:52:50.537 回答
2

如前所述,K 是循环的标签。它允许您识别特定循环以提高可读性,并且还可以从一组嵌套的封闭循环中选择性地退出特定循环(即成为“goto”......嘘!:-)

这是一个人为的示例(未编译检查):

   S : Unbounded_String;
   F : File_Type;
   Done_With_Line : Boolean := False;
   All_Done       : Boolean := False;
begin
    Open(F, In_File, "data_file.dat");
  File_Processor:
    while not End_Of_File(F) loop
        S := Get_Line(F);
       Data_Processor:
        for I in 1 .. Length(S) loop
           Process_A_Character
                (Data_Char => Element(S, I),   -- Mode in
                 Line_Done => Done_With_Line,  -- Mode out
                 Finished  => All_Done);       -- Mode out

           -- If completely done, leave the outermost (file processing) loop
           exit File_Processor when All_Done;

           -- If just done with this line of data, go on to the next one.
           exit Data_Processor when Done_With_Line;
        end loop;
    end loop File_Processor;
    Close(F);
 end;
于 2010-04-23T13:03:38.673 回答
2

K is essentially the name of the loop. The exit k tells the code to stop looping and go to the next statement after loop k ends.

You usually don't need to name loops, as you can just say exit and it will exit the enclosing loop. However, if you have a loop nested inside another loop, and you'd like to exit not the one immediately around the exit statement, but the outermost one, then doing something like this may be required.

于 2010-04-23T17:40:20.867 回答
1

K 是一个命名循环的标签。哇,好久没看到阿达了……

于 2010-04-23T04:54:03.890 回答