Here are a few questions to help you clarify your understanding of basic branching — if, if/else, and even if/else if/else.
What is wrong in the following code? Be sure to tell what kind of error it is you've found... (You can assume that all variables are declared and initialized. And, yes, they are also named 'decently enough'™ for our purposes here.)
if (c > 4); { z = 4 * y; }; else; { z = z * 8; };
The semi-colons after the if condition, after the else keyword, and after the close braces are all extraneous. The first three will actually cause errors. The last is merely excessive.
How does a 2-way branch differ from a 1-way branch?
A 2-way branch has an else clause but a 1-way branch simply has an if.
What sense does a 1-way branch make, anyway!? Do you have any examples of what one [pardon the pun] might be used for?
A 1-way branch is useful when we have to perform a special action which has no alternative. For instance, pluralization typically requires that we add an s to a noun's singular form. Not being plural, however, is just the singular form — no extra s attached: cout << "Sentence about " << count << " noun"; if ( count != 1 ) { cout << 's'; } cout << ".\n"; The singular form has already been displayed and an s is only displayed when plural form is required.
TRUE✓ | FALSE✗ | When one branch is nested inside the else clause of another, we might be able to replace it with a cascaded structure. | ||
---|---|---|---|---|
TRUE✗ | FALSE✓ | For this to work, there must be some common operations that execute no matter which nested branch is taken. | ||
TRUE✓ | FALSE✗ | Cascading provides a cleaner, more compact style than nesting. | ||
TRUE✗ | FALSE✓ | We can also use cascading when a branch is nested inside another branch's if part. |
Recalling the cctype functions for classifying characters into groups, write a branching structure which can classify a character entered by the user into three (or more) of the following categories: letter, number, punctuation, alpha-numeric, hexadecimal digit, lowercase, or uppercase.
For example, if the user typed the character 'e', you might report to them that their character was a lowercase letter, alpha-numeric, and a hexadecimal digit. But if they type in a ',', you'd only be able to say it was punctuation.
cout << "The character '" << ch << "' is "; if ( isalnum(ch) ) { cout << "alpha-numeric (specifically "; // could pre-pend an if to check for hexidecimal digits if ( isalpha(ch) ) { // could also do isupper and islower tests here to classify as // capital or lower-case cout << "alphabetic" } else { cout << "numeric" } cout << ')'; } else if ( ispunct(ch) ) { cout << "punctuation" } else { cout << "unrecognized" }
Why is the category 'spacing' not listed above? Is this category somehow more difficult to read from the user?
Spacing is normally thrown out as not useful by cin's extraction operator (>>). In order to read it, we'd need to use a peek/ignore combo, for instance.
bool toss(void);
which returns true when a coin comes up heads and false when the coin lands tails up.
Use it to write a branch that tells the user which way their virtual coin landed when your program 'tossed' it. (Hint: Remember that you can't print bool directly.)
cout << "Coin came up "; if ( toss() ) { cout << "head"; } else { cout << "tail" } cout << "s.\n";