Looping Structure: Algorithms and Programming
Looping Structure: Algorithms and Programming
English Class
Informatics Engineering 2011
Looping Structure
Steps of the Day
Let’s Start
Why We Need Looping
• Looping condition
• Body statement
• Initialization
Structure
• Termination
Types of Looping Structure
• FOR
• WHILE
• REPEAT
For Structure
the beginning.
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)
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)
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 faktorial1
12 for i nilai downto 1 do
13 faktorialfaktorial*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
•
While Structure
repetition.
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
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 deret0
12 i1
13 while i<=angka do
14 deretderet+i
15 ii+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
repetition.
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 i1
13 j3
14 repeat
15 input(pass)
16 if pass=password then
17 output(‘Password anda benar!’);
18 else
19 ii+1
20 jj-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
N=5
*
**
***
****
*****
Exercise 2
N=3
*
**
***
**
*
Exercise 3