Ø
contoh code statement if,
int grade = 68;
if( grade > 60 )
System.out.println("Congratulations!");
atau
int grade = 68;
if( grade > 60 ){
System.out.println("Congratulations!");
System.out.println("You passed!");
}
Ø
contoh code statement if-else,
int grade = 68;
if( grade > 60 )
System.out.println("Congratulations!");
else System.out.println("Sorry you
failed");
atau
int grade = 68;
if( grade > 60 ){
System.out.println("Congratulations!");
System.out.println("You passed!");
}
else{
System.out.println("Sorry you
failed");
}
Ø contoh
code statement if-else-if
int grade = 68;
if( grade > 90 ){
System.out.println("Very good!");
}
else if( grade > 60 ){
System.out.println("Very good!");
}
else{
System.out.println("Sorry you
failed");
}
Ø statement
if-else-else if
public class Grade
{
public static void main(
String[] args )
{
double grade = 92.0;
if( grade >= 90 ){
System.out.println(
"Excellent!" );
}
else if( (grade < 90)
&& (grade >= 80)){
System.out.println("Good
job!" );
}
else if( (grade < 80)
&& (grade >= 60)){
System.out.println("Study
harder!" );
}
else{
System.out.println("Sorry, you
failed.");
}
}
}
Ø statement
if-else-else if
public class Grade
{
public static void main(
String[] args )
{
double grade = 92.0;
if( grade >= 90 ){
System.out.println(
"Excellent!" );
}
else if( (grade < 90)
&& (grade >= 80)){
System.out.println("Good
job!" );
}
else if( (grade < 80)
&& (grade >= 60)){
System.out.println("Study
harder!" );
}
else{
System.out.println("Sorry, you
failed.");
}
}
}
Ø Contoh statement
switch
public class Grade
{
public static void main(
String[] args )
{
int grade = 92;
switch(grade){
case 100:
System.out.println(
"Excellent!" );
break;
case 90:
System.out.println("Good
job!" );
break;
case 80:
System.out.println("Study harder!"
);
break;
default:
System.out.println("Sorry, you
failed.");
}
}
}
Ø contoh
while loop
int x = 0;
while (x<10)
{
System.out.println(x);
x++;
}
Ø contoh do-while loop
int x =
0;
do
{
System.out.println(x);
x++;
}while (x<10);
Ø contoh dari for loop
int i;
for( i = 0; i
< 10; i++ ){
System.out.print(i);
}
Ø
contoh Array
public
class ArraySample{
public static
void main( String[] args ){
int[]
ages = new int[100];
for( int
i=0; i<ages.length; i++ ){
System.out.print(
ages[i] );
}
}
Ø
Contoh Unlabeled break statement
String names[] = {"Beah",
"Bianca", "Lance", "Belle",
"Nico", "Yza", "Gem",
"Ethan"};
String searchName = "Yza";
boolean foundName = false;
for( int i=0; i< names.length; i++ ){
if( names[i].equals( searchName )){
foundName = true;
break;
}
}
if( foundName ){
System.out.println( searchName + " found!"
);
}
else{
System.out.println( searchName + " not
found." );
}
Ø Contoh
Labeled break statement
int[][] numbers = {{1, 2, 3}, {4, 5, 6},
{7, 8, 9}};
int searchNum = 5;
boolean foundNum = false;
searchLabel:
for( int i=0; i<numbers.length; i++ ){
for( int j=0; j<numbers[i].length; j++ ){
if( searchNum == numbers[i][j] ){
foundNum = true;
break searchLabel;
}
}
}
if(
foundNum ){
System.out.println(
searchNum + " found!" );
}
else{
System.out.println( searchNum + " not
found!" );
}
Ø Contoh
Unlabeled continue statement
String names[] =
{"Beah", "Bianca", "Lance", "Beah"};
int
count = 0;
for( int i=0; i<names.length; i++ ){
if( !names[i].equals("Beah") ){
continue; //skip next statement
}
count++;
}
System.out.println("There
are " + count + " Beahs in the list");
Ø Contoh
Labeled continue statement
outerLoop:
for( int
i=0; i<5; i++ ){
for( int j=0; j<5; j++ ){
System.out.println("Inside
for(j) loop"); //message1
if( j == 2 ) continue outerLoop;
}
System.out.println("Inside
for(i) loop"); //message2
}
Ø Contoh boolean statement
public
class ujiboolean1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean a, b;
//perrnyataan benar
a = true;
System.out.println("nilai a =
" + a);
if (a) {
System.out.println("pernyataan
ke 1 dieksekusi");
}
if (!a) {
System.out.println("pernyataan
ke 2 dieksekusi");
}
//pernyataan salah
b = false;
System.out.println("nilai b =
" + b);
if (b) {
System.out.println("pernyataan
ke 3 dieksekusi");
}
if (!b) {
System.out.println("pernyataan
ke 4 dieksekusi");
}
}
}
Ø Contoh Penggunaan throw

Ø Contoh throws




Emoticon