Tuesday 18 May 2010

While Statement in Delphi ( Delphi Tutorial # 13 )

The syntax of while statement is :

  while boolean expression do

    begin

      first statement

      .

      .

      last statement

    end;

If the boolean expression is true, the statements will be executed until the boolean expression is false.

Example 1 ( writing numbers from 1 to 10 ) :

program Project1;

{$APPTYPE CONSOLE}

uses

  SysUtils;

var

 number : integer;

begin

 number := 1;

 while number  <  11 do

  begin

   writeln(number);

   number := number + 1;

  end;

 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;

 while number   <  101  

  begin

   sum := sum + number;

   number := number + 1;

  end;

 writeln(sum);

 readln;

end.


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

program Project1;

{$APPTYPE CONSOLE}

uses

  SysUtils;

var

 number, sum : integer;

begin

 number := 1;

 sum := 0;

 while number   <  101

  begin

   if Odd(number) then sum := sum + number;

   number := number + 1;

  end;

 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;

 while number   <  101

  begin

   if not Odd(number) then sum := sum + number;

   number := number + 1;

  end;

 writeln(sum);

 readln;

end.


You can download the complete code here.

Go to Previous Tutorial or Next Tutorial

3 comments:

  1. selalu hadir untuk menyimak informasi terbaru

    moga selalu sukses kawan

    ReplyDelete
  2. waduhh
    kalo kayak ginian kayakna ane dah pasrah dehh

    ReplyDelete
    Replies
    1. s The best and a lot of secret drop-shipping suppliers in the world which will provide your products globally available for you directly to your customers doors. Also get the best drop-shipping manufacturer on your organization.

      Delete

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.