Title: Flow Control
1Flow Control
- Nana Ramadijanti
- Laboratorium Computer Vision
- Politeknik Elekltronika Negeri Surabaya PENS-ITS
- 2008
2The Selection Statements
- if
- if-else
- else-if
- switch
3if
if (ekspresi boolean) pernyataan1 pernyataan
2
4public class If public static void
main(String args) int bilangan -5
if (bilanganlt0) System.out.println(Bilan
gan adalah negatif)
5if-else
if (ekspresi boolean) pernyataan1 else
pernyataan2 pernyataan3
6If - else
- If() statement takes a boolean expression, not a
numeric value. - You cannot convert or cast boolean types and
numeric types. - If you have
- if (x) // x is int
- use
- if (x!0)
7public class IfElse public static void
main(String args) int bilangan -5
if (bilanganlt0) System.out.println(Bilan
gan adalah negatif) else
System.out.println(Bilangan adalah positif)
8else-if
if (ekspresi boolean1) pernyataan1 else
if (ekspresi boolean2) pernyataan2 else
pernyataan3 pernyataan4
9switch
switch (ekspresi) case konstanta1
pernyataan1 break case
konstanta1 pernyataan2
break default
pernyataan3 pernyataan4
10switch(x)
- Variabel x harus bertipe byte, short, char, atau
int. - Floating point, long, atau class references
(termasuk String) tidak diperbolehkan. - Kedudukan statement pada default sama dengan
kedudukan else pada if-else.
11(No Transcript)
12(No Transcript)
13public class Switch public static void
main(String args) int i2
switch (i) case 1 i3
break case 2 i5
break default
i10 System.out.println(i)
14The Loop Statements
- The for() Loop
- The while() Loop
- The do while() Loop
15for
for (inisialisasi ekspresi boolean perubah)
pernyataan
16for
- Java programming language allows the comma
separator in a for() loop structure. - Example
- for (i0, j 0 jlt10 i, j)
17for
for (int i0 ilt10 i) System.out.println(Ho
re !!)
for (int i 0 i lt 10 i)
System.out.println("Are you finished
yet?") System.out.println("Finally!")
18while
while (ekspresi boolean) pernyataan
19while
int i 0 while (ilt10) System.out.println(Hor
e !!) i
int i 0 while (i lt 10)
System.out.println("Are you finished yet?")
i System.out.println("Done")
20do while
do pernyataan while (ekspresi boolean)
21do while
int i 0 do System.out.println(Hore
!!) i while (ilt10)
int i 0 do System.out.println("Are you
finished yet?") i while (i lt
10) System.out.println("Done")
22Special Loop Control
- break label
- continue label
- label statement (statement ini berupa loop)
23Special Loop Control
- break digunakan untuk keluar (prematurely exit)
dari switch statements, loop statements, dan
labeled blocks. - continue digunakan untuk meneruskan (skip over
and jump) ke akhir dari loop body, dan kembali ke
loop control statement. - label digunakan untuk mengidentifikasi statement
lain dimana statement lain ini meminta supaya
block statement pada label ini dikerjakan.
24(No Transcript)
25(No Transcript)
26(No Transcript)
27(No Transcript)
28Prak 1 if
import java.util.Scanner public class IfElseName
public static void main(String args)
Scanner inputnew Scanner(System.in) char
firstInisial (char) -1 System.out.println("
Enter your first inisial ") try
firstInisial(char)input.next().charAt(0)
catch(Exception e)
System.out.println("Error "e.toString())
if (firstInisial'A')
System.out.println("Your name must be Ali")
else if(firstInisial'B')
System.out.println("Your name must be Basuki ")
else System.out.println("I can't
figure your name")
29Prak 2 switch without break
import java.util.Scanner public class IfElseName
public static void main(String args)
Scanner inputnew Scanner(System.in) char
firstInisial (char) -1 System.out.println("
Enter your first inisial ") try
firstInisial(char)input.next().charAt(0)
catch(Exception e)
System.out.println("Error "e.toString())
switch(firstInisial) case A
System.out.println("Your name must be Ali")
case B System.out.println("Your name
must be Basuki ") default
System.out.println("I can't figure your name")
30Prak 3 switch with break
import java.util.Scanner public class IfElseName
public static void main(String args)
Scanner inputnew Scanner(System.in) char
firstInisial (char) -1 System.out.println("
Enter your first inisial ") try
firstInisial(char)input.next().charAt(0)
catch(Exception e)
System.out.println("Error "e.toString())
switch(firstInisial) case A
System.out.println("Your name must be
Ali")break case B
System.out.println("Your name must be Basuki
")break default
System.out.println("I can't figure your name")
31Prak 4 if else dan for
public class ForCount public static void
main(String args) Scanner inputnew
Scanner(System.in) char nilai(char)-1
int numToCount System.out.println("Enter
number to count between 0-10 ") try
nilaiinput.next().charAt(0)
catch(Exception e)
System.out.println("Error "e.toString())
numToCountCharacter.digit(nilai, 10)
if((numToCountgt0)(numToCountlt10))
for(int i0iltnumToCounti)
System.out.println(i) else
System.out.println("The number was not between 0
and 10")
32Prak 5 if else dan while
public class ForCount public static void
main(String args) Scanner inputnew
Scanner(System.in) char nilai(char)-1
int numToCount System.out.println("Enter
number to count between 0-10 ") try
nilaiinput.next().charAt(0)
catch(Exception e)
System.out.println("Error "e.toString())
numToCountCharacter.digit(nilai, 10)
if((numToCountgt0)(numToCountlt10)) int
i0 while (iltnumToCount)
System.out.println(i)i
else System.out.println("The number was
not between 0 and 10")
33Prak 6 do while with break
public class BreakLoop public static void
main(String args) int i0 do
System.out.println("I'm stuck") i
if(igt9)break while(true)
34Prak 7 switch case dan if else
import java.util.Scanner public class
SwitchDemo2 public static void main(String
args) Scanner inputnew Scanner(System.in)
int month,year,numDays0
System.out.println("Masukkan Bulan ke (1-12)
") monthinput.nextInt()
System.out.println("Masukkan Tahun ")
yearinput.nextInt() switch(month)
case 1 case 5 case 7
case 8 case 10 case 12
numDays31 break case
4 case 6 case 11
numDays30 break
35Prak 7 switch case dan if else (cont)
case 2 if(year40)
numDays29 else
numDays28 break //end of
case System.out.println("Num of Days
"numDays) //end of main //end of class
36Prak 8 for dan array
37Prak 9 Looping dan if elseif
- Setiap matakuliah di Jurusan TI setiap akhir
semester akan dianalisa berapa jumlah siswa yang
mendapat grade A, grade B, grade C, dan
grade D diketahui konversi nilai ke huruf sbb - A 100-90
- B 80-90
- C 60-80
- D lt60
- Berapa nilai rata-rata siswa dalam 1 kelas
- Sebagai input adalah nilai siswa dalam 1
kelas dimisalkan jumlah siswa 10 - output nilai huruf A,B,C,dan D tiap siswa dan
nilai rata-rata siswa dalam 1 kelas