Tugas Mandiri Algoritma

Mahasiswa Yang Berencana, Dosen Yang Menentukan.

Tugas Mandiri 04C

March28

1. Suppose x = 3 and y = 2, show the output, if any, of the following code.What is the output if x = 3 and y = 4? What is the output if x = 2 and y = 2?

if (x > 2) {

  if (y > 2) {

    int z = x + y;

}  }    System.out.println(“z is ” + z);

else

  System.out.println(“x is ” + x);

Output dari x = 3 & y = 2 tidak memberikan output

Hasil Dari Textpad

Output dari x = 3 & y = 4 memberikan output “z is 7”

Hasil Dari Textpad

Output dari x = 2 & y = 2 memberikan output “x is 2”

Hasil Dari Textpad

2. What is after the following switch statement is executed ?

x = 3; y = 3;
switch (x + 3) {
case 6: y = 1;
default: y += 1;
}

Hasil y adalah  “y = 2”

Hasil Dari Textpad

3. Use a switch statement to rewrite the following if statement:?

if (a ==1)
x +=5;
else if (a == 2)
x +=10;
else if (a == 3)
x +=16;
else if (a == 4)
x += 34;

Hasil dari if statement yang dirubah menjadi switch statement

Hasil Dari Textpad

4. Use a ternary operator to rewrite the following if statement

if(x>65)
System.out.println(“Passed”);
else
System.out.println(“Failed”);

Hasil dari Nilai 100 “Passed”

Hasil Dari Textpad

Hasil dari Nilai 65 “Passed”

Hasil Dari Textpad

Hasil dari Nilai 50 “Failed”

Hasil Dari Textpad

www.binus.ac.id

Email will not be published

Website example

Your Comment: