(5th meeting) Outline • Statements • Iteration Statements • foreach, in statement • Jump Statements • goto statements • yield statements • Form and Control in C# Visual
Indah Permatasari, M.Kom. 2
Statements
Indah Permatasari, M.Kom. 3
Statements • A declaration statement introduces a new variable or constant. • A variable declaration can optionally assign a value to the variable. In a constant declaration, the assignment is required. • Expression statements that calculate a value must store the value in a variable. • Kinds: • Selection statements → if, else, switch, case • Iteration statements → do, for, while, foreach, in • Jump statements → break, continue, default, return, goto, yield • Exception handling statements → throw, try-catch, try-finally, try-catch-finally Indah Permatasari, M.Kom. 4 Iteration statements • Iteration statements enable you to loop through collections like arrays, or perform the same set of statements repeatedly until a specified condition is met • foreach • in
Indah Permatasari, M.Kom. 5
foreach, in • executes a statement or a block of statements for each element string a = "Hello world"; Pernyataan foreach diartikan sebagai berikut: “Lakukan operasi foreach(char b in a) { yang sama untuk setiap karakter b Console.Write($"[{b}] "); yang merupakan elemen di dalam } variabel a“. /** * Output: * [H] [e] [l] [l] [o] [ ] [w] [o] [r] [l] [d] */
Indah Permatasari, M.Kom. 6
foreach /** * Element #0: 0 * Element #1: 1 using System; * Element #2: 1 using System.Collections.Generic; * Element #3: 2 namespace Statement * Number of elements: 4 { class Program */ { public static void Main() { var fibNumbers = new List<int> { 0, 1, 1, 2 }; int count = 0; foreach (int element in fibNumbers) { Console.WriteLine($"Element #{count}: {element}"); count++; } Console.WriteLine($"Number of elements: {count}"); Console.ReadKey(); } } }
Indah Permatasari, M.Kom. 7
goto • The goto statement transfers the program control directly to a labeled statement • A common use of goto is to transfer control to a specific switch-case label or the default label in a switch statement • The goto statement is also useful to get out of deeply nested loops
Digunakan untuk melakukan lompatan kemana saja dari program kecuali program ke dalam suatu blok
Indah Permatasari, M.Kom. 8
class Program { static void Main() { Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large"); Console.Write("Please enter your selection: "); string s = Console.ReadLine(); int n = int.Parse(s); int cost = 0; switch (n) /* Output: { case 1: Coffee sizes: 1=Small 2=Medium 3=Large cost += 25; break; Please enter your selection: 2 case 2: Please insert 50 cents. cost += 25; goto case 1; Thank you for your business. case 3: cost += 50; */ goto case 1; default: Console.WriteLine("Invalid selection."); break; } if (cost != 0) { Console.WriteLine($"Please insert {cost} cents."); } Console.WriteLine("Thank you for your business."); // Keep the console open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } }
Indah Permatasari, M.Kom. 9
yield • When you use the yield contextual keyword in a statement, you indicate that the method, operator, or get accessor in which it appears is an iterator • Using yield to define an iterator removes the need for an explicit extra class • You use a yield return statement to return each element one at a time • You can use a yield break statement to end the iteration. • Sintax: • yield return <expression>; • yield break;
Indah Permatasari, M.Kom. 10
using System; using System.Collections.Generic; namespace Statement { class Program { static void Main() { // Display powers of 2 up to the exponent of 8: foreach (int i in Power(2, 8)) { Console.Write("{0} ", i); } Console.ReadKey(); } public static IEnumerable<int> Power(int number, int exponent) { int result = 1; for (int i = 0; i < exponent; i++) { result = result * number; yield return result; /** } * Output: } } * 2 4 8 16 32 64 128 256 } */
Indah Permatasari, M.Kom. 11
Form & Control in C# Visual Introduction Windows Form Application (.NET)
Indah Permatasari, M.Kom. 12
Console Application vs Windows Application • Aplikasi konsol menampilkan • Aplikasi windows memiliki pada jendela system konsol, spt antarmuka grafis DOS, Command prompt, dll • File > New > Project > Windows • File > New > Project > Console Form Application (.NET) Application (.NET)
Indah Permatasari, M.Kom. 13
Form • Proyek baru yang telah dibuat dengan “Windows Form Application (.NET)” akan menyiapkan kotak kosong yang nantinya dapat diisi oleh beberapa control (control statements).
Indah Permatasari, M.Kom. 14
Pointer • Pointer digunakan untuk memilih kontrol-kontrol yang telah dibuat/dimasukkan ke dalam Form. • Alternatif pada Panel Properties
Indah Permatasari, M.Kom. 15
Button • Untuk meletakkan tombol di dalam form • Dipakai untuk memroses input-input atau data yang dimasukkan oleh pengguna ke dalam kontrol yang telah disediakan
Indah Permatasari, M.Kom. 16
TextBox • Digunakan untuk meng-input data teks maupun angka
TextBox . BackColor pada properties, diubah menjadi black
Indah Permatasari, M.Kom. 17
Lainnya… • Label → membuat tulisan di dalam form • LinkLabel → membuat informasi dalam bentuk teks yang mengandung link • DateTimePicker → untuk membantu pengguna memilih tanggal dan waktu secara cepat dan akurat • PictureBox → untuk memasang gambar atau citra bitmap ke dalam jendela aplikasi • Checkbox → memasang control berbentuk kotak cek (checkbox), hanya dapat memilih salah satu dari dua kondisi yaitu True atau False • RadioButton → kotak cek yang bisa ditampilkan satu saja dalam jendela aplikasi, umumnya dihadirkan dalam bentuk satu set sehingga pengguna dapat memilih salah satu dari pilihan yang tersedia
Indah Permatasari, M.Kom. 18
Lainnya…. • CheckListBox → beberapa jumlah ckeckbox yang dibutuhkan dalam satu jendela aplikasi • NumericUpDown → untuk menentukan valur dalam bentuk angka • RichTextBox → untuk menyediakan tempat bagi pengguna yang ingin menulis teks dalam jumlah banyak • ComboBox → atau menu drop-down, untuk melihat opsi-opsi yang ada di dalamnya • ListBox → pengguna dapat memilih lebih dari satu opsi yang ditawarkan
Indah Permatasari, M.Kom. 19
Contoh
Indah Permatasari, M.Kom. 20
using System; using System.Windows.Forms; namespace Formulir { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int a = 10; int b = 11; int hasil; hasil = (a + b); MessageBox.Show("Hasil penjumlahan dari " + a.ToString() + " dan " + b.ToString() + " adalah " + hasil.ToString()); } } }