You are on page 1of 15

Nama : Arfanio Liswanda Bahtiar

NIM : E31182101
Prodi : Manajemen Informatika
Golongan :D

public class Operator8 {


public static void main(String[] args)
{
int x,y;
x=10;
y=5;
if(x>y & ++x>10)
{
System.out.println("TRUE 3 x : " + x);
System.out.println("TRUE 3 y : " + y);
System.out.println("TRUE 3 : ");
}
else
{
System.out.println("FALSE 3");
}
}
}

run:
TRUE 3 x : 11
TRUE 3 y : 5
TRUE 3 :
BUILD SUCCESSFUL (total time: 0 seconds)

public class TestSeleksi4


{
public static void main(String[] args)
{
int e,f;
e = f = 6;
if(e>5)
{
if(e==f)
System.out.println("if di dalam if Test Seleksi 3");
}
else
{
System.out.println("pasangan if terluar Test Seleksi 3");
}
} }

run:
if di dalam if Test Seleksi 3
BUILD SUCCESSFUL (total time: 0 seconds)

public class TestSwitch1 {


public static void main(String[] args)
{
int bulan = 1;
switch(bulan)
{
case 1:
System.out.println("pertama");
case 2:
System.out.println("kedua");
case 3:
System.out.println("kuartal 1");
break;
case 4:
System.out.println("keempat");
case 5:
case 6:
System.out.println("kuartal 2");
break;
case 7:
case 8:
case 9:
System.out.println("kuartal 3");
break;
default:
System.out.println("kuartal 4");
}
System.out.println("keluar dari switch");
}
}

run:
pertama
kedua
kuartal 1
keluar dari switch
BUILD SUCCESSFUL (total time: 0 seconds)

public class TestSwitch5 {


public static void main(String[] args)
{
int bulan, year;

bulan = 1;
year = 2002;
switch(bulan)
{
case 1: switch(year)
{
case 2002 :
System.out.println("Bulan 1 tahun 2002");
break;
case 2003 : //...
}
case 2: //...
default: //default...
}
}
}

run:
Bulan 1 tahun 2002
BUILD SUCCESSFUL (total time: 0 seconds)

public class TestIter1 {


public static void main(String[] args) {
int a = 9 ;
System.out.println("Sebelum while");
while(a>=10)
{
System.out.println("Nilai a : "+a);
a--;
}
System.out.println("Setelah while");
}
}

run:
Sebelum while
Setelah while
BUILD SUCCESSFUL (total time: 0 seconds)

public class TestIter2 {


public static void main(String[] args)
{
int b = 9 ;
System.out.println("Sebelum do");
do
{
System.out.println("Nilai b : "+b);
--b;
}
while (b>=10);
System.out.println("Setelah do"+b);
}
}

run:
Sebelum do
Nilai b : 9
Setelah do8
BUILD SUCCESSFUL (total time: 0 seconds)

class TestIter3
{
public static void main(String[] args) {
int c;
System.out.println("Sebelum for");
for(c=0;c<5;c++)
System.out.println("Nilai c: " + c);
System.out.println("Setelah for");
}
}

Sebelum for
Nilai c: 0
Nilai c: 1
Nilai c: 2
Nilai c: 3
Nilai c: 4
Setelah for
BUILD SUCCESSFUL (total time: 1 second)
public class TestIter4 {
public static void main(String[] args)
{
int d,e;
System.out.println("Sebelum for");
for(d=0,e=6;d<=3 && e>=3;d++,e--)
{
System.out.println("Nilai d : " + d);
System.out.println("Nilai e : " + e);
}
System.out.println("Setelah for");
}
}

run:
Sebelum for
Nilai d : 0
Nilai e : 6
Nilai d : 1
Nilai e : 5
Nilai d : 2
Nilai e : 4
Nilai d : 3
Nilai e : 3
Setelah for
BUILD SUCCESSFUL (total time: 0 seconds)

public class TestIter5 {


public static void main(String[] args)
{
int g,h;
System.out.println("Sebelum for");
for(g=0,h=6;g<=3 && h>=3;g++,h--)
{
System.out.println("Nilai g : " + g);
System.out.println("Nilai h : " + h);
}
System.out.println("Setelah for");
}

run:
Sebelum for
Nilai g : 0
Nilai h : 6
Nilai g : 1
Nilai h : 5
Nilai g : 2
Nilai h : 4
Nilai g : 3
Nilai h : 3
Setelah for
BUILD SUCCESSFUL (total time: 0 seconds)

public class TestIter6 {


public static void main(String[] args)
{
int a = 9 ;
System.out.println("Sebelum while");
while(a>=0)
{
System.out.println("Nilai a : "+a);
a--;
}
System.out.println("Setelah while");
}
}

Sebelum while
Nilai a : 9
Nilai a : 8
Nilai a : 7
Nilai a : 6
Nilai a : 5
Nilai a : 4
Nilai a : 3
Nilai a : 2
Nilai a : 1
Nilai a : 0
Setelah while
BUILD SUCCESSFUL (total time: 0 seconds)

public class TestIter7 {


public static void main(String[] args)
{
int b = 9 ;
System.out.println("Sebelum do");
do
{
System.out.println("Nilai b : "+b);
b--;
}
while (b>=0);
System.out.println("Setelah do");
}
}

run:
Sebelum do
Nilai b : 9
Nilai b : 8
Nilai b : 7
Nilai b : 6
Nilai b : 5
Nilai b : 4
Nilai b : 3
Nilai b : 2
Nilai b : 1
Nilai b : 0
Setelah do
BUILD SUCCESSFUL (total time: 0 seconds)

public class TestIter8 {


public static void main(String[] args)
{
int a = 0 ;
System.out.println("Sebelum while");
while(a>0)
{
System.out.println("Nilai a : "+a);
a--;
}
System.out.println("Setelah while");
}
}

run:
Sebelum while
Setelah while
BUILD SUCCESSFUL (total time: 0 seconds)
class TestBreak1 {
public static void main(String[] args)
{
System.out.println("Sebelum for");
for(int a=0;a<10;a++) {
if(a==4) break;
System.out.println("Nilai a : "+ a);
}
System.out.println("Setelah for"); }
}

run:
Sebelum for
Nilai a : 0
Nilai a : 1
Nilai a : 2
Nilai a : 3
Setelah for
BUILD SUCCESSFUL (total time: 1 second)

class TestBreak2 {
public static void main(String[] args)
{
System.out.println("Sebelum for");
for(int b=1;b<3;b++) {
System.out.println("Perulangan ke "+ b);
for(int c=0;c<5;c++) {
if(c==2) break;
System.out.println("Nilai c : " + c);
}
}
System.out.println("Setelah for") ;
}
}

run:
Sebelum for
Perulangan ke 1
Nilai c : 0
Nilai c : 1
Perulangan ke 2
Nilai c : 0
Nilai c : 1
Setelah for
BUILD SUCCESSFUL (total time: 1 second)

class TestBreak3a {
public static void main(String[] args) {
boolean status=true; //boolean status=false;
label1:
{
System.out.println("Di dalam blok code label1");
label2 :
{
if(status) break label1;
System.out.println("Di dalam blok code label2");
}
System.out.println("Masih di dalam blok code label1");
}
System.out.println("Di luar blok code label1");
}
}

run:
Di dalam blok code label1
Di luar blok code label1
BUILD SUCCESSFUL (total time: 1 second)

class TestBreak3b
{
public static void main(String[] args) {
//boolean status=true;
boolean status=false;
label1:
{
System.out.println("Di dalam blok code label1");
label2 :
{
if(status) break label1;
System.out.println("Di dalam blok code label2");
}
System.out.println("Masih di dalam blok code label1");
}
System.out.println("Di luar blok code label1");
}
}
run:
Di dalam blok code label1
Di dalam blok code label2
Masih di dalam blok code label1
Di luar blok code label1
BUILD SUCCESSFUL (total time: 1 second)

class TestBreak4 {
public static void main(String[] args) {
System.out.println("Sebelum for");
loop1: for(int x=1;x<3;x++) {
System.out.println("Perulangan ke " + x);
loop2: for(int y=0;y<5;y++) {
if(y==2) break loop1;
System.out.println("Nilai y : "+ y);
} System.out.println("Dalam Loop1 ");
}
System.out.println("Setelah for");
}
}

run:
Sebelum for
Perulangan ke 1
Nilai y : 0
Nilai y : 1
Nilai y : 2
Setelah for
BUILD SUCCESSFUL (total time: 1 second)

class TestBreak5 {
public static void main(String[] args) {
loopA: for(int x =0;x<2;x++) {
System.out.println("Loop A Ke " + x);
}
loop1: for(int x=1;x<3;x++) {
System.out.println("Perulangan ke " + x);
loop2: for(int y=0;y<5;y++) {
// if (y==2) break loopA;
System.out.println("Nilai y : " + y);
}

System.out.println("Akhir program");
}
}
}
}

run:
Loop A Ke 0
Loop A Ke 1
Perulangan ke 1
Nilai y : 0
Nilai y : 1
Nilai y : 2
Nilai y : 3
Nilai y : 4
Perulangan ke 2
Nilai y : 0
Nilai y : 1
Nilai y : 2
Nilai y : 3
Nilai y : 4
BUILD SUCCESSFUL (total time: 1 second)

class TestContinue6 {
public static void main(String[] args) {
int x=10;
System.out.println("Sebelum while");
while(x-->1) {
if(x % 2 == 0) continue;
System.out.println("Nilai x : " + x);
}
System.out.println("Sesudah while");
}
}

run:
Sebelum while
Nilai x : 9
Nilai x : 7
Nilai x : 5
Nilai x : 3
Nilai x : 1
Sesudah while
BUILD SUCCESSFUL (total time: 1 second)

class TestContinue7 {
public static void main(String[] args) {
int x=10,y;
label1: while(x-->0) {
y=0;
while(y++<10) {
if(y > x) {
System.out.println();
continue label1;
}
System.out.print(x*y + " "); } } }}

run:
9 18 27 36 45 54 63 72 81
8 16 24 32 40 48 56 64
7 14 21 28 35 42 49
6 12 18 24 30 36
5 10 15 20 25
4 8 12 16
3 6 9
2 4
1

BUILD SUCCESSFUL (total time: 1 second)

class TestReturn{
public static void main(String args[]) {
boolean t = true;
System.out.println("Sebelum return");
if (t) return;
System.out.println("Setelah return"); }
}

run:
Sebelum return
BUILD SUCCESSFUL (total time: 1 second)

Tugas Praktikum 5 :
1. public class CetakAngka {

/**
* @param args the command line arguments
*/
public static void main (String [] args)
{
for ( int a=0; a<=5; a++){
for (int b=4; b>=a; b--){
System.out.print(" ");
}
for (int c=1; c<=a; c++){
System.out.print("*");}
for (int d=1; d<=a-1; d++){
System.out.print("*");
}
System.out.println();
}
}
}

run:

*
***
*****
*******
*********
BUILD SUCCESSFUL (total time: 0 seconds)

2. public class BilanganPrima {


public static void main(String[] args) {
int awal = 1, akhir = 30, bil;
System.out.println("Program Penampil Bilangan Prima");
System.out.println("batas awal\t: "+awal);
System.out.println("batas akhir\t: "+akhir);
System.out.printf("Bilangan prima dari %d sampai %d adalah \n" ,awal
,akhir);
int ang=awal;
while (ang <= akhir) {
int tmp = 0;
for (int bagi = 2; bagi < ang; bagi++) {
if (ang%bagi==0) {
tmp=1;
}
}
if (tmp != 1) {
System.out.println(ang);
}
ang++;
}
}
}

run:
Program Penampil Bilangan Prima
batas awal : 1
batas akhir : 30
Bilangan prima dari 1 sampai 30 adalah
1
2
3
5
7
11
13
17
19
23
29
BUILD SUCCESSFUL (total time: 1 second)

3.public class JajaranGenjang{


public static void main(String[] args) {
for(int i = 4; i <= 7; i++){
for(int j = 4; j<i; j++){
System.out.print(" ");
}
for (int k = 0; k<=4; k++){
System.out.print("* ");
}
System.out.println();
}
}
}

run:
* * * * *
* * * * *
* * * * *
* * * * *
BUILD SUCCESSFUL (total time: 0 seconds)
4.public class SegitigaSiku{
public static void main(String[] args) {
for(int i = 1; i <= 4; i++){
for(int j = 1; j<i; j++){
System.out.print("*");
}
System.out.println("*");
}
}
}

run:
*
* *
* * *
* * * *
BUILD SUCCESSFUL (total time: 0 seconds)

5.public class Persegi{


public static void main(String[] args) {
for(int i = 1; i <= 3; i++){
for(int j = 1; j<3; j++){
System.out.print(" *");
}
System.out.println(" *");
}
}
}

run:
* * *
* * *
* * *
BUILD SUCCESSFUL (total time: 0 seconds)

You might also like