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

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around the purposes of strings and switch statements in C++ programming. Participants explore the functionality and implications of using these constructs, touching on their applications and potential alternatives.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants propose that strings can be used for more than just words, suggesting that they can represent various forms of text, including non-English words or even nonsensical strings.
  • One participant discusses encoding strings as natural numbers using prime factorization, noting the complexity this introduces compared to using strings directly.
  • There is a suggestion that switch statements provide clarity and efficiency over nested if-else structures, especially in cases with many branches.
  • Another participant questions whether strings are limited to words, prompting a clarification that strings can represent any sequence of characters.
  • Concerns are raised about the behavior of if-else statements compared to switch statements, particularly regarding potential pitfalls in logic when using if-else without careful structuring.
  • Participants discuss the representation of characters in C++, noting that char variables can store integer values corresponding to ASCII codes.

Areas of Agreement / Disagreement

Participants express differing views on the scope and utility of strings and switch statements. There is no consensus on whether strings are limited to words or if switch statements are always preferable to if-else structures.

Contextual Notes

Some limitations in the discussion include the lack of formal definitions for terms like "words" in the context of strings and the potential ambiguity in the behavior of conditional statements in C++.

Who May Find This Useful

This discussion may be of interest to C++ programmers, computer science students, and individuals exploring programming concepts related to data representation and control structures.

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]count[/m] prints them as letters and not their numeric codes.
 

Similar threads

  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 34 ·
2
Replies
34
Views
4K
Replies
89
Views
6K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 25 ·
Replies
25
Views
1K
  • · Replies 40 ·
2
Replies
40
Views
3K