E N D
SSK 1013-ASAS PENGATURCARAAN TOPIK 2 & 3 : PEMBOLEHUBAH, JENIS DATA DAN OPERATOR ARITMETIK ANIS ELIANI BINTI ABDUL RAHMAN
C# adalahbahasapengaturcaraan yang berorientasikanobjek. • MUDAHuntukdipelajari.
RUANGAN PENGISTIHARAN (DECLARATION) • CLASS • CLASS METHODS • CLASS ATTRIBUTES • MAIN METHOD • STATEMENTS & EXPRESSIONS • COMMENTS STRUKTUR ASAS PENGATURCARAAN C#
KOMEN /PENERANGAN HEADER : PREPROCESSOR RUANG PROGRAM CLASSS PROGRAM KOMEN /PENERANGAN STRUKTUR ASAS PROGRAM
Pembolehubahadalahsalahsatujenispengecam. • Pembolehubahadalahpengecam yang nilainyabolehberubahsemasaperlaksanaanaturcara. • Iamestidiisytiharkansebelumbolehdigunakan. • Contohpengisytiharanpembolehubah: • int x; • float balance; • char ch; • int y; • Bolehdigabungkanjikamempunyaijenis data yang sama: • intx, y, z; • char ch, chr; • float f, balance; PEMBOLEHUBAH
Contohpengisytiharanpembolehubah: int x; doublebalance; charch; int y; Bolehdigabungkanjikamempunyaijenis data yang sama: int x, y, z; char ch, chr; double f, balance; PEMBOLEHUBAH
PERATURAN MENAMAKAN • Panjangaksaradari 1 hingga 255 aksara • Bolehterdiridaripadahuruf & digit • Tidakbolehterdiridaripadaruangkosongdanperkataaanrizab/khasataukeyword. keywordadalahperkataan yang sudahmempunyaifungsi-fungsitertentudanbukan variable. • Nama yang bermaknadanmudah • Nombor, underscore atausimboltidakdibenarkansebelum variable. Jarakantara variable dengan variable juga tidakdibenarkan. • Setiap variable mestilahbermuladarihurufbarulahbolehdiletakkannombordan underscore. PEMBOLEHUBAH
Naming Variables – Examples Proper names: - name - first_Name - name2 Improper names (will lead to compilation error): - 1 (digit) - if (keyword) - 2name(starts with a digit) PEMBOLEHUBAH
Contohnama yang sesuaiuntukpembolehubah - noPertama - umur - nama_pelajar - NoKedua PEMBOLEHUBAH
And here are some examples for badly named variables (although the names are correct from the C# compiler’s perspective): - _firstName (starts with _) - last_name (contains _) - AGE (is written with capital letters) - Start_Index (starts with capital letter and contains _) - lastNegativeNumber_Index (contains _) - a37 (the name is not descriptive and does not clearly provide the purpose of the variable) - fullName23, fullName24, etc. (it is not appropriate for a variable name to contain digits unless that would improve variable use clarity) PEMBOLEHUBAH
NILAI TETAP /CONSTANT • The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. • Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. • The constants are treated just like regular variables except that their values cannot be modified after their definition. • Constants are defined using the const keyword. Syntax for defining a constant is: • const<data_type> <constant_name> = value; • EXAMPLE : constdouble pi = 3.14159;
OPERATOR ARITMETIK (+, -, *, /) • Assume variable A holds 10 and variable B holds 20 then:
OPERATOR PRECEDENCE • KEUTAMAAN PROSES MENGIKUT OPERATOR • Operator precedence determines the grouping of terms in an expression. This affects evaluation of an expression. • Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator. • For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so the first evaluation takes place for 3*2 and then 7 is added into it.
OPERATOR PRECEDENCE KEUTAMAAN PROSES MENGIKUT OPERATOR So, 7 + 3 * 2 = ?
ASSIGNMENT / UMPUKAN • Cara yang paling lazimdigunakanuntukmendapatkannilaipembolehubahadalahdenganmenggunakanumpukan. • Tanda‘=’ disebutpengendaliumpukan. • Sintaks: • Variable_name= ungkapan; • Contoh : • name = "John Smith"; • age = 25;
CONTOH ATURCARA MENGGUNAKAN PELBAGAI JENIS DATA & OPERATOR ARITMETIK CONTOH SOALAN : KIRA HASIL TAMBAH, TOLAK, DARAB DAN BAHAGI BAGI 2 NOMBOR
ATURCARA CONSOLE
private void Button_Click_1(object sender, RoutedEventArgs e) { int nombor1; int nombor2; inthasil; nombor1 = int.Parse(nilai1.Text); nombor2 = int.Parse(nilai2.Text); hasil = nombor1 + nombor2; result.Text = ("Hasiltambahialah ") + hasil; }