Flow Control - PowerPoint PPT Presentation

About This Presentation
Title:

Flow Control

Description:

Politeknik Elektronika Negeri Surabaya PENS-ITS. public class If ... if (ekspresi boolean1) { pernyataan1; } else if (ekspresi boolean2) { pernyataan2; ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 38
Provided by: Bas7150
Category:
Tags: boolean1 | control | flow

less

Transcript and Presenter's Notes

Title: Flow Control


1
Flow Control
  • Nana Ramadijanti
  • Laboratorium Computer Vision
  • Politeknik Elekltronika Negeri Surabaya PENS-ITS
  • 2008

2
The Selection Statements
  • if
  • if-else
  • else-if
  • switch

3
if
if (ekspresi boolean) pernyataan1 pernyataan
2
4
public class If public static void
main(String args) int bilangan -5
if (bilanganlt0) System.out.println(Bilan
gan adalah negatif)
5
if-else
if (ekspresi boolean) pernyataan1 else
pernyataan2 pernyataan3
6
If - 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)

7
public 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)

8
else-if
if (ekspresi boolean1) pernyataan1 else
if (ekspresi boolean2) pernyataan2 else
pernyataan3 pernyataan4
9
switch
switch (ekspresi) case konstanta1
pernyataan1 break case
konstanta1 pernyataan2
break default
pernyataan3 pernyataan4
10
switch(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)
13
public 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)

14
The Loop Statements
  • The for() Loop
  • The while() Loop
  • The do while() Loop

15
for
for (inisialisasi ekspresi boolean perubah)
pernyataan
16
for
  • Java programming language allows the comma
    separator in a for() loop structure.
  • Example
  • for (i0, j 0 jlt10 i, j)

17
for
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!")
18
while
while (ekspresi boolean) pernyataan
19
while
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")
20
do while
do pernyataan while (ekspresi boolean)
21
do 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")
22
Special Loop Control
  • break label
  • continue label
  • label statement (statement ini berupa loop)

23
Special 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)
28
Prak 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")
29
Prak 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")

30
Prak 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")

31
Prak 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")
32
Prak 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")
33
Prak 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)
34
Prak 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
35
Prak 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
36
Prak 8 for dan array
37
Prak 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
Write a Comment
User Comments (0)
About PowerShow.com