BRANCHING
(1)
I. THEORETICAL FOUNDATIONS
In C++, a conditional structure supported by two
basic commands, i.e. if and switch. If the command form has several variations
as follows:
a. if the
first Form
Syntactic structure if the first has the
following form:
|
If
(kondisi)
{
Pernyataan
}
|
The first form is read:
Jika kondisi benar, maka kerjakan pernyataan
The first form is the form
of the command if the simplest. In this form, the statement will be executed if
the condition is true. The value of true in the language C++ stated in a value
of 1 (one), while the value of any declared with a value of 0 (zero).
b. the second form
if
|
If (kondisi)
{
Pernyataan;
}
Else
{
Pernyataan lain;
}
|
Jika kondisi
benar, maka kerjakan pernyataan
Jika tidak,
kerjakan pernyataan lain
In the
second form, if the condition is true, then working on the statement. But if
the condition is false, then the level of the other statements.
c. the form if a third
(stacked)
|
If
(kondis 1)
{
Pernyataan
1;
}
Else if
(kondis 2)
{
Pernyataan
2;
}
....
Else
{
Pernyataan
lain;
}
|
Third form if it read:
Jika kondisi 1 benar, maka
kerjakan pernyataan 1.
Jika tidak,
Jika kondisi 2 benar, maka
kerjakan pernyataan 2.
Dst...
Jika tidak, kerjakan
pernyataan lain.
This
third form is an extension of the second form of if statement in which else
there if more. This form is used to certify yangnlebih choice of the two. In
this form there is also some some of the conditions that will be tested by the
if command. If one of the conditions is true, then the pernyatan bersesuian
with the statement to be executed. If the condition does not exist, then it
will be working on the other statements.
I.
STEP EXPERIMENT AND DISCUSSION
a single Branching
|
#include
<iostream.h>
main()
{ int umur;
cout << "Masukkan umurmu =
";
cin >> umur;
if umur >= 60;
cout<<"Halo
Mbah...!!" <<endl;
cout << "Jadi umurmu"
<< umur << "tahun";
}
|
After
compile
going on Error message that statement
missing on
if umur >=60;
After repairs at if age > = 60; be if (age > = 60) produce the
following values filled in several times with age.
|
cout <<
"Salam buat cucumu ya
" <<endl;
|
|
cout<<"Halo
Mbah...!!" <<endl;
|
Always result in additional comments for age is not as desirable
as there are 2 different statement so that it can not only use one formatting.
|
#include
<iostream.h>
main()
{ int umur;
cout << "Masukkan umurmu =
";
cin >> umur;
if (umur < 60)
cout<<"Halo Mbah...!!"
<<endl;
if (umur >=60)
cout<< "Salam buat cucumu
ya " <<endl;
cout << "Jadi umurmu"
<< umur << "tahun";
}
|
|
#include
<iostream.h>
main()
{ int tahun, umur;
cout << "Masukkan
kelahiranmu = ";
cin >> tahun;
umur = 2009-tahun;
cout << "Unmurmu\t"
<< umur << "tahun\n";
if (umur <17)
cout<<"kamu belum
sweet seventeen\n";
cout << "belum cukup
umur\n";
}
|
At the time of mengcompile
the program error that is incompatible with the Comment that you want. Because
in the year stated on the program does not match the current year so that
error, and the on error statement anyway so need to fix such as follows.
|
#include
<iostream.h>
main()
{ int tahun, umur;
cout << "Masukkan kelahiranmu
= ";
cin >> tahun;
umur = 2012-tahun;
cout << "Unmurmu\t"
<< umur << "tahun\n";
if (umur <17)
cout<<"kamu belum
sweet seventeen\n";
if (umur <15)
cout << "belum cukup
umur\n";
}
|
a.
Branching IF-ELSE
|
#include
<iostream.h>
main()
{ int N;
cout << "Masukkan nilai (0
s/d 100 = ";
cin >> N;
if (N >= 50)
cout <<
"Lulus";
else(N < 50)
cout << "Tidak
Lulus";
}
|
After the compile Error message are as
follows
Means there is a function
of the program is lacking or incorrect, in the program there are 2 statements
and there is one condition that the if-else here is the correct program.
|
#include
<iostream.h>
main()
{ int N;
cout << "Masukkan nilai (0
s/d 100) = ";
cin >> N;
if (N >= 50)
{
cout << "Lulus";
}
else if (N < 50)
}
cout << "Tidak Lulus"
;
}
|
b.
stratified Ramification : IF-ELSE-IF
|
#include
<iostream.h>
main()
{ int N;
cout << "Masukkan nilai =
";
cin >> N;
cout << "Nilai huruf =
";
if (N >=80)
cout << "A";
if (N >=60)
cout << "B";
if (N >=40)
cout << "C";
if (N <40)
cout << "D";
}
|
On the Program, the result is not as it
should because in this form there are some conditions, and this form is used to
certify more than two choices so it must use the function if-else if this is
the right program.
|
#include
<iostream.h>
main()
{ int N;
cout << "Masukkan nilai =
";
cin >> N;
cout << "Nilai huruf =
";
if (N >=80)
cout << "A";
else if (N >=60)
cout << "B";
else if (N >=40)
cout << "C";
else if (N <40)
cout << "D";
}
|
|
Tidak
|
|
B
|
|
Ya
|
|
SELESAI
|
|
Mulai
|
|
C
|
|
Ya
|
|
D
|
|
Ya
|
|
Else
ifN<40
|
|
Tidak
|
|
Else
if N>= 40
|
|
A
|
|
Tidak
|
|
Tidak
|
|
Ya
|
|
Else
if N>= 60
|
|
If
N>=80
|
|
Masukkan
Nilai N
|
|
#include
<iostream.h>
main()
{ int N;
cout << "Masukkan nilai =
";
cin >> N;
cout << "Nilai huruf =
";
if (N >=80&&N<100)
cout << "A";
if (N >=60&&N<80)
cout << "B";
if (N >= 40&&N<60)
cout << "C";
if (N >=0&&N<40)
cout << "D";
}
|
|
SELESAI
|
|
If
N>= 40&& N<40
|
|
C
|
|
If
N>0&&N<40
|
|
D
|
|
B
|
|
If
N>= 60 && N<80
|
|
A
|
|
If
N>=80&&N<100
|
|
Masukkan nilai N
|
|
Mulai
|
task
|
#
include <stdio.h>
#
include <math.h>
#
include <iostream.h>
main()
{
int
N;
float
C, F, suhu ;
cout
<<"===========+<KONVERSI SUHU>+===========\n";
cout
<< "(Celsius -
Fahrenheit & Fahrenheit - Celsius)\n";
cout
<< " \n";
cout
<<"Silahkan Masukan Suhu yang Akan di Konversi : ";
cin
>> suhu;
cout
<<"1. Konversi Celcius ke Fahrenheit\n";
cout
<<"2. Konversi Fahrenheit ke Celcius\n";
cout<<"Masukkan
Konversi Pilihan Anda : ";
cin
>> N;
if
(N==1)
{
F
= 9.00/5.00*suhu + 32;
cout
<< " \n";
cout
<< "Hasil Konversi ke Fahrenheit ="<<F;
}
else
{
C
= (suhu-32)/1.80;
cout
<< " \n";
cout
<< "Hasil Konversi ke Celcius ="<<C;
}
}
|
Results Temperature Conversion
Program
Task 2
Flowchart :
|
MULAI
|
|
Masukkan nilai suhu
|
|
pilihan
menu
|
|
C ke F
|
|
F=9.00/5.00*suhu+32
|
|
C=(suhu-32)/1.80
|
|
F
|
|
C
|
|
C=Celcius
F=Fahrenhiet
|
|
F ke C
|
|
Selesai
|
II.
Conclude
i. There are two basic command structure is conditional,
i.e. IF and switch.
ii. the syntactic Structure can be divided into
three forms, namely if, if-else, and if-else if,
 If the first form of the statement will be
executed if the condition is true.
 If the second form In the second form, if
the condition is true, then working on the statement. But if the condition is
false, then the other dikejakan statements.
 If third form, this Form is used to certify
yangnlebih choice of the two. In this form there is also some some of the
conditions that will be tested by the if command. If one of the conditions is
true, then the pernyatan bersesuian with the statement to be executed. If the
condition does not exist, then it will be working on the other statements.
iii. Flowchart is a sequence of processes in the
system by showing media tool input, output and the type of storage medium in
the process of data processing. Flowchart is also a sequence of instructions
that is described with a specific symbol to solve problems in a program.
iv. some basic things that should be/needs to be
considered for menyususn a good flow charts:
-Have an arrow indicating the direction of the
series to find out process flow
-Each set has a clear understanding of the
limitations/
-Must have a symbol-symbol indicating the decision
for "Yes" and "no" or other expressions to reject and
accept.
III.
REFERENSI
Ø Modul
Praktikum Dasar Pemrograman Kartika Firdausy, S.T.,M.T
Ø http://blog.uin-malang.ac.id/cahyono/2010/10/04/flowchart
