Program Lagu Anak Ayam (Looping)

Repeat-Until

Program LaguAnakAyam;

var

N : integer;

begin

write(‘Masukkan n = ‘);

readln(N);

if (N<=0) then

begin

write(‘Lagu tidak dapat dimainkan, silahkan masukkan nilai positif’);

readln;

end else if (N=1) then

begin

write(‘Anak ayam turunlah ‘,N);

readln;

write(‘Mati satu tinggal induknya’);

readln;

end else

begin

write(‘Anak ayam turunlah ‘,N);

readln;

repeat

write(‘Mati satu tinggallah ‘,N-1);

readln;

N := N – 1;

until (N<2);

write(‘Mati satu tinggal induknya’);

readln;

end;

end.

____________________________________________________________

While do

Program LaguAnakAyam2;

var

N : integer;

begin

write(‘Masukkan n = ‘);

readln(N);

if (N<=0) then

begin

write(‘Lagu tidak dapat dimainkan, silahkan masukkan nilai positif’);

readln;

end else

begin

write(‘Anak ayam turunlah ‘,N);

readln;

while (N>1) do

begin

write(‘Mati satu tinggallah ‘,N-1);

readln;

N := N-1;

end;

write(‘Mati satu tinggal induknya’);

readln;

end;

end.

____________________________________________________________

For

Program LaguAnakAyam3;

var

N : integer;

begin

write(‘Masukkan n:’);

readln(N);

if (N<=0) then

begin

write(‘Lagu tidak dapat dimainkan, silahkan memasukkan nilai positif’);

readln;

end else if (N=1) then

begin

write(‘Anak ayam turunlah ‘,N);

readln;

write(‘Mati satu tinggal induknya’);

readln;

end else

begin

write(‘Anak ayam turunlah ‘,N);

readln;

for N := N-1 downto 1 do

begin

write(‘Mati satu tinggallah ‘,N);

readln;

end;

write(‘Mati satu tinggal induknya’);

readln;

end;

end.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s