Sunday 16 May 2010

Repeat Statement in Delphi ( Delphi Tutorial # 12 )

The syntax of repeat statement is :

  repeat

    first statement

    .

    .

    last statement

  until boolean expression

The first statement until the last statement will be executed once. If the boolean expression is false, all statements will be executed again until the boolean expression is true.

Example 1 ( writing numbers from 1 to 10 ) :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

var
  number : integer;
begin
  number := 1;
  repeat
   writeln(number);
   number := number + 1;
  until number = 11;
  readln;
end.

Example 2 ( adding numbers from 1 to 100 ) :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

var
  number, sum : integer;
begin
  number := 1;
  sum := 0;
  repeat
   sum := sum + number;
   number := number + 1;
  until number = 101;
  writeln(sum);
  readln;
end.

Example 3 ( adding odd numbers from 1 to 10 ) :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

var
  number, sum : integer;
begin
  number := 1;
  sum := 0;
  repeat
   if Odd(number) then sum := sum + number;
   number := number + 1;
  until number = 101;
  writeln(sum);
  readln;
end.

Example 4 ( adding even numbers from 1 to 100 ) :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

var
  number, sum : integer;
begin
  number := 1;
  sum := 0;
  repeat
   if not Odd(number) then sum := sum + number;
   number := number + 1;
  until number = 101;
  writeln(sum);
  readln;
end.

You can download the complete code here.

Go to Previous Tutorial or Next Tutorial

6 comments:

  1. ijin download script sobat untuk literasi catatan saya. nice article. programmer ya, salam kenal aja ^_^

    ReplyDelete
  2. Hello,Ari Christianto!

    I am grateful that you visited my blog.
    Welcome again.You have a nice blog here.
    All the best in your life^_^

    ReplyDelete
  3. perlu memahami lebih lanjut dulu nih, ga paham bahasa program :)

    ReplyDelete
  4. @Akhmad Syaiful : Ok, it's free to download.Thank you 4 visiting
    @Katariina : Hi, I'm grateful too, seeing you in this blog Katariina, thank you for dropping by. ^_^
    @richoyul : Ok... let's learn together, I suggest you follow this blog from the first tutorial. thank you

    ReplyDelete
  5. pakde coba pahami dulu kawan, thank atas sharingnya

    moga sukses selalu

    ReplyDelete
  6. @Pakde : thank you pakde for visiting and encouraging

    ReplyDelete

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.