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.
selalu hadir untuk menyimak informasi terbaru
ReplyDeletemoga selalu sukses kawan
waduhh
ReplyDeletekalo kayak ginian kayakna ane dah pasrah dehh
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