Pascal guide - Goto: Label, Repeat...Until & While...do

Looping,and IF_then looping.
  1.simple looping by using if...then  statement:
    A.first we talk about declaraction LABEL and function of GOTO:
      about LABEL,label is used to show a address in a program,
      goto is used to go to a specifed label,
       the following program show they use:
      sample 1:
      program LABELshow;
      const
        A=1;
        B=2;
      label
        TheEnd;  {not just string,number can also be a "label"}
      begin
        IF A>B  then
         begin
           writeln('A is greater than B');
           goto TheEnd;
          end;
        if A=B then                              (in this program The goto part like a else function)
         begin
           writeln('A is equal to B');
           goto TheEnd;
          end;
        if A<B
          writeln('A is smaller than B');
       TheEnd :
     end.

    B.about if_then,looping by using Label and Goto together:
        first ,Looping means for doing a job more then or equal to one
        continuity under a condition:
        without looping we will done the following useless things:
        sample 2:
        program numbers;
        {this program are used to print number on screen from 1 to 100}
        var
          a:integer;
        begin
          a:=1;
          write( a, ' , ');
          a:=a+1;
          write(a, ',');
          a:=a+2;
          write(a, ',');
          ........
          .......             {until a = 100 by human count!}
         end.
        This is a completely wrong program!! I waste a lot of time of COPY nad PASTE!!
         following is shot program with If_then looping ,let's compare with about program,
        sample 3;
        program sample2mod;
        label start;
        var
         a:integer;
        begin
          start:
           a:=a+1;
           write(a,' , ');
           if  a<=99 then goto start:
         end.
        This program are more shorter than the sample2!!.

  2.looping by  repeat_until loop:
     that is more easy to understand of repeat_until function,
     by compare to sample3.
     let's we modifiy the sample3 from if_then loop to repeat_until loop:
     sample 4:
     program REPEAT;   {start label removed}
     var
      a:integer;
     begin
       repeat  {start label replaced by this}
        a:=a+1;
        write(a,' , ');
       until (a>99);     {if then replaced,OR/And condition can be used in this loop)
     end.

  3.looping by while_do loop
     while_do loop,this loop like the following if_then loop which content like sample 3:
     sample 5:
     label
      start,Theend;
     var
      a:integer;
     begin
       start:
       if a>100 then goto Theend;
       a:=a+1;
       write(a,' ,');
       goto start;
       Theend:
      end.
     Then,let's we modify this loop to while_do loop:
     sample 6:
     var    {label removed}
       a:integer;
     begin
       while (a<=99) do     {start and label replaced by  while (condition) do }
        begin                     {begin...end required for the loop}
          a:=a+1;
          write(a,' ,');
        end;
     end.

  4.compare of repeat_until and while_do loop:
     that's easy,just refer to the sample 5 and 3;
     in sample 3 ,the content in loop must be one First before condition checking,
     but in sample 5,the condition checking is done before running the content!!
     that's means the content in repeat_until loop must be run first but
while_do
     loop are not required.

  5.for_to_do
    for_to_do is more easy,this haven't any True or False "OR"/"AND" requirement,
   that's are only require a number,but it is most useful in filehandling of searching ,sorting..,
   here is a sample of that:(compare with sample 2 are most easy to understand)
   sample 7:
   var
    a:integer;
   begin
     for a:=1 to 100 do   {begin and end also required}
      begin
       write(a ,' ,');         {if a:=a+1; is added,the program will Jump and Jump}
      end;                     {become show even or odd number only!!}
   end.

<< Previous || Next >>