Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehTone Egeland Telah diubah "5 tahun yang lalu
1
Bahasa Pemrograman (Pemrograman Visual)
#2 Structured Control Iteration - Loop
2
Objective Loop For While Break and Continues
3
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); }
4
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 dst
5
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
6
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) ;}
7
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, ,88,11.55}; Gunakan for (int i : nilaiBahasa) Output yang di inginkan : Daftar Nilai Bahasa ============== 20.5 78.8 40.25 dst... ============= Total : 9999 Rata-rata : 9999 Max : 99 Min : 11.5
8
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
9
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) }
10
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; }
11
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");
12
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 ++
13
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.");
14
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;
15
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: Subject: tugas bhsprog 01 Kasih keterangan nim dan nama pada
16
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
17
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
18
“The More You Share, The More You Get”
Terima Kasih “The More You Share, The More You Get”
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.