ineedhelpnow
- 649
- 0
what are the purposes of strings. also what is the purpose of switch statements. would it not be enough to use if else statements instead of switch stmts?
Well, you could encode your printed messages as natural numbers. For example, "Hello" consists of characters with ASCII codes 72, 101, 108, 108 and 111. You could encode it as $2^{72}3^{101}5^{108}7^{108}11^{111}$. Since decomposition into prime factors is unique, the original string can be reconstructed. But for this you need arbitrarily large integers. Also, this would be considerably more complex than having strings.ineedhelpnow said:what are the purposes of strings.
Yes, you could do with [m]if ... else[/m]. My understanding is that there is a general principle in programming: if there is an often-used special case of an expression or function, then it is a good idea to use it when it is sufficient. This way you signal to your program's reader that this is a special case.ineedhelpnow said:what is the purpose of switch statements. would it not be enough to use if else statements instead of switch stmts?
if (c == 'a') statament1;
else
if (c == 'b') statament2;
else
if (c == 'c') statament3;
else
statement4;
switch (c) {
case 'a' : statement1; break;
case 'b' : statement2; break;
case 'c' : statement3; break;
default : statement4;
}
if (c == 'a') statament1;
else
if (c == 'b') statament2;
if (c == 'c') statament3;
else
statement4;
What do you mean by words? English words? Then who would forbid me assigning "hoomin" to a string? Maybe my program is dealing with LOLspeak. What about another language that uses Latin letters? Restricting C++ strings to English words, either formally, as a part of the language semantics, or informally would be very strange.ineedhelpnow said:so strings are only used for words?
In fact, a char variables stores integers either between 0 an 255 or between -128 and 127 (or at least that's how it is in C). Characters are represented via their ASCII codes. So you could add [m]char[/m] variables as though they were integers, but when they are printed, [m]cout[/m] prints them as letters and not their numeric codes.ineedhelpnow said:can char only be used for single characters?