Saturday, January 12, 2013

BRANCHING  (2)
 
Switch (Multiple Branches)
The case is going on if stacked, can sometimes be boring in writing program, because it renders some commands nearly the same birthday – repeatedly. Such things will be more noticeable if the expression to be tested by an order if more and more. In cases like this, C++ provides a special peintah the switch. The command switch allows to perform a number of actions against a number of different possible values. The General form of the switch command is as follows


switch (ekspresi)
{
Case label : pernyataan1 ; break;
Case label2 : pernyataan2 ; break;
. . . .
Default : pernyataan lain:
}

 
 




This form of the command above to read:
JIKA ekspresi sama dengan label1,
MAKA kerjakan pernyataan1.
JIKA ekspresi sama dengan label2,
MAKA kerjakan pernyataan2.
…….
Dan seterusnya
JIKA ekspresi tidak sesuai dengan semua label,MAKA kerjakan prnyatan lain
So for example the expression equal to 1, then the label statement 1 will be executed until the break statement was found, then the program will jump goto the end structure of the switch.
The switch statement is its use limited, namely that the label can only be constants of type char or int.

III. DISCUSSION OF THE EXPERIMENT AND STEP
Branching swich
Program V.1    
Listing Program:


#include <iostream.h>
main() {
      char kategori;
      float diskon;
      cout << "kategori pelanggan(A/B/C/D/E)= ";

      cin >> kategori;
      switch (kategori) {
            case 'A' :
                  diskon=40;
            case 'B' :
                  diskon=25;
            case 'C' :
            case 'D' :
                  diskon =10;
            default:
                  diskon =0;
      }
      cout << "Diskon = "<< diskon <<  "%";
}
 
 










After Listing at compile and filling of categories A, B, C, D, or E the output is as follows:
#include <iostream.h>
main() {
      char kategori;
      float diskon;

      cout << "kategori pelanggan(A/B/C/D/E)= ";
      cin >> kategori;
      switch (kategori) {
            case 'A' :
                  diskon=40; break;
            case 'B' :
                  diskon=25; break;
            case 'C' :
            case 'D' :
                  diskon =10; break;
            default:
                  diskon =0;
      }
      cout << "Diskon = "<< diskon <<  "%";
}
 
The following program Listing after modification:









After Listing at compile and filling of categories A, B, C, D, or E the output is as follows:


#include <iostream.h>
int main() {
{
      int pilih;
      cout << "Pilihan = " << endl;
      cout << "1. Jakarta" << endl;
      cout << "2. Bandung" << endl;
      cout << "3. Surabaya" <<endl;
      cout << endl;
      cout << "Pilihan  ";endl;
      cin >> pilih;
      switch (pilih)
      {
            case 1:
                  cout << "Pilihan ke Jakarta"
                          << endl;
                  break;
            case 2:
                  cout<< "Pilihan ke Bandung"
                          << endl;
                  break;
            case 3:
                  cout << "Pilihan ke Surabaya"
                          << endl;
                  break;
            default :
                  cout << "Pilihan anda salah"
                          << endl;
      }
      return 0;
}
}
 
Program V-2















After Listing at compile and charging select 1,2,3, or 4 outputs are as follows:
Modification of the v-2 Program resulting in a display that has been specified as follows.



#include <iostream.h>
int main() {
{
int pilih;
cout << "Konsentrasi yang dipilih = " << endl;
cout << "1. Teknik Kendali" << endl;
cout << "2. Elektronika dan Telekomunikasi" << endl;
cout << "3. Teknik Komputer dan Teknologi Informasi" <<endl;
cout << endl;
cout << "Pilihan  ";endl;
cin >> pilih;
switch (pilih)
{
case 1:
cout << "Teknik Kendali"
<< endl;
break;
case 2:
cout<< "Elektronika dan Telekomunikasi"
<< endl;
break;
case 3:
cout << "Teknik Komputer dan Teknologi Informasi"
<< endl;
break;
default :
cout << "Pilihan anda salah"
<< endl;
}
return 0;
}
}
 
 
















                                                                                                                                                                   
            When on the display then compiles will produce that has been specified, as follows.
Lompatan : label and goto
Program V-3


#include <iostream.h>
     main ()
     {
           cout << "Ini langkah pertama" << endl;
           goto LABEL2;
     LABEL1:
           cout << "Ini langkah kedua" << endl;
           goto LABEL3;
     LABEL2:
           cout << "Ini langkah ketiga" <<endl;
           goto LABEL1;
     LABEL3:
           cout << "Ini langkah keempat" <<endl;
     }
 
 








Once on the run then it will displays as follows.
The above Program uses the function labels and goto. The goto function is the revelation that directs execution to a labeled statement (a statement which is prefaced by a label name and the trailing period – two (:)), and the label was declared a location will be sent.

       I.            The task
Menu: Temperature Conversion Program with switch
Flowchart Temperature conversion with a branching structure switch


#include <iostream.h>
#include <math.h>
main (){
      char pilihan;
      float suhu, hasil;
cout << " ==============KONVERSI SUHU============= " << endl ;
cout << "           BY. Washilul Mustofa © " <<endl;
cout << " " << endl;
cout << " Masukan Nilai Suhu yang akan di Konversi : ";
cin  >> suhu;
cout << "   " << endl;
cout << "     Pilih konversi berikut : " <<endl;

cout << "         1. Celcius ke Fahrenheit " <<endl;
cout << "         2. Fahrenheit ke Celcius " << endl;
cout << "         3. Celcius ke Reamur " << endl;
cout << "         4. Reamur ke Celcius " <<endl;
cout << "         5. Fahrenheit ke Reamur " <<endl;
cout << "         6. Reamur ke Fahrenheit " <<endl;
cout << " " <<endl ;
cout<< "     Masukan konversi pilihan anda : " ; endl;
cin >> pilihan;
switch(pilihan) {
      case '1':
        hasil=  9.00/5.00*suhu + 32; break;
      case '2':
        hasil=  (suhu-32)/1.80; break;
      case '3':
        hasil=  suhu*0.8; break;
      case '4':
            hasil= suhu*1.25; break;
      case '5':
            hasil= (2.25/suhu)-32;break;
      default :
            hasil= (suhu*2.25)+32;
      }
      cout << "  " << endl;
      cout << "         Hasil konversi = "  <<hasil;
      cout << " " <<endl;
      cout  << " " <<endl;
      cout << " " <<endl;
      cout <<"             #OkehSipYess";
}
 
Listing Temperature conversion with a branching structure switch



















Here are the steps to switch Temperature Conversion Program. After pengkompilan.
First, after pengkompilan it will be shown the program. As the following picture.


Then Input the value of the Temperature will be in the conversion, example 1 Celsius will in conversion to Fahrenheit. Then his pace as follows.
And the last step the input conversion of your choice, fill in example 1
then it will appear as a result of the temperature conversion.

II.               Conclude
a) Switches used to run one of the statements of some of the possible statements, based on value and an expression and the value of selectors.
b) for example the expression equal to label1, then pernyatan1 will be executed until the break statement was found, then the program will jump goto the end structure of the switch.
c) presence of a break at each case will cause out of the switch, the function goto was the revelation that directs execution to a labeled statement and the label are declared to be intended location.
d) limited use of switch Statement, namely that the label can only be constants brtipe char or int.


III.            Referensi

a)      Firdausy, Kartika,S.T.,M.T, 2010, “Petunjuk Prakttikum Dasar Pemrogaman”, Teknik Elektro Universitas Ahmad Dahlan, Yogyakarta




Author: Unknown