dipertemuan 3 ini membahas :
-Program Control – Selection
-Selection: SWITCH-CASE
-?: Operator
-Go To and Label
-Jenis-jenis Error
Program control
Selection (I)
In order to perform meaningful tasks, it’s common for programs to select some sections of code for execution, and to omit other sections of code. The decision to select or omit sections of code is based on conditions encountered during processing. This type of program control is called selection.
For example, in a program which calculates the quotient of integer pairs, it’s important to avoid division by zero. This means that the program must be able to:
1.identify the condition of a divisor with a value of zero,
2.omit the arithmetic operation, and
3.provide the program’s user with an explanation for the omission.
Following is a program fragment which is able to perform these three tasks. It selects one section of code, and omits another, based on the value stored in the variable divisor.
% Example 1:
put dividend, ” / “, divisor, ” = ” ..
if divisor = 0 then
% handle division by zero:
put ” undefined.”
else
% handle normal case:
put dividend / divisor : 0 : 1
end if
Boolean expressions
Example 1 uses an if statement. if statements begin with the keyword if, followed by a boolean expression and the keyword then.
Boolean expressions have a value of true or false. These values are not strings: they are stored in memory differently, and different operations can be performed with them.
Boolean expressions can be:
•constructed with relational operators, as shown in Example 1,
•used to assign a value to boolean variables, as shown in Example 2, and
•constructed with boolean operators, as shown in Example 3.
% Example 2:
var admissible : boolean
admissible := age > MINIMUM_AGE
% Example 3:
var identityConfirmed : boolean
identityConfirmed := name = “Victoria Windsor”
if not identityConfirmed then
% handle this case
end if
if statement
Using an if statement, we can direct the computer to select one section of code for execution, and to omit other sections of code. An if statement can take one of three forms, as shown below.
Form 1
% Form 1: one true part and no false part
if boolean_expression then
zero or more statements comprising the true” part
end if
When the computer encounters an if statement of Form 1:
1.The computer evaluates the boolean expression.
2.If the boolean expression has the value true, the computer executes the true part.
3.The computer continues execution with the statement which immediately follows the keywords end if.
Form 2
% Form 2: one true part and one false part
if boolean_expression then
zero or more statements comprising the “true” part
else
zero or more statements comprising the “false” part
end if
When the computer encounters an if statement of Form 2:
1.The computer evaluates the boolean expression.
2.If the boolean expression has the value true, the computer executes the true part, and omits the false part.
3.If the boolean expression has the value false, the computer omits the true part, and executes the false part .
4.The computer continues execution with the statement which immediately follows the keywords end if.
Form 3
% Form 3: more than one true part and
% one false part
if boolean_expression_1 then
zero or more statements comprising the
“true” part for boolean_expression_1
elsif boolean_expression_2 then
zero or more statements comprising the
“true” part for boolean_expression_2
⋮
elsif boolean_expression_n then
zero or more statements comprising the
“true” part for boolean_expression_n
else
zero or more statements comprising the
“false” part
end if
When the computer encounters an if statement of Form 3:
1.The computer evaluates boolean_expression_1.
2.If boolean_expression_1 has the value true, the computer executes the true part for boolean_expression_1, and omits all other parts.
3.If boolean_expression_1 has the value false, the computer omits the true part for boolean_expression_1, and evaluates boolean_expression_2.
4.If boolean_expression_2 has the value true, the computer executes the true part for boolean_expression_2, and omits all other parts.
5.If boolean_expression_2 has the value false, the computer omits the true part for boolean_expression_2, and evaluates the next boolean expression…
6.If boolean_expression_n has the value true, the computer executes the true part for boolean_expression_n, and omits the false part.
7.If boolean_expression_n has the value false, the computer omits the true part for boolean_expression_n, and executes the false part.
8.The computer continues execution with the statement which immediately follows the keywords end if.
Jenis Jenis Error
Error !, mungkin ini kerap menghantui anda mana kala melakukan kegiatan pemrograman. Nah agar anda bisa memahami lebih lanjut lagi dengan error yang anda temukan, anda harus mengetahui klasifikasi error tersebut.
Pada dasarnya jenis error di klasifikasikan atas 3 jenis, yaitu :
1.Syntax Error
2.Runtime Error
3.Logic Error
1. Syntax Error
Kesalahan jenis ini sangat tergolong mudah untuk di perbaiki. Hal ini di karenakan kesalahan ini akan memunculkan baris yang salah dan bahkan langsung statemen yang salah akan di tampilkan.
Kesalahan jenis ini akan muncul jika program akan di kompilasi atau di jalankan.
Untuk jenis jenis pemrograman yang harus di Kompilasi, kesalahan ini sering terjadi. Tetapi untuk pemrograman yang Interpretasi, atau jenis Interpreter, seperti Visual Basic, VB.Net dan lainnya maka kesalahan jenis ini sudah langsung di perbaiki oleh Interpreternya ataupun langsung di tandai (Mark). Sehingga akan langsung di edit.
2. Runtime Error
Kesalahan jenis ini juga tergolong mudah di perbaiki. Kesalahan ini akan muncul saat program sedang berjalan, jika program yang sedang di jalankan, terjadi error, maka kesalahan akan di tampilkan dan di tujukan pada baris yang salah.
Hmm sangat mudah di perbaiki kan? 😀
3. Logic Error
Nah untuk kesalahan jenis ini, sangat sulit di perbaiki, karna termasuk logika kita atau yang membuatnya yang salah. Sehingga butuh waktu yang lama untuk memperbaikinya. Hal ini dikarenakan logika program terdapat kesalahan.
Kesalahan ini sulit di ketahui, hanya programer programer yang teliti dengan outputnya saja yang bisa mengantisipasi kesalahan ini.
Nah 3 jenis kesalahan sudah saya uraikan satu demi satu, saya mengharapkan anda memahami kesalahan kesalahan yang anda temui di dalam pemrograman.