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
|
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:
|
After Listing at compile and
filling of categories A, B, C, D, or E the output is as follows:


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




|
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.
|
When on the display then compiles will produce that has been
specified, as follows.

Lompatan : label and goto
Program V-3
|
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

|
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
