You are on page 1of 44

Elemen Bahasa C#

Pertemuan 2

Materi
Variabel dan tipe data
Flow Control
Looping
Branching

Aturan Penamaan Variabel


C# Code Convention

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?

Wajibkah dilakukan inisialisasi variabel?

Initialization vs Assignment
Initialization example:
int hours;

//not initialized

int hours = 0; //initialized to zero

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

Primitive Data Types


short, int, long, ushort, uint, ulong
float, double, decimal
char
bool
byte, sbyte

Integers whole numbers (+ or -)


byte

1 byte

0-255

short

2 bytes

32,767

int

4 bytes

Just over 2.1 billion

long

8 bytes

Very large number

Floating Point decimal values


float

double

decimal

4 bytes

single precision 6 digits to


the right of the decimal point.

8 bytes

double precision 14 digits


to right of the decimal point.

16 bytes

28 digits to right of the decimal


point

Alphanumeric, Boolean, and Date


char

2 Bytes

1 Unicode character
(ASCII 8 bits
Unicode 16 bits)

string

varies

1 or more characters

bool

1 byte

Boolean: True or False

8 Bytes

01/01/0001 - 12/31/9999
includes HH:MM:SS

DateTime

Non-primitive Data Types


String
Object
Array
List
Queue
Stack
dll.

Implicit Data types


Di dalam .NET dikenal keywords

var

Digunakan untuk:
Deklarasi tipe data secara

implisit,

dan harus ada inisialisasi


Best practice: untuk

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)

// Write the index to the screen

Console.Write("While statement ");


Console.WriteLine(i);
// Increment the variable.
i++;
}

do-while
int i = 0;
do
{

// Write the index to the screen

Console.Write("While statement ");


Console.WriteLine(i);
// Increment the variable.
i++;
} while (i < 10);

Question Time
Apa perbedaan utama dari loop while dan dowhile?

foreach
string[] ferns = {
"Psilotopsida", "Equisetopsida",
"Marattiopsida", "Polypodiopsida
};

// Loop with the foreach keyword.


foreach (string value in ferns)
{
Console.WriteLine(value);
}

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

Aturan Penamaan Variabel


PascalCase
camelCase
Upper Case

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)

Nama property harus deskriptif dan tidak


ambigu

Method Naming
Nama methods harus mencerminkan

kerja

kata

(verbs)

Nama methods harus mencerminkan operasi


yang dilakukan

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

only one statement

per line
Write

only one declaration

per line
Make a
Use

clear indentations

parentheses for more clear

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

/* C++ and Java 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

You might also like