Tugas Algoritma 4C

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

 

2. What is y after the following switch statement is executed?
x = 3; y = 3;
switch (x + 3) {
case 6: y = 1;
default: y += 1;
}

 

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;

4. Use a ternary operator to rewrite the following if statement
if(x>65)
System.out.println(“Passed”);
else
System.out.println(“Failed”);

Jawaban :

 

1. Input dari x = 3 and y = 2 tidak memberikan Output, atau dengan kata lain output dilewati sehingga menghasilkan layar yang kosong.

tg1

Input dari if x = 3 and y = 4 memberikan output “z is 7”

tg2

 

Input dari if x = 2 and y = 2 menghasilkan output “x is 2”.

tg3

 

 

2. Hasil akhir y adalah y=2

 

3. switch (a) {
case 1: x+=5;
break;
case 2: x+=10;
break;
case 3: x+=16;
break;
case 4: x+=34;
break;
}

 

4.  System.out.println(x>60 ? “Passed” : “\nFailed” );

 

Binus University – www.binus.ac.id

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *