Warm-up Exercises
-
Write a program that performs the following steps:
-
First ask user to enter any number between 0 and 10
-
Depending on the user’s answer, do the following:
When user enters number 7, display the following message:
When user enters a number that is 5 or above but not 7, display the following message:
When user enters a number that is below 5, display the following message:
Solution
-
Explain the meaning of the switch mechanism including the meaning of break and default. Give a small example.
Solution
A switch statement will execute code attached to a specific case determined by the value of a variable. The default keyword is used when defining the case in which the value of the determining variable does not match the value attached to any of the other cases.
Example
int i = 0
switch(i)
{
case 1: break;
case 2: break;
default: break;
- Identify lines and cause of syntax errors preventing this code from executionrecursion
Solution
In line 7, there is a semicolon in between the method header and body. In line 11, there is period in assignment statement.
- Assume you are testing two positive (>0) integer numbers X and Y. What property is tested by during call of method E(X, Y) in the code given below?
Solution
The method E(X, Y) tests whether X and Y are equal.
- Assuming a string variable
name
was declared and initialized with a value given by the user, write an if statement that displays “I have the same name!” ifname
matches your first name.
Solution
- Write an
if-else
statement that displays “It’s free for you!” if an int variableage
is between 0 and 18, and “It’s $5.00.” otherwise.
Solution
- Write a switch statement that sets a double variable
discount
to 0.5 if a string variableday
contains “Monday” or “Wednesday”, 0.25 ifday
contains “Saturday”, and 0.125 otherwise.
Solution
- (We’ll use the 24-hour clock, sometimes called “military time”.)
Assuming that an int variable
hours
has been initialized, write part of a program that would display on the screen “Good morning” ifhours
is less than or equal to 12, and “Hello” otherwise.
Solution
- Assuming that
myString
is a string variable, write a statement that print “Hello, Mélodie!” if the value ofmyString
is equal to “Mélodie”, and nothing otherwise.
Solution
- Write a program that asks the user to enter a value between 0 and 10, and asks again as long as the user enters integers outside that range.
Solution
- Write a small program that asks the user for an integer, and displays “It is positive” if the number entered is positive, “It is negative” if the number entered is negative, and “Not a number” if the user entered a string that is not an integer.
Solution
- Write a program that asks the user to enter a value between 1900 and 1999 (both included), and asks again as long as the user enters integers outside that range.
Solution
Questions
- Assume that A, B, and C are integer variables. What property on A, B, and C is tested by this code? Provide short justification.
Solution
This code tests whether or not the 3 variables are equal in value. Integers A and B are checked for equality before their values are incremented and decremented respectively. Then, B is incremented back to its original value and subsequently checked for equality with C.
- The if-statement allows a program to run a piece of code based on the truth of some condition.
-
Yes
-
No
Solution
This code tests whether or not the 3 variables are equal in value. Integers A and B are checked for equality before their values are incremented and decremented respectively. Then, B is incremented back to its original value and subsequently checked for equality with C.
- A(n) _____ statement allows a program to make a decision based on the truth or falsity of some condition.
- if
- test
- logic
- None of the above.
- The three types of selection structures are:
- foreach, for and switch
- if, for and switch
- if, if-else and while
- if, if-else and switch.
- In a switch statement, a case can be labeled as _____ so that it executes in the event that none of the provided cases are equivalent to the controlling expression.
- general
- default
- case *
- None of the above.
- What is sequential processing?
Solution
When code is executed sequentially, without any branching.
- What is a decision structure?
Solution
When portions of code are executed conditionally.
- Determine if the following boolean expressions evaluate to
true
orfalse
: true || 3 > 4
'A' == 'C' || ! false
(4 != 3) && true
(true && 4 >= 3) == false
Solution
- true
- true
- true
- false
- What is the relational operator used to determine whenever two values are different?
Solution
!=
- What is a flag?
Solution
A boolean variable.
- Give three relational operators, and then two logical operators.
Solution
⇐, ==, >, and !, &&
- What would be displayed on the screen by the following code?
Solution
“Hi!”
- Is there a simpler way to write the expression
over21 == true
, assuming thatover21
is a boolean variable?
Solution
if(over21)
- Assume that
x
andy
are two int variables that have already been initialized (i.e. declared and assigned), write an if statement that assigns 10 tox
ify
is (strictly) greater than 5.
Solution
if (y > 5) {x = 10;}
In C#, is there a difference between =
and ==
? Write a statement
that uses =
.
Solution
Yes, one is to assign, the other is to compare. if (x == 9){x = 12;}
- Is the following statement correct, i.e., would it compile, assuming
myFlag
is a bool variable, andmyAge
is an initialized int variable?
Solution
No, the semi-colon should come before the }
.
- Write an if statement that prints “Bonjour!” if the value of the char variable lang is ‘f’.
Solution
if (lang == 'f'
) { Console.WriteLine(“Bonjour !”); }
- For each of the following boolean expressions, determine if it will
evaluate to
true
orfalse
when the boolean variablesx
,y
, andz
are all set to true:x || y && z
!x || y && z
!(x || y) && (z && y)
(!x && x) || (x! || x)
Do the same when they are all set tofalse
.
Solution
- true, false
- true, true
- false, false
- false, false
- Write a boolean expression that evaluates to
true
if a variablex
is between 3 (excluded) and 5 (included).
Solution
x>3 && 5>=x
- Write an if-else statement that assigns 0.1 to
z
ify
is greater than or equal to 0, and that assigns -.01 toz
otherwise.
Solution
if (y >= 0){z = 0.1;} else {z = -0.1;}
- What will be displayed on the screen by the following program?
Solution
“x is 3, y is 2, and z is 6.”
- What will be displayed on the screen by the following program?
Solution
“x is 3, y is -2, and z is 8.”
- What will be displayed on the screen by the following program?
Solution
“x is 3, y is 2, and z is 7.”
Problems
- Write a program that decides whether you should sleep or not, based on whether you are tired and whether you have to work.
Solution
- Rewrite, if possible, the three following if-else-if statements as switch statements:
If you think it is not possible or not feasible, explain why.
Solution for Part 1
Solution for Part 2
Solution for Part 3
Impossible: can’t write switch comparing all possible float values.
- Write a program that asks the user to write a country name and stores the user’s input into a string variable. Then, compare that string with “france”: if it is equal, then print “Bienvenue en France!”. Then, compare that string with “usa”: if it is equal, then print “Welcome to the US!”. If the string is different from both “france” and “usa”, then print “Welcome to” followed by the name of the country the user typed in. Can you think of two ways to implement this program, one using if-else-if statements, the other using switch?
Solution for if-else-if Statement
Solution for switch Statement
- You want to write a small program for an on-line printing company.
Your program should ask the user to choose a format (10 x 15
centimeters, or 8 x 1 inches), ask if it is the first time the
customer has ordered through your company, and the number of copies
to print. Then, calculate the total cost of printing those pictures,
knowing that
- Printing a 10 x 15 -centimeter picture costs $0.20, printing a 8 x 11-inch picture costs $0.25
- A new customer gets a $3 coupon if the order is more than $5
- A 10% discount is given if more than 50 copies were ordered
- The two previous offers can be cumulated. Display on the screen a message starting with “Welcome!”, then a new line, then “We cherish our new customers” if it is the first time the user uses your company, “, so we’re giving you a $3 coupon!” if the user is allowed to get the coupon, then print the total and “You get a 10% discount!” if the user ordered more than 50 copies.
Solution
- Write a program that asks the user to enter an integer between 0 and 100, and asks again until user enters one of these numbers (0, 1, …, 99, or 100).
Solution
- You are writing a program for an ice-cream shop. At this ice cream shop customers have many options of what kind of ice cream they might get. The price of the ice cream depends on the options they choose.
-
First ask if they would like hard ice cream ($1.50), soft-serve ice cream ($1.00), or rolled ice cream ($2.00).
-
If user chooses rolled ice-cream, it will always be served in a cup ($0.50). Otherwise, ask user if they would like an ice cream in a cup ($0.50) or a waffle cone ($0.65).
-
Then ask user if they would like sprinkles. Only if they say yes add $0.20 to price.
-
Lastly, display the final calculated ice cream price to the user.
Solution
- Write a program that:
- Asks the user to enter a positive number between 0 and 100,
- Asks again if user does not enter a number in this range,
- Does not crash on bad user input, i.e. when input is not a number at all. (optional)
Example:
Solution
- Write a program that will:
- Ask the user to enter a positive number. Make sure user enters a number, and that that number is positive before you proceed with the next steps.
- Find the largest number that is a multiplicand of 7, between 0 and the number user entered. Multiplicands of 7 are e.g. 7, 14, 21, 28, 35, 42, 49….. you want to find the one that is the closest, but not greater than the number user entered.
- Display the result on the screen.
There is an edge case: If user enters a number that is less than seven, there is no answer. In that case the program should display appropriate output, such as “number you entered is less than 7”.
Sample Output
After you finish the program, test it with different numbers. For example:
- input 56 result is 56
- input 167 result is 161
- input 72364279 result is 72364278
Solution
- Write a program that asks the user to enter a value between 0 and 10, and asks again as long as the user enters integers outside that range.
Solution
- Write a program that asks user to select an option A, B, or C. If user does not enter a valid selection, repeat the question until they do.
Solution
- Ask user to enter exactly 3 numbers (integers). Check that user enters valid numbers for each 3 inputs. Lastly display those numbers in order from largest to smallest. For example, if user enters numbers 16, 42 and 37, the program should output 42 37 16.