0% found this document useful (0 votes)
64 views37 pages

Looping Structure: Algorithms and Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views37 pages

Looping Structure: Algorithms and Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Adam Mukharil Bachtiar

English Class
Informatics Engineering 2011

Algorithms and Programming

Looping Structure
Steps of the Day

For Structure While Do Repeat Until


Structure Structure

Let’s Start
Why We Need Looping

Make a program to showing “I LOVE

ALGORITHM” on the screen as much as 1000


Structure?

times. WHAT WILL YOU DO?


What is Looping Structure

An Algorithm structure that allow us to REPEAT

some statements that fulfill LOOPING CONDITION.


Components in Looping

• Looping condition

• Body statement

• Initialization
Structure

• Termination
Types of Looping Structure

• FOR

• WHILE

• REPEAT
For Structure

Definition and Structures of For Structure


• For structure was used in looping that have

specified ending of repetition.

• Number of repetition have been known in


For Structure

the beginning.

• Can be in ASCENDING or DESCENDING way


Format of For Structure (Ascending)

Algorithm Notation:
for variable  start_value to end_value do
statement
endfor

Pascal Notation I:
for variable := start_value to end_value do
statement;
Format of For Structure (Ascending)

Pascal Notation II:


for variable := start_value to end_value do
begin
statement;
end;
Example of For in Ascending Way (Algorithm)

1 Algoritma Deret_Bilangan_Ganjil
2 {I.S: Diinputkan satu nilai akhir oleh user}
3 {F.S: Menampilkan jumlah deret ganjil}
4
5 Kamus:
6 x,akhir:integer
7 jumlah:integer
8
9 Algoritma:
10 input(akhir)
11 jumlah  0
12 for x  1 to akhir do
13 if x mod 2 = 1 then
14 jumlah  jumlah + x;
15 endfor
16 output(‘Jumlah deret ganjil dari 1 – ‘,akhir,’ = ‘,jumlah)
Example of For in Ascending Way (Pascal)

1 program Deret_Bilangan_Ganjil;
2 uses crt;
3
4 var
5 x,akhir:integer;
6 jumlah:integer;
7
8 begin
9 write('Masukan batas akhir angka : ');readln(akhir);
10 jumlah:=0;
11 for x:=1 to akhir do
12 begin
13 if x mod 2=1 then
14 jumlah:=jumlah+x;
15 end;
16 writeln('Jumlah Deret ganjil dari 1 - ',akhir,' = ',jumlah);
17 writeln();
18 write('Tekan sembarang tombol untuk menutup...');
19 readkey();
20 end.
Format of For Structure (Descending)

Algorithm Notation:
for variable  end_value downto start_value do
statement
endfor

Pascal Notation I:
for variable := end_value downto start_value do
statement;
Format of For Structure (Ascending)

Pascal Notation II:


for variable := end_value downto start_value do
begin
statement;
end;
Example of For in Descending Way (Algorithm)

1 Algoritma Deret_Faktorial
2 {I.S: Diinputkan satu nilai oleh user}
3 {F.S: Menampilkan faktorial dari bilangan tersebut}
4
5 Kamus:
6 i,nilai:integer
7 faktorial:integer
8
9 Algoritma:
10 input(nilai)
11 faktorial1
12 for i  nilai downto 1 do
13 faktorialfaktorial*i
14 endfor
15 output(nilai,’! = ‘,faktorial)
Example of For in Descending Way (Pascal)

1 program Deret_Faktorial;
2 uses crt;
3
4 var
5 i,nilai:integer;
6 faktorial:integer;
7
8 begin
9 write('Masukan nilai = ');readln(nilai);
10 faktorial:=1;
11 for i:=nilai downto 1 do
12 faktorial:=faktorial*i;
13 writeln(nilai,'! = ',faktorial);
14 writeln();
15 write('Tekan sembarang tombol untuk menutup...');
16 readkey();
17 end.
While Structure

Definition and Structures of For Structure


• While structure always be executed while its

condition value is true.


While Structure

If the condition value is false, it means stop

repetition.

• While structure have condition in the

beginning of structure.
Format of While Structure

Algorithm Notation:
while kondisi do
statement
endwhile

Pascal Notation I:
while kondisi do
statement;
Format of While Structure

Pascal Notation II:


while kondisi do
begin
statement;
end;
Example of While (Algorithm)

1 Algoritma Deret_Bilangan
2 {I.S: Diinputkan satu angka oleh user}
3 {F.S: Menampilkan jumlah deret dari 1 sampai angka}
4
5 Kamus:
6 i,deret:integer
7 angka:integer
8
9 Algoritma:
10 input(angka)
11 deret0
12 i1
13 while i<=angka do
14 deretderet+i
15 ii+1;
16 endwhile
17 output(‘Jumlah deret dari 1 – ‘,angka,’ = ‘,deret)
Example of While (Pascal)
1 program Deret_Angka;
2 uses crt;
3
4 var
5 i,deret:integer;
6 angka:integer;
7
8 begin
9 write('Masukan angka = ');readln(angka);
10 deret:=0;
11 i:=1;
12 while i<=angka do
13 begin
14 deret:=deret+i;
15 i:=i+1;
16 end;
17 writeln('Jumlah deret dari 1 - ',angka,' = ',deret);
18 writeln();
19 write('Tekan sembarang tombol untuk menutup...');
20 readkey();
21 end.
Repeat Structure

Definition and Structures of For Structure


• Repeat structure always be executed until its

condition value is true.


Repeat Structure

• If the condition value is true, it means stop

repetition.

• Repeat structure have condition in the end

of structure.
Format of While Structure

Algorithm Notation:
repeat
statement
until kondisi

Pascal Notation:
repeat
statement;
until kondisi;
Example of Repeat (Algorithm)

1 Algoritma Coba_Password
2 {I.S: Diinputkan password oleh user}
3 {F.S: Menampilkan pesan benar atau salah}
4
5 Kamus:
6 const
7 password=1234
8
9 pass,i,j:integer
10
11 Algoritma:
12 i1
13 j3
14 repeat
15 input(pass)
16 if pass=password then
17 output(‘Password anda benar!’);
18 else
19 ii+1
20 jj-1
21 output(‘Password salah (‘,j,’ kali lagi)!’)
22 endif
23 until (pass=password)or(i=4)
Example of Repeat (Pascal)
1 program Coba_Password;
2 uses crt;
3
4 const
5 password=1234;
6
7 var
8 pass,i,j:integer;
9
10 begin
11 i:=1;
12 j:=3;
13 repeat
14 write('Masukan password (',i,'): ');readln(pass);
15 if pass=password then
16 begin
17 writeln('Password anda benar!');
18 writeln();
19 writeln('Tekan sembarang tombol untuk menutup...');
21 readkey();
22 end
23 else
24 begin
25 i:=i+1;
26 j:=j-1;
27 writeln('Password salah (',j,' kali lagi)!');
28 readkey();
29 end;
30 clrscr();
31 until (pass=password)or(i=4);
32 end.
EXERCISE
Exercise 1

Make the algorithm to solve this problem below (Color of


stars will be different each row):

N=5

*
**
***
****
*****
Exercise 2

Make the algortihm to solve this problem below (Color of


stars will be different each row):

N=3

*
**
***
**
*
Exercise 3

Make algorithm to count:

s = 1 – 2/3 + 3/5 – 4/7+….


Exercise 4

Make algorithm to count the maximum value and


mean value from n students.
Contact Person:
Adam Mukharil Bachtiar
Informatics Engineering UNIKOM
Jalan Dipati Ukur Nomor. 112-114 Bandung 40132
Email: [email protected]
Blog: http://adfbipotter.wordpress.com

Copyright © Adam Mukharil Bachtiar 2011

You might also like