Professional Documents
Culture Documents
Pertemuan 2
Materi
Variabel dan tipe data
Flow Control
Looping
Branching
VARIABEL DAN
TIPE DATA
Variables
Variabel adalah lokasi penyimpanan di memory
dan memiliki nama simbolik yang memiliki
suatu nilai/informasi
0c100018
3.69
double ipk;
00000002
Deklarasi Variabel
Syntax:
dataType variableName;
C# convention:
int quantity;
decimal price;
string name;
Inisialisasi Variabel
Ketika sebuah variabel dideklarasikan mereka akan
mendapat sebuah default value.
Apa default value untuk string?
Apa default value untuk numeric variabel?
Initialization vs Assignment
Initialization example:
int hours;
//not initialized
Assignment example:
decPrice = Decimal.Parse
(txtPrice.Text);
decGross = intHours * decRate;
Data Types
Primitive Data Types
traditional data types
Non-primitive Data
Types
Generic and Collections
User defined data types
1 byte
0-255
short
2 bytes
32,767
int
4 bytes
long
8 bytes
double
decimal
4 bytes
8 bytes
16 bytes
2 Bytes
1 Unicode character
(ASCII 8 bits
Unicode 16 bits)
string
varies
1 or more characters
bool
1 byte
8 Bytes
01/01/0001 - 12/31/9999
includes HH:MM:SS
DateTime
var
Digunakan untuk:
Deklarasi tipe data secara
implisit,
lokal
variabel
FLOW CONTROL
Looping
for
while
do-while
foreach
for
for (int i = 0; i <
10; i++)
{
Console.WriteLine(i);
}
while
int i = 0;
{while (i < 10)
do-while
int i = 0;
do
{
Question Time
Apa perbedaan utama dari loop while dan dowhile?
foreach
string[] ferns = {
"Psilotopsida", "Equisetopsida",
"Marattiopsida", "Polypodiopsida
};
foreach
Hanya untuk koleksi (array, list, dll.)
TIDAK diperkenankan
mengubah data
koleksi yang sedang diiterasi
di dalam
Question Time
Apakah keunggulan dan kelemahan dari
masing-masing branching (if dan switch)?
Branching
if-else
switch
if-else
if
(conditions)
else
{
if-else
if
(condition1) {
else if
{
else
{
}
(condition2)
switch
switch (caseSwitch) {
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}
Question Time
Apakah keunggulan dan kelemahan dari
masing-masing branching (if dan switch)?
Aturan
Penamaan
Variabel
Pascal Case
Digunakan untuk identifiers (class name,
interface name, methods name, namespace,
etc.)
Setiap kata diawali dengan huruf kapital
Contoh:
public class BackColor;
public interface IDisplay;
Camel Case
Digunakan untuk object instance dan
variabel
Kata pertama diawali huruf kecil, kata
selanjutnya diawali huruf besar
Contoh:
Color backColor;
int maxHeight;
Upper Case
Digunakan untuk nama identifier yang memiliki
kurang dari dua huruf.
Contoh:
System.Web.UI
System.IO
Property Naming
Nama property harus mencerminkan
benda
kata
(noun)
Method Naming
Nama methods harus mencerminkan
kerja
kata
(verbs)
A bad example
http://geek-and-poke.com/2013/02/self-documenting-code.html
C# CODE
CONVENTION
Manfaat
Consistent look focus on content
Understandable
Easy for copying, changing, maintaining
C# best practice
Conventions
Naming
Layout
Conventions
Conventions
Commenting
Conventions
Naming conventions
Menggunakan camelCase dan PascalCase
Prefiks khusus untuk beberapa identifier
Interface: IDisplay, IEnumerable, dsb.
Layout Conventions
Write
per line
Write
per line
Make a
Use
clear indentations
structure
Commenting Conventions
Place the comment on a separate line, not at
the end of a line of code.
Begin comment text with an uppercase letter.
End comment text with a period.
Insert one space between the comment
delimiter (//) and the comment text
Commenting Conventions
// C# style comment
// The following declaration creates a query.
// It does not runthe query
Latihan
Debugging
http://www.codeproject.com/Articles/79508/Mas
tering-Debugging-in-Visual-Studio-2010-A-Begi
nn