October 27, 2020
For each of the following boolean expressions, decide if it will evaluate to
trueorfalse:
true || 3 != 44 >= 4 && ! false('c' == 'd') && true(true && 4 >= 3) == false
This problem is left as an exercise. If you want to check your answers, you can use e.g
Write an if-else statement that displays “It’s free for you!” if an int variable
ageis between 0 and 18.
Assuming a
namestring was declared and initialized with a value given by the user, write an if statement that displays “I have the same name!” if name contains your first name.
Assume initialized an int variables called
graduationYearand a string variable calledgraduationSemester. Write a series of statements that would display “I will graduate at the same time!” ifgraduationYearis 2023 andgraduationSemesteris Fall, “I love this season.” ifgraduationSemesteris Winter, “That’s in a long time!” ifgraduationYearis greater than 2025, and “I hope you’ll have in person ceremony!” otherwise. Your program should display exactly one message.
if(graduationYear == 2023 && graduationSemester == "Fall"){
Console.WriteLine("I will graduate at the same time!");
}
else if (graduationSemester == "Winter"){
Console.WriteLine("I love this season!");
}
else if (graduationYear > 2025){
Console.WriteLine("That's in a long time!");
}
else{
Console.WriteLine("I hope you'll have in person ceremony!");
}Assume initialized an int variables called
courseNumberand a string variable calledcourseCode. Write a series of statements that would display “I’m taking this class!” ifcourseNumberis 1301 andcourseCodeis CSCI, “That’s my major!” ifcourseCodeis CSCI, “Is that an elective?” ifcourseCodeis greater than 3000, and “Is it a good class?” otherwise. Your program should display exactly one message.
You can take inspiration from the answer posted for Section A to figure this one out.
Give an example of an
ifstatement that could not be rewritten as aswitch.
Basically anything using an operator that is not == could work. For instance,
Or anything involving multiple variables could work too (even if it could be mimicked by nested switch in some cases):