1. Nama : Welman Bramco Munthe
NPM : 15 111 220
Kelas : TIF 15 A Reguler Malam
Algoritma dan Pemrograman I – Looping
1. While - Do Result :
program whileLoop;
var
a: integer;
begin
a := 10;
while a < 20 do
begin
writeln('value of a: ', a);
a := a + 1;
end;
end.
When the above code is compiled and
executed, what is the result? And draw the
flowchart!
Flowchart :
START
Var a : int
a := 10
a < 20 ?
Print
Value of a :
a := a + 1
Yes
No
END
2. 2. For – Do Result :
program forLoop;
var
a: integer;
begin
for a := 10 to 20 do
begin
writeln('value of a: ', a);
end;
end.
When the above code is compiled and
executed, what is the result? And draw
the flowchart!
START
Var a : int
a := 10
a <= 20 ?
Print
Value of a :
a := a + 1
Yes
END
No
3. 3. Repeat Until Result :
program repeatUntilLoop;
var
a: integer;
begin
a := 10;
(* repeat until loop execution *)
repeat
writeln('value of a: ', a);
a := a + 1
until a = 20;
end.
When the above code is compiled and
executed, what is the result? And draw the
flowchart!
Flowchart :
START
a := 10
a := a + 1
a <20 ?
Print
Value of a :
Yes
No
END
Var a : int
4. 4. NestedPrime Result :
program nestedPrime;
var
i, j:integer;
begin
for i := 2 to 50 do
begin
for j := 2 to i do
if (i mod j)=0 then
break; {* if factor found, not prime *}
if(j = i) then
writeln(i , ' is prime' );
end;
end.
When the above code is compiled and
executed, what is the result? And draw the
flowchart!
5. Flowchart :
Var i, j : int
START
i := 2
i <= 50 ?
j := 2
Yes
j < i ?
Yes
i mod j = 0 ?
Break;
True
j = i ?
False
Print
i ’ is prime’;
True
No
j = j + 1;
i = i + 1;
END
False
No
6. 5. Break Result:
program exBreak;
var
a: integer;
begin
a := 10;
(* while loop execution *)
while a < 20 do
begin
writeln('value of a: ', a);
a:=a +1;
if( a > 15) then
(* terminate the loop using break statement *)
break;
end;
end.
When the above code is compiled and executed, what is the result? And draw the flowchart!
Flowchart :
START
Var a : int
a := 10
a < 20 ?
Print
Value of a :
a := a + 1
a > 15 ?
Yes
Yes
Break;
No
No
END
7. 6. Continue Result :
program exContinue;
var
a: integer;
begin
a := 10;
(* repeat until loop execution *)
repeat
if( a = 15) then
begin
(* skip the iteration *)
a := a + 1;
continue;
end;
writeln('value of a: ', a);
a := a+1;
until ( a = 20 );
end.
When the above code is compiled and executed, what is the result? And draw the flowchart!
Flowchart :
Var a : int
a := 10
START
a = 20 ?
a := a + 1
Continue;
No
Print
Value of a :
Yes
a = 15 ?
END
Yes
No
8. 7. Goto Result:
program exGoto;
label 1;
var
a : integer;
begin
a := 10;
(* repeat until loop execution *)
1: repeat
if( a = 15) then
begin
(* skip the iteration *)
a := a + 1;
goto 1;
end;
writeln('value of a: ', a);
a:= a +1;
until a = 20;
end.
When the above code is compiled and executed, what is the result? And draw the flowchart!
Flowchart :
Var a : int
a := 10
a = 20 ?
a := a + 1
Goto 1;
No
Print
Value of a :
Yes
a = 15 ?
END
Yes
No
START
Label 1