15
8/8/2019 05 Selection Conditional http://slidepdf.com/reader/full/05-selection-conditional 1/15 SELECTION CONDITIONAL Oleh: M. Ade Erik (19870911 201001 1 006) Doc Versi. 05.00/Pas/OSN/TIK/2010 SMA Negeri 1 Tegal

05 Selection Conditional

Embed Size (px)

Citation preview

Page 1: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 1/15

SELECTION

CONDITIONALOleh: M. Ade Erik (19870911 201001 1 006)

Doc Versi. 05.00/Pas/OSN/TIK/2010

SMA Negeri 1 Tegal

Page 2: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 2/15

Pembahasan:

IF

Satu Statement

Banyak Statement If ± Then ± Else

Nested If 

CASE

Satu Statement

Banyak Statement

Case ± Else

Page 3: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 3/15

IF Satu Statement

Pernyataan IF biasanya diikuti dengan pernyataan ELSE,

yang akan dikerjakan bila kondisi pernyataan IF

menghasilkan kondisi FALSE

Syntax: If <ekpresi boolean> then

Begin <pernyataan>

End;

Page 4: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 4/15

IFBanyak Statement

If <ekpresi boolean> then

begin <pernyataan> end

Else if <ekpresi boolean> then

begin <pernyataan> end;

Page 5: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 5/15

Nested if (If Bersarang)

If <ekpresi boolean> then

Begin

If <ekpresi boolean> then

Begin

end

end;

Page 6: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 6/15

Contoh Soal

Tentukan genap atau ganjil dari suatu bilangan.

Input: 4

Output: Bilangan tersebut adalah bilangan Genap

Page 7: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 7/15

Penjelasan:

Lakukan pengujian bilangan, jika n mod 2 = 1 then

tampilkan pesan = Bilangan tersebut adalah bilangan

ganjil, jika tidak n mod 2 <> 1 maka tampilkan pesanBilangan tersebut adalah bilangan genap.

Page 8: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 8/15

uses wincrt;

Var n:integer;

Begin write('n= ');readln(n);

if (n mod 2 = 1) then

begin

write('Bilangan tersebut adalah bilangan ganjil');

End else

begin

write('Bilangan tersebut adalah bilangan genap');

end;

end.

Page 9: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 9/15

Soal:

Konversi nilai. Jika 10>=n = µE¶; jika 10<n<=40 = µD¶; jika

40<n<=70 = µC¶; jika 70<n<=90 = µB¶ ; jika 90<n<=100 =

µA¶; Contoh

Input: n=60

Output: nilai=µC¶

Page 10: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 10/15

Penjelasan

Lakukan seleksi nilai,

if n<=10 then nilai:=µE¶ else

if (n>10) and (n<=40) then nilai:=µD¶ else

If (n>40) and (n<=70) then nilai:=µC¶ else

If (n>70) and (n<=90) then nilai:=µB¶ else

If (n>90) and (n<=100) then nilai:=µA¶

Page 11: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 11/15

uses wincrt;

Var n:integer; nilai:char;

begin

write('n= ');readln(n);

if n<=10 then begin nilai:='E' end else

if (n>10) and (n<=40) then begin nilai:='D' end else

If (n>40) and (n<=70) then begin nilai:='C' end else

If (n>70) and (n<=90) then begin nilai:='B' end else

If (n>90) and (n<=100) then begin nilai:='A'end;

write('Nilai= ',nilai);

end.

Page 12: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 12/15

CASE .. OF

Pernyataan CASE dipergunakan apabila kita menulis

program yang memerlukan banyak pernyataan

IF...THEN...ELSE, sehingga program akan tampak lebih

 jelas untuk dibaca

Syntax:

Case <variable> of 

<nilai variable> : <pernyataan>

<nilai variable> : <pernyataan> Else

<nilai variable> : <pernyataan>

End;

Page 13: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 13/15

Contoh if:

if operator = '*' then result := number1 * number2

else if operator = '/' then result := number1 / number2

else if operator = '+' then result := number1 + number2 else if operator 

= '-' b result := number1 - number2

else invalid_operator = 1;

Contoh case:

case operator of 

'*' : result:= number1 * number2; '/' : result:= number1 / number2;

'+' : result:= number1 + number2;

'-' : result:= number1 - number2;

else invalid_operator := 1 end;

Page 14: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 14/15

Soal:

Ubahlah script berikut dalam bentuk case of 

uses wincrt;

Var n:integer; nilai:char; begin

write('n= ');readln(n);

if n<=10 then begin nilai:='E' end else

if (n>10) and (n<=40) then begin nilai:='D' end else

If (n>40) and (n<=70) then begin nilai:='C' end else

If (n>70) and (n<=90) then begin nilai:='B' end else

If (n>90) and (n<=100) then begin nilai:='A'end;

write('Nilai= ',nilai);

end.

Page 15: 05 Selection Conditional

8/8/2019 05 Selection Conditional

http://slidepdf.com/reader/full/05-selection-conditional 15/15

Jawab:

uses wincrt;

Var n:integer; nilai:char;

begin

write('n= ');readln(n);

case n of 

0..10:nilai:='E';

10..40:nilai:='D';

40..70:nilai:='C'; 70..90:nilai:='B';

90..100:nilai:='A';

End; write('Nilai= ',nilai);end.