What are the Purposes of Strings and Switch Statements in C++ Programming?

  • Topic: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
3 replies · 2K views
ineedhelpnow
Messages
649
Reaction score
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?
 
Physics news on Phys.org
ineedhelpnow said:
what are the purposes of strings.
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 is the purpose of switch statements. would it not be enough to use if else statements instead of switch stmts?
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.

If you have a long nested [m]if[/m] structure, the reader has to analyze it to make sure that it behaves like a [m]switch[/m]. If there is a small deviation, it may behave differently. For example,
Code:
if (c == 'a') statament1;
else
if (c == 'b') statament2;
else
if (c == 'c') statament3;
else
statement4;
does behave like
Code:
switch (c) {
  case 'a' : statement1; break;
  case 'b' : statement2; break;
  case 'c' : statement3; break;
  default : statement4;
}
But if you omit one [m]else[/m]:
Code:
if (c == 'a') statament1;
else
if (c == 'b') statament2;
if (c == 'c') statament3;
else
statement4;
then [m]statement4[/m] would execute also when c == 'a'. If you want to show your reader that there is no hidden trick and your branching structure is a special case compared to the full power of [m]if[/m], then it is a good idea to use a special syntax, i.e., [m]switch[/m].

Also, when there are many branches [m]switch[/m] is a little faster than [m]if[/m].
 
so strings are only used for words?

- - - Updated - - -

(Wondering) can char only be used for single characters?
 
ineedhelpnow said:
so strings are only used for words?
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.

One use of strings that comes to mind has to do with hashes. It's when a program goes through an entire file and generates a short string that is based on the file's contents. Of course, the string does not contains the entire information stored in the file, but if a file changes, the string will almost surely change as well. Thus, checking whether a file matches the approved hash is a way of checking that the file has not been corrupted, e.g., during downloading.

A hash itself is a meaningless string, for example, "4716ca912495c805b94a88ef6dc3fb4aff46bf3c". In the version control system Git, hashes are used as names of different file versions.

ineedhelpnow said:
can char only be used for single characters?
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]count[/m] prints them as letters and not their numeric codes.