Write a while loop with a sentinel value This while loop ends when the user enters a
Question:
Write a while loop with a sentinel value
This while loop ends when the user enters a 1, the sentinel value
Assign the number 8 to a variable which will serve as the sentinel value
Condition: while variable is not equal to 1
Action: display the number assigned to the variable
Use an input statement (no prompt) to ask the user for a number and assign this number to the sentinel value variable
Convert the input to an integer and assign the converted number to the same variable
After the while loop ends, display the following statement on screen: All done!
Exercise 2
Write a while loop with a sentinel value
#This while loop ends when the user enters a zero, the sentinel value
#Use an input statement to ask the user for an initial value (no prompt) and assign it to a sentinel value variable
#Convert the variable to an integer and assign the converted number to the same variable
#Condition: while the variable is not equal to 0
#Action: display the number assigned to the variable
#Ask the user for a number by using an input statement (no prompt) and assign this number to the sentinel value variable
#Convert the variable to an integer and assign the converted number to the same variable as well
#After the while loop ends, display the following statement on screen: Goodbye!
#Exercise 3
#Note: in this exercise you need to keep track of 2 variables, one assigned the sentinel and another assigned a song
#This time you will use a string as the sentinel value. This while loop ends when the user enters an n
#Initialize your sentinel value by assigning the string y to a variable that will store the sentinel value
#Condition: while the sentinel variable is not equal to 'n'
#Ask the user for a song title by using an input statement with the prompt: Song? and assign the input to a new variable
#Display the user entered song on screen
#Ask the user for the sentinel value with an input statement with the prompt: Continue (y/n)?
#and assign value to the variable assigned the sentinel
#After the while loop ends, display the following on screen: Thank you for playing
#Exercise 4
#This time you will again use a string as the sentinel value. This while loop ends when the user enters a y
#Note: in this exercise you need to keep track of 2 variables, one assigned the sentinel and another assigned a grocery item
#Initialize your sentinel value by assigning the string n to a variable to store the sentinel
#Condition: while sentinel variable is equal to 'n'
#Ask the user for a grocery item by using an input statement with the prompt: item? and assign the input to a new variable
#Display the user entered item (variable name g) on screen in the following format:
#Your grocery item is: [grocery item] (Tip, you'll need a concatenator)
#Ask the user for the sentinel value with an input statement with the prompt: Stop (y/n)?
#and assign value to variable assigned the sentinel
#After the while loop ends, display the following on screen: Thank you for visiting
#Exercise 5
#This uses a string as the sentinel value and collects user entries to be printed out at the end of the while loop
#1. Initialize your sentinel value by assigning the string c to a variable (this is your sentinel)
#2. Initialize another variable to collect user entries as an empty string, for example x = " " [space between the quotes]
#3. Condition: while sentinel is equal to 'c'
#4. Ask the user for an item name by using an input statement with the prompt: item? and assign the input to a new variable
#5. Concatenate the item to the variable containing the space (initalized in instruction 2, e.g. x = x+' '+item)
#6. Ask the user for the sentinel value with an input statement (assign to sentinel) with the prompt:
#7 c to continue x to exit: (no space after colon)
#8. After the while loop ends, print the following statement, substituting the variable updated w instruction 5
#9. (use the + or , concatenator):
#10. These are your items: [variable updated w instruction 5]
#11. Display the following as the last line: Thank you for shopping.
Microsoft Visual C# An Introduction to Object-Oriented Programming
ISBN: 978-1337102100
7th edition
Authors: Joyce Farrell