Chapter 4.14: The switch statement – Flashcards
Unlock all answers in this set
Unlock answersquestion
HTTP is the protocol that governs communications between web servers and web clients (i.e. browsers). Part of the protocol includes a status code returned by the server to tell the browser the status of its most recent page request. Some of the codes and their meanings are listed below: 200, OK (fulfilled) 403, forbidden 404, not found 500, server error Given an int variable status, write a switch statement that prints out the appropriate label from the above list based on status.
answer
switch( status ){ case 200: cout << "OK (fulfilled)"; break; case 403: cout << "forbidden"; break; case 404: cout << "not found"; break; case 500: cout << "server error"; break; }
question
Assume that grade is a variable whose value is a letter grade-- any one of the following letters: 'A', 'B', 'C', 'D', 'E', 'F', 'W', 'I'. Assume further that there are the following int variables , declared and already initialized : acount, bcount, ccount, dcount, ecount, fcount, wcount, icount. Write a switch statement that increments the appropriate variable (acount, bcount, ccount, etc.) depending on the value of grade. So if grade is 'A' then acount is incremented ; if grade is'B' then bcount is incremented , and so on.
answer
switch (grade) { case 'A': acount++; break; case 'B': bcount++; break; case 'C': ccount++; break; case 'D': dcount++; break; case 'E': ecount++; break; case 'F': fcount++; break; case 'W': wcount++; break; case 'I': icount++; break; }
question
Write a switch statement that tests the value of the char variable response and performs the following actions: if response is y, the message Your request is being processed is printed if response is n, the message Thank you anyway for your consideration is printed if response is h, the message Sorry, no help is currently available is printed for any other value of response, the message Invalid entry; please try again is printed
answer
switch(response){ case 'y': cout << "Your request is being processed"; break; case 'n': cout << "Thank you anyway for your consideration"; break; case 'h': cout << "Sorry, no help is currently available"; break; default: cout << "Invalid entry; please try again"; break; }
question
Given a int variable named yesCount and another int variable named noCount and a char variable named response, write the necessary code to read a value into intoresponse and then carry out the following: if the character typed in is a y or a Y then increment yesCount and print out "YES WAS RECORDED" if the character typed in is an n or an N then increment noCount and print out "NO WAS RECORDED" If the input is invalid just print the message "INVALID" and do nothing else.
answer
cin >> response; switch (response) { case 'Y': case 'y': cout << "YES WAS RECORDED"; yesCount++; break; case 'N': case 'n': cout << "NO WAS RECORDED"; noCount++; break; default: cout << "INVALID"; break; }
question
In the Happy Valley School System, children are classified by age as follows: less than 2, ineligible 2, toddler 3-5, early childhood 6-7, young reader 8-10, elementary 11 and 12, middle 13, impossible 14-16, high school 17-18, scholar greater than 18, ineligible Given an int variable age, write a switch statement that prints out the appropriate label from the above list based on age.
answer
switch (age) { case 0: case 1: cout << "ineligible"; break; case 2: cout << "toddler"; break; case 3: case 4: case 5: cout << "early childhood"; break; case 6: case 7: cout << "young reader"; break; case 8: case 9: case 10: cout << "elementary"; break; case 11: case 12: cout << "middle"; break; case 13: cout << "impossible"; break; case 14: case 15: case 16: cout << "high school"; break; case 17: case 18: cout << "scholar"; break; default: cout << "ineligible"; break; }