Bahasa Pemrograman (Pemrograman Visual) #2 Structured Control Iteration - Loop
Objective Loop For While Break and Continues
Iteration for - loops for - loops i = 1 Syntax for - loops in java True False Statements Here i++ Syntax for - loops in java for (initialization; termination; multiplier) { // your code goes here } Example for - loops java for(int i=1; i<6; i++) { System.out.println("Count is: " + i); }
latihan Buat class:Asli100. Tampilkan bilangan dari 1 s/d 100 dengan menggunakan loop – for Buat class:Genap100 , menampilkan bilangan sebanyak 100 angka bilangan genap berurutan dimulai dari 150 Buat class:Pangkat2 ; menampilkan bilangan dan pangkatnya dari 1 s/d 100 output : angka pangkat2 1 1 2 4 ... dst
Iteration for - loops import java.util.Scanner; public class string_charCheck { public static void main(String[] args) { String kalimat; char cari; int jumlah=0; System.out.println("Masukkan kalimat anda! "); Scanner input1 = new Scanner(System.in); kalimat = input1.nextLine(); System.out.println("Masukkan karakter yg ingin dihitung! "); Scanner input2 = new Scanner(System.in); cari = input2.nextLine().charAt(0); Example for – searching number of character in a sentence for (int i = 0; i < kalimat.length(); i++) { if (cari == kalimat.charAt(i)) { jumlah++; } System.out.println(jumlah); Continued
Loop pada array (for var:array) Array adalah suatu sekumpulan variable yang memiliki nama sama dan bertipe data yang sama serta berindex. Awal index dimulai dari nol Misalkan array bertipe integer int[] nilai = {100,75,30} nilai Memiliki 3 anggota nilai[0] adalah 100 , nilai[1] adalah 75, nilai[2] adalah 30 Loop menggunakan for(var : array) for (int i : nilai) { System.out.println(i) ;}
Latihan Class : NilaiBahasa Tampilkan dan hitung nilai total, rata-rata ,max,min utk array double[] nilaiBahasa ={20.5,78.8,40.25,90.15,77,92,46,99,48,60,62,67, 80.67,88,11.55}; Gunakan for (int i : nilaiBahasa) Output yang di inginkan : Daftar Nilai Bahasa ============== 20.5 78.8 40.25 dst... 15. 11.55 ============= Total : 9999 Rata-rata : 9999 Max : 99 Min : 11.5
Iteration while - loops True False Statements Here i++ while (i < 6) { // your code goes here } Front check while - loops Statements Here i++ Rear check while - loops i < 6 do { // your code goes here } while (i < 6) True False
Iteration while - loops Contoh front check repetition in java class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } Syntax rear check repetition in java class WhileDemo { public static void main(String[] args){ int count = 1; do{ System.out.println("Count is: " + count); count++; } while (count < 11) }
break Statement in Iteration break at for - loops i < L i = 0 True False i++ cari = str.charAt(i) Contoh break pada java … for (i = 0; i < L; i++) { if (str.charAt(i)==cari) { break; }
break Statement in Iteration Contoh break pada java class BreakDemo { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; int searchfor = 12; int i; boolean foundIt = false; for (i = 0; i < arrayOfInts.length; i++) { if (arrayOfInts[i] == searchfor) { foundIt = true; break; } if (foundIt) { System.out.println("Found " + searchfor + " at index " + i); } else { System.out.println(searchfor + " not in the array");
continue Statement in Iteration break at for - loops i = 0 Contoh break pada java i < L False … for (i = 0; i < L; i++) { if (str.charAt(i)!=cari) { continue; } jmlChar++; True cari != str.charAt(i) True False i++ jmlChar ++
continue Statement in Iteration Contoh continue pada java class ContinueDemo { public static void main(String[] args) { String searchMe = "peter piper picked a peck of pickled peppers"; int max = searchMe.length(); int numPs = 0; for (int i = 0; i < max; i++) { //interested only in p's if (searchMe.charAt(i) != 'p') continue; //process p's numPs++; } System.out.println("Found " + numPs + " p's in the string.");
Methods Method is known as a function or procedure in other languages. public class MinTest { public static void main( String [ ] args ) { int a = 3; int b = 7; System.out.println( min( a, b ) ); } // Method declaration public static int min( int x, int y ) { return x < y ? x : y;
Exercises Modifikasi class calculatorApp (pada slide berikutnya), sehingga membaca input untuk x dan y, dengan menggunakan JOptionPane. Modifikasi program Example for – searching number of character in a sentence dengan menggunakan while – loops. Kirim ke: augury.elrayeb@upj.ac.id Subject: tugas bhsprog 01 Kasih keterangan nim dan nama pada email
Universitas Pembangunan Jaya – SIF_TIF Class calculatorApp class calculatorApp { public static void main(String[] args) { double x = 7.5; double y = 3.3; System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("x + y = " + (x+y)); System.out.println("x - y = " + (x-y)); System.out.println("x * y = " + (x*y)); System.out.println("x / y = " + (x/y)); } Tugaskan mahasiswa membuat dengan membaca input untuk x dan y, dengan menggunakan scanner class AER – 2011/2012 Universitas Pembangunan Jaya – SIF_TIF
Iteration for - loops import java.util.Scanner; public class string_charCheck { public static void main(String[] args) { String kalimat; char cari; int jumlah=0; System.out.println("Masukkan kalimat anda! "); Scanner input1 = new Scanner(System.in); kalimat = input1.nextLine(); System.out.println("Masukkan karakter yg ingin dihitung! "); Scanner input2 = new Scanner(System.in); cari = input2.nextLine().charAt(0); Example for – searching number of character in a sentence for (int i = 0; i < kalimat.length(); i++) { if (cari == kalimat.charAt(i)) { jumlah++; } System.out.println(jumlah); Continued
“The More You Share, The More You Get” Terima Kasih “The More You Share, The More You Get”