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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
Strings serve multiple purposes in programming, primarily for representing text, including words and phrases in various languages, not just English. They can be encoded as numerical values, but this method is complex compared to the simplicity of using strings. Strings are also crucial for operations like hashing, where a short string represents the contents of a file, aiding in integrity checks.Switch statements provide a clearer and often more efficient alternative to long nested if-else structures. They signal to the reader that a specific case is being handled, reducing the risk of errors that can arise from omitted else conditions. While if-else statements can achieve the same functionality, switch statements enhance readability and can be faster with multiple branches. Additionally, char types are used for single characters, but they can also be manipulated as integers, with their ASCII values determining their representation.
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?
 
Technology 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]cout[/m] prints them as letters and not their numeric codes.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
22
Views
3K
Replies
34
Views
4K
Replies
8
Views
4K
Replies
5
Views
3K
Replies
40
Views
3K
Back
Top