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

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++
In summary: If I want to store a symbol in a string, what would I use?Well, if you want to store a symbol in a string, you could use a character variable that is mapped to a symbol. For example, you could usechar my_symbol[] = "MY_SYMBOL";
  • #1
ineedhelpnow
651
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
  • #2
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].
 
  • #3
so strings are only used for words?

- - - Updated - - -

(Wondering) can char only be used for single characters?
 
  • #4
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.
 
  • #5


I am not an expert in computer programming, but I can provide a general response to these questions.

Strings in C++ are used to represent a sequence of characters. They are commonly used to store and manipulate textual data, such as words, sentences, or even entire documents. The purpose of strings is to provide a way for programmers to work with text data in a more efficient and organized manner. For example, strings have built-in functions that allow for operations such as concatenation, comparison, and searching. They also have a fixed length, making them more secure to use than traditional character arrays.

Switch statements in C++ are used to evaluate a variable or expression against a list of possible values and execute different blocks of code based on the matching value. The purpose of switch statements is to provide a more concise and efficient way to handle multiple conditions. It can also make code more readable and easier to maintain, especially when there are many possible conditions.

While it is true that if-else statements can also handle multiple conditions, they are typically used when there are only a few conditions to consider. Switch statements are more suitable for situations where there are many possible conditions, as they can be written in a more compact and organized way. Additionally, switch statements can also be more efficient in terms of execution time, as the compiler can optimize the code for the switch cases.

In conclusion, strings and switch statements serve different purposes in C++ and are both important tools for programmers to work with data and handle multiple conditions in their code. While if-else statements can also be used in some cases, switch statements offer a more efficient and organized solution for handling multiple conditions.
 

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

What is the difference between a variable and a constant in C++?

A variable is a named storage location in memory that can hold a value, which can be changed throughout the program. A constant, on the other hand, is a value that cannot be altered during the execution of the program. It is declared using the keyword "const" and must be assigned a value at the time of declaration.

What are the basic data types in C++?

The basic data types in C++ are integer, floating-point, character, boolean, and void. Integer types include int, short, long, and long long. Floating-point types include float, double, and long double. Character type is represented by the char keyword and can hold a single character. Boolean type has two possible values: true or false. Void type is used to indicate that a function does not return a value.

What is the difference between declaration and initialization in C++?

Declaration refers to the act of creating a variable or function, while initialization is the process of assigning an initial value to the variable at the time of declaration. For example, int num; is a declaration of an integer variable, while int num = 10; is a declaration and initialization of the same variable.

What is the scope of a variable in C++?

The scope of a variable refers to the part of the program where the variable is visible and can be accessed. In C++, there are three types of variable scopes: global, local, and class. Global variables are declared outside of any functions and can be accessed by any part of the program. Local variables are declared inside a function and can only be accessed within that function. Class variables (also known as member variables) are declared inside a class and can be accessed by any member function of that class.

What is the difference between pass by value and pass by reference in C++?

In pass by value, the value of the actual parameter is copied to the formal parameter of the function. Any changes made to the formal parameter will not affect the actual parameter. In pass by reference, a reference to the actual parameter is passed to the formal parameter. This means that any changes made to the formal parameter will also affect the actual parameter. Pass by reference is often used when the function needs to modify the value of the variable passed to it.

Similar threads

Replies
118
Views
8K
Replies
22
Views
2K
Replies
5
Views
1K
Replies
22
Views
3K
Replies
34
Views
3K
Replies
8
Views
2K
Replies
40
Views
3K
Replies
5
Views
2K
Back
Top