Adam Mukharil Bachtiar English Class Informatics Engineering 2011 Algorithms and Programming Branching Structure
Steps of the Day Let’s Start Definition Types of Branching Case Structure
Definition Definition of Branching Structure
DefinitionofBranching Structure Algorithm structure which allow to execute the instruction if the condition of this instruction was met.
UsageofBranchingStructure • Make menu structure • Input validation • Error handling
Types of Branching Structure All about Branching Structure
TypesofBranchingStructure • One Case Branching • Two Cases Branching • Three/Many Cases Branching • Many Conditions Branching
One Case Branching Algorithm Notation: if condition then statement endif
One Case Branching Pascal Notation (if there’s only one statement): if condition then statement;
One Case Branching Pascal Notation (if there are many statement): if condition then begin statement 1; statement 2; end;
Example of One Case Branching (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 Algoritma Bilangan_Ganjil {I.S: Diinputkan satu bilangan oleh user} {F.S: Menampilkan statement apabila bilangannya ganjil} Kamus: bil:integer Algoritma: input(bil) if bil mod 2 = 1 then output(‘Bilangan ‘,bil,’ adalah bilangan ganjil’) endif
Example of One Case Branching (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 program Bilangan_Ganjil; uses crt; var bil:integer; begin write('Masukan sebuah bilangan bulat: '); readln(bil); if bil mod 2 = 1 then writeln('Bilangan ',bil,' adalah bilangan ganjil'); writeln(); writeln('Ketik sembarang tombol untuk menutup...'); readkey(); end.
Two Cases Branching Algorithm Notation: if condition then statement 1 else statement 2 endif
Two Cases Branching Pascal Notation (if there’s only one statement): if condition then statement 1 else statement 2;
Two Cases Branching Pascal Notation (if there are many statement): if condition then begin statement 1; statement 2; end else begin statement 3; statement 4; end;
Example of Two Cases Branching (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Algoritma Bilangan_Genap_Ganjil {I.S: Diinputkan satu bilangan oleh user} {F.S: Menampilkan statement bilangan ganjil atau genap} Kamus: bil:integer Algoritma: input(bil) if bil mod 2 = 1 then output(‘Bilangan ‘,bil,’ adalah bilangan ganjil’) else output(‘Bilangan ‘,bil,’ adalah bilangan genap’) endif
Example of Two Cases Branching (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 program Bilangan_Genap_ganjil; uses crt; var bil:integer; begin write('Masukkan sebuah bilangan bulat: '); readln(bil); if bil mod 2 = 1 then writeln('Bilangan ',bil,' adalah bilangan ganjil') else writeln('Bilangan ',bil,' adalah bilangan genap'); writeln(); writeln('Tekan sembarang tombol untuk menutup...'); readkey(); end.
Three/Many Cases Branching Algorithm Notation: if condition 1 then statement 1 else if condition 2 then statement 2 else if condition 3 then statement 3 else statement 4 endif endif endif
Three/Many Cases Branching Pascal Notation (if there’s only one statement): if condition 1 then statement 1 else if condition 2 then statement 2 else if condition 3 then statement 3 else statement 4;
Three/Many Cases Branching Pascal Notation (if there are many statement): if kondisi 1 then begin statement 1; end else if kondisi 2 then begin statement 2; end else if kondisi 3 then begin statement 3; end else begin statement 4; end;
Example of Three/Many Cases Branching (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Algoritma Lampu_Lalu_Lintas {I.S: Diinputkan satu warna lampu oleh user} {F.S: Menampilkan statement sesuai warna lampu} Kamus: warna:string Algoritma: input(warna) if warna = ‘MERAH’ then output(‘Berhenti!’) else if warna = ‘KUNING’ then output(‘Hati-Hati!’) else if warna = ‘HIJAU’ then output(‘Jalan!’) else output(‘Warna salah!’) endif endif endif
Example of Three/Many Cases Branching (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 program Lampu_Lalu_Lintas; uses crt; var warna:string; begin write('Masukkan sembarang warna: '); readln(warna); warna:=upcase(warna); {membuat uppercase} if warna='MERAH' then writeln('Berhenti!') else if warna='KUNING' then writeln('Hati-Hati!') else if warna='HIJAU' then writeln('Jalan!') else writeln('Warna salah!'); writeln(); writeln('Tekan sembarang tombol untuk menutup...'); readkey(); end.
Many Conditions Branching • There are several cases which was requested more than one conditions. • Problem solving:  Use AND : if all condition must be fulfilled  Use OR : if only one condition must be fulfilled.
Example of Many Conditions Branching (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Algoritma Huruf_Konsonan {I.S: Diinputkan satu huruf oleh user} {F.S: Menampilkan pesan huruf konsonan jika konsonan} Kamus: k:char Algoritma: input(k) if (k≠’a’)and(k≠’i’)and(k≠’u’)and(k≠’e’)and(k≠’o’) then output(‘Huruf ‘,k,’ adalah huruf konsonan’) else output(‘Huruf ‘,k,’ adalah huruf vokal’) endif
Example of Many Conditions Branching (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 program Huruf_Konsonan; uses crt; var k:char; begin write('Masukkan satu huruf: '); readln(k); k:=lowercase(k); if (k<>'a')and(k<>'i')and(k<>'u')and(k<>'e')and(k<>'o') then writeln('Huruf ',k,' adalah huruf konsonan') else writeln('Huruf ',k,' adalah huruf vokal'); writeln(); writeln('Tekan sembarang tombol untuk menutup...'); readkey(); end.
Case Structure All About Case Structure
Case Structure • Expression could be arithmetics or boolean. • Expression was produce a constant. • Value must be ordinal type (char, boolean, and integer) • Statement in otherwise will be executed if the other value aren’t fulfilled.
Case Structure Algorithm Notation: case ekspresi nilai 1 : statement 1 nilai 2 : statement 2 nilai 3 : statement 3 . . . nilai n : statement n otherwise : statement x endcase
Case Structure Pascal Notation: case ekspresi of nilai 1 : statement 1; nilai 2 : statement 2; nilai 3 : statement 3; . . . nilai n : statement n; else statement x; end;
Example of Case Structure (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Algoritma Ukuran_Baju {I.S: Diinputkan satu huruf untuk ukuran baju oleh user} {F.S: Menampilkan arti ukuran baju} Kamus: size:char Algoritma: input(size) case size ‘S’:output(‘Kecil’); ‘M’:output(‘Sedang’); ‘L’:output(‘Besar’); otherwise : output(‘Ukuran salah!’) endcase
Example of Case Structure (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 program Ukuran_Baju; uses crt; var size:char; begin write('Masukkan ukuran baju [S/M/L]: '); readln(size); size:=upcase(size); case size of 'S':writeln('Kecil'); 'M':writeln('Sedang'); 'L':writeln('Besar'); else writeln('Ukuran salah!'); end; writeln(); writeln('Tekan sembarang tombol untuk menutup...'); readkey(); end.
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 (Branching Structure)

  • 1.
    Adam Mukharil Bachtiar EnglishClass Informatics Engineering 2011 Algorithms and Programming Branching Structure
  • 2.
    Steps of theDay Let’s Start Definition Types of Branching Case Structure
  • 3.
  • 4.
    DefinitionofBranching Structure Algorithm structure whichallow to execute the instruction if the condition of this instruction was met.
  • 5.
    UsageofBranchingStructure • Make menustructure • Input validation • Error handling
  • 6.
    Types of Branching Structure Allabout Branching Structure
  • 7.
    TypesofBranchingStructure • One CaseBranching • Two Cases Branching • Three/Many Cases Branching • Many Conditions Branching
  • 8.
    One Case Branching AlgorithmNotation: if condition then statement endif
  • 9.
    One Case Branching PascalNotation (if there’s only one statement): if condition then statement;
  • 10.
    One Case Branching PascalNotation (if there are many statement): if condition then begin statement 1; statement 2; end;
  • 12.
    Example of OneCase Branching (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 Algoritma Bilangan_Ganjil {I.S: Diinputkan satu bilangan oleh user} {F.S: Menampilkan statement apabila bilangannya ganjil} Kamus: bil:integer Algoritma: input(bil) if bil mod 2 = 1 then output(‘Bilangan ‘,bil,’ adalah bilangan ganjil’) endif
  • 13.
    Example of OneCase Branching (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 program Bilangan_Ganjil; uses crt; var bil:integer; begin write('Masukan sebuah bilangan bulat: '); readln(bil); if bil mod 2 = 1 then writeln('Bilangan ',bil,' adalah bilangan ganjil'); writeln(); writeln('Ketik sembarang tombol untuk menutup...'); readkey(); end.
  • 14.
    Two Cases Branching AlgorithmNotation: if condition then statement 1 else statement 2 endif
  • 15.
    Two Cases Branching PascalNotation (if there’s only one statement): if condition then statement 1 else statement 2;
  • 16.
    Two Cases Branching PascalNotation (if there are many statement): if condition then begin statement 1; statement 2; end else begin statement 3; statement 4; end;
  • 18.
    Example of TwoCases Branching (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Algoritma Bilangan_Genap_Ganjil {I.S: Diinputkan satu bilangan oleh user} {F.S: Menampilkan statement bilangan ganjil atau genap} Kamus: bil:integer Algoritma: input(bil) if bil mod 2 = 1 then output(‘Bilangan ‘,bil,’ adalah bilangan ganjil’) else output(‘Bilangan ‘,bil,’ adalah bilangan genap’) endif
  • 19.
    Example of TwoCases Branching (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 program Bilangan_Genap_ganjil; uses crt; var bil:integer; begin write('Masukkan sebuah bilangan bulat: '); readln(bil); if bil mod 2 = 1 then writeln('Bilangan ',bil,' adalah bilangan ganjil') else writeln('Bilangan ',bil,' adalah bilangan genap'); writeln(); writeln('Tekan sembarang tombol untuk menutup...'); readkey(); end.
  • 20.
    Three/Many Cases Branching AlgorithmNotation: if condition 1 then statement 1 else if condition 2 then statement 2 else if condition 3 then statement 3 else statement 4 endif endif endif
  • 21.
    Three/Many Cases Branching PascalNotation (if there’s only one statement): if condition 1 then statement 1 else if condition 2 then statement 2 else if condition 3 then statement 3 else statement 4;
  • 22.
    Three/Many Cases Branching PascalNotation (if there are many statement): if kondisi 1 then begin statement 1; end else if kondisi 2 then begin statement 2; end else if kondisi 3 then begin statement 3; end else begin statement 4; end;
  • 24.
    Example of Three/ManyCases Branching (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Algoritma Lampu_Lalu_Lintas {I.S: Diinputkan satu warna lampu oleh user} {F.S: Menampilkan statement sesuai warna lampu} Kamus: warna:string Algoritma: input(warna) if warna = ‘MERAH’ then output(‘Berhenti!’) else if warna = ‘KUNING’ then output(‘Hati-Hati!’) else if warna = ‘HIJAU’ then output(‘Jalan!’) else output(‘Warna salah!’) endif endif endif
  • 25.
    Example of Three/ManyCases Branching (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 program Lampu_Lalu_Lintas; uses crt; var warna:string; begin write('Masukkan sembarang warna: '); readln(warna); warna:=upcase(warna); {membuat uppercase} if warna='MERAH' then writeln('Berhenti!') else if warna='KUNING' then writeln('Hati-Hati!') else if warna='HIJAU' then writeln('Jalan!') else writeln('Warna salah!'); writeln(); writeln('Tekan sembarang tombol untuk menutup...'); readkey(); end.
  • 26.
    Many Conditions Branching •There are several cases which was requested more than one conditions. • Problem solving:  Use AND : if all condition must be fulfilled  Use OR : if only one condition must be fulfilled.
  • 28.
    Example of ManyConditions Branching (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Algoritma Huruf_Konsonan {I.S: Diinputkan satu huruf oleh user} {F.S: Menampilkan pesan huruf konsonan jika konsonan} Kamus: k:char Algoritma: input(k) if (k≠’a’)and(k≠’i’)and(k≠’u’)and(k≠’e’)and(k≠’o’) then output(‘Huruf ‘,k,’ adalah huruf konsonan’) else output(‘Huruf ‘,k,’ adalah huruf vokal’) endif
  • 29.
    Example of ManyConditions Branching (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 program Huruf_Konsonan; uses crt; var k:char; begin write('Masukkan satu huruf: '); readln(k); k:=lowercase(k); if (k<>'a')and(k<>'i')and(k<>'u')and(k<>'e')and(k<>'o') then writeln('Huruf ',k,' adalah huruf konsonan') else writeln('Huruf ',k,' adalah huruf vokal'); writeln(); writeln('Tekan sembarang tombol untuk menutup...'); readkey(); end.
  • 30.
  • 31.
    Case Structure • Expressioncould be arithmetics or boolean. • Expression was produce a constant. • Value must be ordinal type (char, boolean, and integer) • Statement in otherwise will be executed if the other value aren’t fulfilled.
  • 32.
    Case Structure Algorithm Notation: caseekspresi nilai 1 : statement 1 nilai 2 : statement 2 nilai 3 : statement 3 . . . nilai n : statement n otherwise : statement x endcase
  • 33.
    Case Structure Pascal Notation: caseekspresi of nilai 1 : statement 1; nilai 2 : statement 2; nilai 3 : statement 3; . . . nilai n : statement n; else statement x; end;
  • 35.
    Example of CaseStructure (Algorithm) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Algoritma Ukuran_Baju {I.S: Diinputkan satu huruf untuk ukuran baju oleh user} {F.S: Menampilkan arti ukuran baju} Kamus: size:char Algoritma: input(size) case size ‘S’:output(‘Kecil’); ‘M’:output(‘Sedang’); ‘L’:output(‘Besar’); otherwise : output(‘Ukuran salah!’) endcase
  • 36.
    Example of CaseStructure (Pascal) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 program Ukuran_Baju; uses crt; var size:char; begin write('Masukkan ukuran baju [S/M/L]: '); readln(size); size:=upcase(size); case size of 'S':writeln('Kecil'); 'M':writeln('Sedang'); 'L':writeln('Besar'); else writeln('Ukuran salah!'); end; writeln(); writeln('Tekan sembarang tombol untuk menutup...'); readkey(); end.
  • 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