You are on page 1of 45

Review DDP

Pemrograman
Berorientasi Objek

Herika Hayurani, M.Kom. M. Fathurrachman, M.Kom.


Content by:
Image(s):
M. Faturrachman, Herika H., Ammar A., Ahimsa I., and Cesario A. P. P.
:https://www.sccpre.cat/ Layout by Aisyah D.A, A.Hisyam
:https://www.pngarts.com/ Desain by Arksnet – Desain Modified by Andreas F. 1
UNIVERSITAS
YA R S I Topics

Flow Control

Looping

2
Flow Control

3
UNIVERSITAS
YA R S I Struktur IF

if(boolean){
// aksi-aksi/statement
}

 true atau false


 boolean operator
 relational operator
 boolean + relational operator
 boolean + relational + aritmatika operator

Image(s):
https://www.kisspng.com/ 4
UNIVERSITAS
YA R S I Perilaku IF

boolean eksekusi = true; eksekusi false


true
int x = 10;
x 101
10
100
if (eksekusi) {
x *= x;
x++;
eksekusi = !eksekusi;
System.out.println(eksekusi);
}
System.out.println(x);

false
Output:

101
5
UNIVERSITAS
YA R S I Perilaku IF (2)

boolean eksekusi = false; eksekusi false


if(eksekusi){
System.out.println("masuk if");
}
else {
System.out.println("masuk else");
}

System.out.println("selesai");
Output:

masuk else
selesai
6
UNIVERSITAS
YA R S I Perilaku IF (3)

int x = 10;
x 10
if(x == 11){
System.out.println("x adalah 11");
}else if(x < 11){
System.out.println("x < 11");
}else{
System.out.println("x > 11");
}

System.out.println("selesai");

x < 11
Output:

selesai
7
UNIVERSITAS
YA R S I Soal 1

?
boolean status = true; What is the
output of given
double x = 5.10;
code snippet(s)

if(!!!status){
x = x + (short)x;
}

System.out.println(x);

8
UNIVERSITAS
YA R S I Soal 2

?
short x = 30; What is the
output of given
code snippet(s)
if(x < 10 || true && false);
x = 30 + 5;
++x;
++x;
System.out.println(x);

9
UNIVERSITAS
YA R S I Soal 3

?
String str = "Captain Marvel!"; What is the
output of given
int x = 10;
code snippet(s)
if(str.charAt(x-1) == 'r'){
x--;
}
if(x > 0 & !!false){
x -= 10;
}
System.out.println(++x);

10
UNIVERSITAS
YA R S I Soal 4

?
short x = 25; What is the
output of given
if(x / 5 == 5){
code snippet(s)
if(x > 25){
x += 10;
}else{
x -= 10;
}
}else{
System.out.println(x++);
}

11
UNIVERSITAS
YA R S I Soal 5

?
int eksekusi = 1; What is the
output of given
String str = "PBO";
code snippet(s)

if((int)(double)eksekusi == 1){
str = "String";
}
else{
str = "ubah str";
}
System.out.println(str);

12
UNIVERSITAS
YA R S I Soal 6

?
String str = "Kok, menyenangkan!"; What is the
output of given
int x = 10;
code snippet(s)
if(str.charAt(x) == 'e'){
x--;
} else if(x > 0){
x -= 10;
}System.out.println(x);

13
Switch-case

14
UNIVERSITAS
YA R S I Switch Structure

switch(expression){
case value: Data must be
// other statements comparable
break;

case value: Break out of the scope


// other statements
break;
Everything else
default:
// other statements
}
15
UNIVERSITAS
YA R S I Switch’s Behavior

byte i = 10;
i 11
10
switch(i % 3){
case 1:
i += 1; Output : 11
break;
case 2:
i += 2;
break;
default:
i += 3;
}
System.out.print(i);

16
UNIVERSITAS
YA R S I Soal 7

?
byte i = 17; What is the
output of given
switch(i % 3){ code snippet(s)
case 1:
i += 1; break;
case 2:
i += 2; break;
}
System.out.print(i);

17
UNIVERSITAS
YA R S I Soal 8

?
byte i = 27; What is the
output of given
switch(i % 3){ code snippet(s)
case 1:
i += 1; break;
case 2 + 2:
i += 2; break;
default:
i += 3;
}
System.out.print(i);

18
UNIVERSITAS
YA R S I Soal 9

double i = 100.0;

switch(i){
Identify the
error(s) in given
code snippet(s) !
case 10 * 10:
System.out.println("true");
break;
case 10:
System.out.println("false");
}
System.out.print(i);

19
UNIVERSITAS
YA R S I Soal 10

?
int i = 200; What is the
output of given
code snippet(s)
switch(i){
default:
System.out.println("TwoFourEight");
break;
case 10:
System.out.println("Ten");
break;
}
System.out.print(i);

20
UNIVERSITAS
YA R S I Soal 11

long i = 100;

switch(i){
Identify the
error(s) in given
code snippet(s) !
case 100 * 10:
System.out.println("A Thousand");
break;
default:
System.out.println("????");
break;
}
System.out.print(i);

21
While Loop

22
UNIVERSITAS
YA R S I While-Loop Syntax

while(kondisi) {
pernyataan(s)
}
Example:

while(true && 4 * 10 > 30){


System.out.println("looping");
}

23
UNIVERSITAS
YA R S I While Loop’s Behaviour

String str = "!eciN"; str !eciN


!ec
!eci
!e!
String result = ""; result Nice!
Nice
Ni
N
Nic

while(!str.isEmpty()){
result += str.charAt(str.length() - 1);
str = str.substring(0, str.length() - 1);
}

System.out.println(result);
Output:

Nice!
24
UNIVERSITAS
YA R S I Soal 12

?
int number = 1; What is the
output of given
while(number <= 10 & true){
code snippet(s)
System.out.println(number%2);
number++;
}

25
UNIVERSITAS
YA R S I Soal 13

?
String str = "ala"; What is the
output of given
boolean check = false;
code snippet(s)
while(check){
str = "o" + str; break;
}
System.out.print(str + ((!check)? "la" : "lo"));

26
UNIVERSITAS
YA R S I Soal 14
String str = "xwxjxxjxxaxjjjxxkajjnxdxxa!x"; What is the
int temp = 0;
while(str.length() != temp){
switch(str.charAt(temp)){
output of given
code snippet(s) ?
case 'j':
case 'x':
break;
default:
System.out.print(str.charAt(temp));
}
temp++;
}

27
UNIVERSITAS
YA R S I Soal 15

String str = "huh?";

while(true||false){
Identify the
error(s) in given
code snippet(s) !
str = "kenapa?";
System.out.println(str);
}

28
UNIVERSITAS
YA R S I Soal 16
String str = "xxxaaa";

while(!str.equals("aaa")){
while(!str.isEmpty()){
Identify the
error(s) in given
code snippet(s) !
str = str.substring(1, str.length());
}
str += "a";
}
System.out.print(str);

29
For Loop

30
UNIVERSITAS
YA R S I Penulisan FOR-LOOP

boolean
for(initialization; termination;
increment/decrement){

statement(s)
}
Example:

for(int i = 0; i <= 10; i++){


System.out.println(i);
}
31
UNIVERSITAS
YA R S I For-Loop Behaviour

int sum = 0; sum 0


1
3
6

for(int i = 0; i <= 3; i++){ i 0


1
2
3
4
System.out.print(i + " ");
sum += i;
}

System.out.print("\nSum is " + sum);

0 1 2 3
Output:

Sum is 6
32
UNIVERSITAS
YA R S I Soal 17

?
int sum = 0; What is the
output of given
code snippet(s)
for(int i = 4; i >= 0; i--){
sum += i%10;
}

System.out.println("The numbers: " + sum);

33
UNIVERSITAS
YA R S I Soal 18
String all = "";
long i = 0;

for(; i <= 5; i--){


Identify the
error(s) in given
code snippet(s) !
all += i;
}

System.out.println("The numbers: " + all);

34
UNIVERSITAS
YA R S I Soal 19

?
String all = ""; What is the
output of given
boolean check = true; code snippet(s)

for(int i = 1; !check; i++ ){


all += i;

if(i == 4)
check = !false;
}
System.out.println("The numbers: " + all);

35
UNIVERSITAS
YA R S I Soal 20

?
for(int i = 1; i <= 5; i++){ What is the
output of given
code snippet(s)
for(int j = 1; j <= i; j++){
System.out.print("*");
}

System.out.print("\n");
}

36
UNIVERSITAS
YA R S I Soal 21

?
int count = 0; What is the
output of given
for(int i = 0; i < 3.5; i++){ code snippet(s)
if(i <= 1){
for(int j = 0; j < 3.5; j++){
count++;
}
}
}
System.out.print(count);

37
Do-While

38
UNIVERSITAS
YA R S I Do-While-Loop Syntax

do{
statement(s)
}while(boolean);
Example:

do{
System.out.println("looping");
}while(true && 4 * 10 > 30);

39
UNIVERSITAS
YA R S I Do-While Loop Behaviour

String str = "binaan biasa";


str binaan biasa
bisa
sabi
String result = "";
result sabibisasabi
bisasabi
sabi

do{
str = str.substring(str.length() - 2,
str.length()) + str.substring(0, 2);
result = str + result;
}while(!result.startsWith("bibi", 2));

System.out.println(result);
Output:

sabibisasabi
40
UNIVERSITAS
YA R S I Soal 22

?
String result = ""; What is the
int index = 1; output of given
code snippet(s)

do{
result += index;
index--;
}while(index == 0);

System.out.print(result);

41
UNIVERSITAS
YA R S I Soal 23
String str = "!umak ,labmog rasad";
String result = "";
int index = str.length() - 1;
Identify the
error(s) in given
code snippet(s) !
do{
result += str.charAt(index);
index++;
}while(!result.endsWith(","));

System.out.print(result);

42
UNIVERSITAS
YA R S I Soal 24

int count = 1;
boolean check = true;
Identify the
error(s) in given
code snippet(s) !
do{
System.out.print(count % 10);
count++;
check = check != !check;
}while(check);

43
UNIVERSITAS
YA R S I Soal 25

?
String result = ""; What is the
int index = 0; output of given
code snippet(s)

do{
do{
result += "*";
index++;
}while(index < 2);
result += "\n";
}while(index < 2);
System.out.print(result);

44
Any
Question(s)?

Image(s):
https://www.animaker.com/ 45

You might also like