1 Chapter 4 Selection Statements A Review A 1 What do we mean “selection” in selection statements? Introduce at least three use contexts of each kind of selection statements A 2 When should we use if[.]
Trang 11
Chapter 4: Selection Statements
A Review
A.1 What do we mean “selection” in selection statements? Introduce at least three use contexts of each kind of selection statements
A.2 When should we use if else statements instead of conditional expressions? Give an example for
an illustration
A.3 What is a multi-way decision? Give an example with if else statements and another one with switch case statements
A.4 When should we use if else statements instead of switch case statements? Give an example for
an illustration
A.5 What is the “fall-through” property of switch case statements? Give an example to illustrate this property
B Practice
B.1 Write selection statements that might be used in the solutions of the problems B.1.1 to B.1.15 in Chapter 1 Remember to write variable declaration statements before the selection statements if there
is any variable in these selection statements
B.2 For each following description, write a corresponding selection statement
B.2.1 If the norm of any vector is zero, let the user know that it is unable to find an angle between that vector with the other
B.2.2 Increase the unit price by 10 if the stock level goes below 5
B.2.3 Double the unit price if the stock level is 1 Otherwise, decrease the stock level by 1 and increase the unit price by its half
B.2.4 If a date is 13 and its day is Friday, inform that the day is an unlucky day Otherwise, it is informed
as a normal day
B.2.5 If time is morning, introduce “orange, lemon, apple, pineapple” If time is noon, introduce
“watermelon, apple, grape, coconut, banana” If time is evening, introduce “plum, durian, melon, mango” If time is night, introduce “peach, pear, avocado”
B.2.6 If the user chooses “open a file”, the user is given a form to enter a file name If the user chooses
“exit the program”, a prompt about saving the current status of the user is given to the user If the user chooses “save”, the current status of the user is saved and the program is closed Otherwise, the current status of the user is deleted and the program is closed
Trang 2B.2.7 If you consume from more than 0 to 10 KW, the lowest price is applied If you consume from 10 to
50 KW, the second lowest price is applied If you consume from 50 to 100 KW, the second highest price
is applied If you consume more than 100 KW, the highest price is applied And of course, if you consume
0 KW, there is no charge
B.2.8 If a week is the first week, introduce the course, start the first chapter, and do no homework If a week is the second one, continue the first chapter and start doing homework If a week is the third one, start the second chapter, review the first chapter, and do homework If a week is the fourth one, continue the second chapter and do homework If a week is the fifth one, start the third chapter, review the second chapter, start the assignment, and do homework If the week is the sixth one, continue the third chapter, keep working on the assignment, and do homework If the week is the seventh one, start the fourth chapter, review the third chapter, keep working on the assignment, do homework, and take a break
B.3 Identify which statement is correct and which is not For those incorrect statements, make appropriate changes so that they become correct After all, describe the output of each statement B.3.1
B.3.2
Trang 33 B.3.4
B.3.5
Trang 4B.4 Rewrite the following conditional expressions using appropriate if /if else statements If those statements are executed, what will be on screen?
B.4.1
B.4.2
B.4.3
B.4.4
Trang 55
B.5 Rewrite the following switch case statements using appropriate if /if else statements If those statements are executed, what will be on screen with different values of the input variable?
B.5.1
B.5.2
Trang 6B.5.3
Trang 77
B.6 In your assignment, you might have a menu listing several options to have access to your program The following are some options:
- Populate data
- Query data
- Make simple queries
- Make advanced queries
- Update data
- Insert a new item
- Modify an existing item
- Delete an existing item
- Simulate a control flow
- Exit
Write a program to generate a menu with the aforementioned options in a similar format shown above Ask a user about what he/she would like to proceed Display what he/she has chosen After that, say
“Thank you!” before asking he/she to press Enter for closing the program It is supposed that a user can key what he/she can Therefore, make sure that you check his/her input carefully and appropriately B.7 Write a program to ask a user to input 2 opposite vertices of a 4-sided polygon in an R2 space Display the information about a given 4-sided polygon with a header Polygon’s model \t Area \t Type
Trang 8Area column is with right justification and the others with left justification Examples are given as follows:
Polygon’s model Area Type
Or:
Polygon ‘s model Area Type
Rectangle 80 small-sized
Or:
Polygon’s model Area Type
Invalid 0 none
Polygon’s model is Square, Rectangle, or Invalid If an area of a polygon is zero, such a polygon has an invalid polygon’s model and its type is none If an area of a polygon is in (0, 100), its type is small-sized If
an area of a polygon is in [100, 10000), its type is medium-sized Otherwise, its type is large-sized
It is supposed that a user can key what he/she can Therefore, make sure that you check his/her input carefully and appropriately
B.8 Write a program to ask a user to input 3 vertices of a triangle in an R2 space Check if a given triangle
is an equilateral, isosceles, squared, squared isosceles, normal, or invalid triangle An invalid triangle is a triangle with no area, i.e 3 vertices lie on the same straight line Print the information of a given triangle along with its type using left justification as follows:
The vertices that have been input:
Trang 99
is standardized with (1000, 10, 100) corresponding to the aforementioned 3 attributes and a non-valued customer with (10, 1, 1) and a potentially valued customer with (500, 5, 50) Given at least 3 customers each of whom has 3 input attribute values, determine if each of them is a valued customer, a non-valued customer, or a potentially non-valued customer based on the most similarity he/she has to a corresponding standardized customer The Euclidean distance can be used to express similarity between
a given customer and a standardized customer The smaller Euclidean distance implies the more similarity Display the result in the form of a table with a header such as: CustomerID \t BoughtItem# \t RegisteredService# \t ShoppingTime# \t Standardized In the resulting table, BoughtItem#, RegisteredService#, and ShoppingTime# columns are with right justification and the others with left justification An example is given below
Customer grouping:
CustomerID BoughtItem# RegisteredService# ShoppingTime# Standardized
1 1200 9 120 Valued
2 12 0 2 Non-valued
3 205 6 50 Potentially valued Euclidean distance between the first customer and the standardized valued customer is calculated as follows: ( 1200 1000 )2 ( 9 10 )2 ( 120 100 )2 201
It is supposed that a user can key what he/she can Therefore, make sure that you check his/her input carefully and appropriately
B.10 Check your coding style with the programs you have written Should anything be changed with
your programs so that they can be not only executable but also readable? Rewrite your programs if yes
NOTE: none of for, while, do while statements is allowed to be used in your programs in this chapter