boolean rsvp;
int selection;
String option1;
String option2;
(a) if(rsvp){
System.out.println("Attending");
} else {
System.out.println("Not attending");
}
(b) if (selection == 1){
System.out.println("Beef");
} else if (selection == 2){
System.out.println("Chicken");
} else if (selection == 3) {
System.out.println("Pasta");
} else {
System.out.println("Fish");
}
(c) switch(selection){
case 1:
if(rsvp){
option1 = "Thanks for attending. You will be served beef.";
} else{
option1 = "Sorry you can't make it";
}
break;
case 2:
if(rsvp){
option1 = "Thanks for attending. You will be served chicken.";
} else{
option1 = "Sorry you can't make it";
}
break;
case 3:
if(rsvp){
option1 = "Thanks for attending. You will be served pasta.";
} else{
option1 = "Sorry you can't make it";
}
break;
default:
if(rsvp){
option1 = "Thanks for attending. You will be served fish.";
} else{
option1 = "Sorry you can't make it";
}
break;
}
(d) if (option1 == option2){
System.out.println("True");
} else {
System.out.println("False");
}
Explanation:
This code snippet is written in Java.
a) Uses if statement to print "attending" if rsvp is true and "not attending" if rsvp is false.
b) Uses if-else statement to print the variety of food based on user selection. If selection is 1, it print beef. If selection is 2, it print chicken. If selection is 3, it prints pasta else it print fish for any other value of selection.
c) Uses switch statement to determine the value of selection and then uses inner if statement to assign "Thanks for attending. You will be served beef." if rsvp is true to option1. The value of beef changes depending on the value of selection. If rsvp is false, then "Sorry you can't make it." is assigned to option1.
d) Uses if statement to print true if option1 and option2 are equal else it print false.