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
SUMMARY

The discussion focuses on the purposes of strings and switch statements in C++ programming. Strings are essential for representing sequences of characters, allowing for complex data manipulation, such as hashing for file integrity verification. Switch statements provide a clearer and more efficient alternative to nested if-else structures, particularly when handling multiple conditions, enhancing code readability and performance. The conversation highlights that while char variables represent single characters, strings can encompass a broader range of textual data beyond just English words.

PREREQUISITES
  • Understanding of C++ syntax and data types
  • Familiarity with ASCII character encoding
  • Knowledge of control flow structures in programming
  • Basic concepts of hashing and its applications
NEXT STEPS
  • Explore C++ string manipulation functions and libraries
  • Learn about the performance implications of switch statements versus if-else statements in C++
  • Investigate hashing algorithms and their use in file integrity checks
  • Study the differences between char and string data types in C++
USEFUL FOR

C++ developers, software engineers, and computer science students interested in improving their understanding of string handling and control flow mechanisms in programming.

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
857
  • · Replies 40 ·
2
Replies
40
Views
3K