3.6 Detecting ranges with if-else statements
What is the range for the last branch below? if (numItems < 0) { ... } else if (numItems > 100) { ... } else { // Range: ______ ... }
0-100`
If the final else branch executes, what must userNum have been? Type "unknown" if appropriate. if (userNum <= 9) { ... } else if (userNum >= 11) { ... } else { ... // userNum if this executes? }
10
Type the range for each branch. Type ranges as: 25 - 29, or type 30+ for all numbers 30 and larger. if (numSales < 10) { ... } else if (numSales < 20) { // 2nd branch range: _____ ... } else if (numSales < 30) { // 3rd branch range: _____ ... } else { // 4th branch range: _____ ... } 2nd branch range: ________
10-19
Type the range for each branch. Type ranges as: 25 - 29, or type 30+ for all numbers 30 and larger. if (numSales < 10) { ... } else if (numSales < 20) { // 2nd branch range: _____ ... } else if (numSales < 30) { // 3rd branch range: _____ ... } else { // 4th branch range: _____ ... } 3rd branch range: __________
20-29
Type the range for each branch. Type ranges as: 25 - 29, or type 30+ for all numbers 30 and larger. if (numSales < 10) { ... } else if (numSales < 20) { // 2nd branch range: _____ ... } else if (numSales < 30) { // 3rd branch range: _____ ... } else { // 4th branch range: _____ ... } 4th branch range: ____________
30+
Second branch: userNum is positive (non-zero) if (userNum < 0 ) { ... } _________________________ { ... } else { // userNum is 0 ... }
else if (userNum > 0)
Second branch: userNum is greater than 105 if (userNum < 100 ) { ... } _________________________ { ... } else { // userNum is between // 100 and 105 ... }
else if (userNum > 105)
Write an if-else statement with multiple branches. If givenYear is 2101 or greater, print "Distant future" (without quotes). Else, if givenYear is 2001 or greater (2001-2100), print "21st century". Else, if givenYear is 1901 or greater (1901-2000), print "20th century". Else (1900 or earlier), print "Long ago". Do NOT end with newline. #include <iostream> using namespace std; int main() { int givenYear; givenYear = 1776; /* Your solution goes here */ return 0; }
if (givenYear >= 2101) { cout << "Distant future"; } else if (givenYear >= 2001) { cout << "21st century"; } else if (givenYear >=1901) { cout<<"20th century"; } else { cout << "Long ago"; }
Which branch will execute? Valid answers: 1, 2, 3, or none. userNum = 555; if (userNum < 0) { ... // Branch 1 } else if (userNum == 0) { ... // Branch 2 } else if (userNum < 100) { ... // Branch 3 }
none
Second branch: userNum is less than 200 if (userNum < 100 ) { ... } else if (________________________) { ... } else { // userNum >= 200 ... }
userNum < 200, or userNum <= 199