KEMBAR78
Algorithm and Programming (Looping Structure) | PDF
Adam Mukharil Bachtiar
English Class
Informatics Engineering 2011
Algorithms and Programming
Looping Structure
Steps of the Day
Let’s Start
For Structure While Do
Structure
Repeat Until
Structure
WhyWeNeedLooping
Structure?
Make a program to showing “I LOVE
ALGORITHM” on the screen as much as 1000
times. WHAT WILL YOU DO?
WhatisLoopingStructure
An Algorithm structure that allow us to REPEAT
some statements that fulfill LOOPING CONDITION.
ComponentsinLooping
Structure
• Looping condition
• Body statement
• Initialization
• Termination
TypesofLoopingStructure
• FOR
• WHILE
• REPEAT
For Structure
Definition and Structures of For Structure
ForStructure
• For structure was used in looping that have
specified ending of repetition.
• Number of repetition have been known in
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Algoritma Deret_Bilangan_Ganjil
{I.S: Diinputkan satu nilai akhir oleh user}
{F.S: Menampilkan jumlah deret ganjil}
Kamus:
x,akhir:integer
jumlah:integer
Algoritma:
input(akhir)
jumlah  0
for x  1 to akhir do
if x mod 2 = 1 then
jumlah  jumlah + x;
endfor
output(‘Jumlah deret ganjil dari 1 – ‘,akhir,’ = ‘,jumlah)
Example of For in Ascending Way (Pascal)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
program Deret_Bilangan_Ganjil;
uses crt;
var
x,akhir:integer;
jumlah:integer;
begin
write('Masukan batas akhir angka : ');readln(akhir);
jumlah:=0;
for x:=1 to akhir do
begin
if x mod 2=1 then
jumlah:=jumlah+x;
end;
writeln('Jumlah Deret ganjil dari 1 - ',akhir,' = ',jumlah);
writeln();
write('Tekan sembarang tombol untuk menutup...');
readkey();
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Algoritma Deret_Faktorial
{I.S: Diinputkan satu nilai oleh user}
{F.S: Menampilkan faktorial dari bilangan tersebut}
Kamus:
i,nilai:integer
faktorial:integer
Algoritma:
input(nilai)
faktorial1
for i  nilai downto 1 do
faktorialfaktorial*i
endfor
output(nilai,’! = ‘,faktorial)
Example of For in Descending Way (Pascal)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
program Deret_Faktorial;
uses crt;
var
i,nilai:integer;
faktorial:integer;
begin
write('Masukan nilai = ');readln(nilai);
faktorial:=1;
for i:=nilai downto 1 do
faktorial:=faktorial*i;
writeln(nilai,'! = ',faktorial);
writeln();
write('Tekan sembarang tombol untuk menutup...');
readkey();
end.
While Structure
Definition and Structures of For Structure
WhileStructure
• While structure always be executed while its
condition value is true.
• 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Algoritma Deret_Bilangan
{I.S: Diinputkan satu angka oleh user}
{F.S: Menampilkan jumlah deret dari 1 sampai angka}
Kamus:
i,deret:integer
angka:integer
Algoritma:
input(angka)
deret0
i1
while i<=angka do
deretderet+i
ii+1;
endwhile
output(‘Jumlah deret dari 1 – ‘,angka,’ = ‘,deret)
Example of While (Pascal)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
program Deret_Angka;
uses crt;
var
i,deret:integer;
angka:integer;
begin
write('Masukan angka = ');readln(angka);
deret:=0;
i:=1;
while i<=angka do
begin
deret:=deret+i;
i:=i+1;
end;
writeln('Jumlah deret dari 1 - ',angka,' = ',deret);
writeln();
write('Tekan sembarang tombol untuk menutup...');
readkey();
end.
Repeat Structure
Definition and Structures of For Structure
RepeatStructure
• Repeat structure always be executed until its
condition value is true.
• 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Algoritma Coba_Password
{I.S: Diinputkan password oleh user}
{F.S: Menampilkan pesan benar atau salah}
Kamus:
const
password=1234
pass,i,j:integer
Algoritma:
i1
j3
repeat
input(pass)
if pass=password then
output(‘Password anda benar!’);
else
ii+1
jj-1
output(‘Password salah (‘,j,’ kali lagi)!’)
endif
until (pass=password)or(i=4)
Example of Repeat (Pascal)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
21
22
23
24
25
26
27
28
29
30
31
32
program Coba_Password;
uses crt;
const
password=1234;
var
pass,i,j:integer;
begin
i:=1;
j:=3;
repeat
write('Masukan password (',i,'): ');readln(pass);
if pass=password then
begin
writeln('Password anda benar!');
writeln();
writeln('Tekan sembarang tombol untuk menutup...');
readkey();
end
else
begin
i:=i+1;
j:=j-1;
writeln('Password salah (',j,' kali lagi)!');
readkey();
end;
clrscr();
until (pass=password)or(i=4);
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: adfbipotter@gmail.com
Blog: http://adfbipotter.wordpress.com
Copyright © Adam Mukharil Bachtiar 2011

Algorithm and Programming (Looping Structure)

  • 1.
    Adam Mukharil Bachtiar EnglishClass Informatics Engineering 2011 Algorithms and Programming Looping Structure
  • 2.
    Steps of theDay Let’s Start For Structure While Do Structure Repeat Until Structure
  • 3.
    WhyWeNeedLooping Structure? Make a programto showing “I LOVE ALGORITHM” on the screen as much as 1000 times. WHAT WILL YOU DO?
  • 4.
    WhatisLoopingStructure An Algorithm structurethat allow us to REPEAT some statements that fulfill LOOPING CONDITION.
  • 5.
    ComponentsinLooping Structure • Looping condition •Body statement • Initialization • Termination
  • 6.
  • 7.
    For Structure Definition andStructures of For Structure
  • 8.
    ForStructure • For structurewas used in looping that have specified ending of repetition. • Number of repetition have been known in the beginning. • Can be in ASCENDING or DESCENDING way
  • 9.
    Format of ForStructure (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;
  • 10.
    Format of ForStructure (Ascending) Pascal Notation II: for variable := start_value to end_value do begin statement; end;
  • 12.
    Example of Forin Ascending Way (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Algoritma Deret_Bilangan_Ganjil {I.S: Diinputkan satu nilai akhir oleh user} {F.S: Menampilkan jumlah deret ganjil} Kamus: x,akhir:integer jumlah:integer Algoritma: input(akhir) jumlah  0 for x  1 to akhir do if x mod 2 = 1 then jumlah  jumlah + x; endfor output(‘Jumlah deret ganjil dari 1 – ‘,akhir,’ = ‘,jumlah)
  • 13.
    Example of Forin Ascending Way (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 program Deret_Bilangan_Ganjil; uses crt; var x,akhir:integer; jumlah:integer; begin write('Masukan batas akhir angka : ');readln(akhir); jumlah:=0; for x:=1 to akhir do begin if x mod 2=1 then jumlah:=jumlah+x; end; writeln('Jumlah Deret ganjil dari 1 - ',akhir,' = ',jumlah); writeln(); write('Tekan sembarang tombol untuk menutup...'); readkey(); end.
  • 14.
    Format of ForStructure (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;
  • 15.
    Format of ForStructure (Ascending) Pascal Notation II: for variable := end_value downto start_value do begin statement; end;
  • 17.
    Example of Forin Descending Way (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Algoritma Deret_Faktorial {I.S: Diinputkan satu nilai oleh user} {F.S: Menampilkan faktorial dari bilangan tersebut} Kamus: i,nilai:integer faktorial:integer Algoritma: input(nilai) faktorial1 for i  nilai downto 1 do faktorialfaktorial*i endfor output(nilai,’! = ‘,faktorial)
  • 18.
    Example of Forin Descending Way (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 program Deret_Faktorial; uses crt; var i,nilai:integer; faktorial:integer; begin write('Masukan nilai = ');readln(nilai); faktorial:=1; for i:=nilai downto 1 do faktorial:=faktorial*i; writeln(nilai,'! = ',faktorial); writeln(); write('Tekan sembarang tombol untuk menutup...'); readkey(); end.
  • 19.
    While Structure Definition andStructures of For Structure
  • 20.
    WhileStructure • While structurealways be executed while its condition value is true. • If the condition value is false, it means stop repetition. • While structure have condition in the beginning of structure.
  • 21.
    Format of WhileStructure Algorithm Notation: while kondisi do statement endwhile Pascal Notation I: while kondisi do statement;
  • 22.
    Format of WhileStructure Pascal Notation II: while kondisi do begin statement; end;
  • 24.
    Example of While(Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Algoritma Deret_Bilangan {I.S: Diinputkan satu angka oleh user} {F.S: Menampilkan jumlah deret dari 1 sampai angka} Kamus: i,deret:integer angka:integer Algoritma: input(angka) deret0 i1 while i<=angka do deretderet+i ii+1; endwhile output(‘Jumlah deret dari 1 – ‘,angka,’ = ‘,deret)
  • 25.
    Example of While(Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 program Deret_Angka; uses crt; var i,deret:integer; angka:integer; begin write('Masukan angka = ');readln(angka); deret:=0; i:=1; while i<=angka do begin deret:=deret+i; i:=i+1; end; writeln('Jumlah deret dari 1 - ',angka,' = ',deret); writeln(); write('Tekan sembarang tombol untuk menutup...'); readkey(); end.
  • 26.
    Repeat Structure Definition andStructures of For Structure
  • 27.
    RepeatStructure • Repeat structurealways be executed until its condition value is true. • If the condition value is true, it means stop repetition. • Repeat structure have condition in the end of structure.
  • 28.
    Format of WhileStructure Algorithm Notation: repeat statement until kondisi Pascal Notation: repeat statement; until kondisi;
  • 30.
    Example of Repeat(Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Algoritma Coba_Password {I.S: Diinputkan password oleh user} {F.S: Menampilkan pesan benar atau salah} Kamus: const password=1234 pass,i,j:integer Algoritma: i1 j3 repeat input(pass) if pass=password then output(‘Password anda benar!’); else ii+1 jj-1 output(‘Password salah (‘,j,’ kali lagi)!’) endif until (pass=password)or(i=4)
  • 31.
    Example of Repeat(Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 27 28 29 30 31 32 program Coba_Password; uses crt; const password=1234; var pass,i,j:integer; begin i:=1; j:=3; repeat write('Masukan password (',i,'): ');readln(pass); if pass=password then begin writeln('Password anda benar!'); writeln(); writeln('Tekan sembarang tombol untuk menutup...'); readkey(); end else begin i:=i+1; j:=j-1; writeln('Password salah (',j,' kali lagi)!'); readkey(); end; clrscr(); until (pass=password)or(i=4); end.
  • 32.
  • 33.
    Exercise 1 Make thealgorithm to solve this problem below (Color of stars will be different each row): N=5 * * * * * * * * * * * * * * *
  • 34.
    Exercise 2 Make thealgortihm to solve this problem below (Color of stars will be different each row): N=3 * * * * * * * * *
  • 35.
    Exercise 3 Make algorithmto count: s = 1 – 2/3 + 3/5 – 4/7+….
  • 36.
    Exercise 4 Make algorithmto count the maximum value and mean value from n students.
  • 37.
    Contact Person: Adam MukharilBachtiar Informatics Engineering UNIKOM Jalan Dipati Ukur Nomor. 112-114 Bandung 40132 Email: adfbipotter@gmail.com Blog: http://adfbipotter.wordpress.com Copyright © Adam Mukharil Bachtiar 2011