< Previous | Contents | Next >

Questions and Answers

Q: Which is better, a while loop or a for loop?

A: Neither is inherently better than the other. Use the loop that best fits your needs.

Q: When might it be better to use a for loop than a while loop?

A: You can create a while loop to do the job of any for loop; however, there are some cases that cry out for a for loop. Those include counting and iterating through a sequence.

Q: Can I use break and continue statements with for loops?

A: Sure. And they behave just like they do in while loops: break ends the loop and continue jumps control back to the top of the loop.

Q: Why do programmers tend to use variable names such as i, j, and k as counters in for loops?

112 Chapter 3 n For Loops, Strings, and Arrays: Word Jumble


A: Believe it or not, programmers use i, j, and k mainly out of tradition. The practice started in early versions of the FORTRAN language, in which integer variables had to start with certain letters, including i, j, and k.

Q: I dont have to include a file to use int or char types, so why do I have to include the string file to use strings?

A: int and char are built-in types. Theyre always accessible in any Cþþ program. The string type, on the other hand, is not a built-in type. Its defined as part of the standard library in the file string.

Q: How did C-style strings get their name?

A: In the C programming language, programmers represent strings with arrays of characters terminated by a null character. This practice carried over to Cþþ. After the new string type was introduced in Cþþ, programmers needed a way to differentiate between the two. Therefore, the old method was dubbed C-style strings.

Q: Why should I use string objects instead of C-style strings?

A: string objects have advantages over C-style strings. The most obvious is that they are dynamically sizeable. You dont have to specify a length limit when you create one.

Q: Should I ever use C-style strings?

A: You should opt for string objects whenever possible. If youre working on an existing project that uses C-style strings, then you might have to work with C-style strings.

Q: What is operator overloading?

A: Its a process that allows you to define the use of familiar operators in different contexts with different but predictable results. For example, the + operator that is used to add numbers is overloaded by the string type to join strings.

Q: Cant operator overloading be confusing?

A: Its true that by overloading an operator you give it another meaning. But the new meaning applies only in a specific new context. For example, its clear in

Discussion Questions 113


the expression 4+ 6 that the + operator adds numbers, while in the expression

myString1 + myString2, the + operator joins strings. Q: Can I use the += operator to concatenate strings?

A: Yes, the += operator is overloaded so it works with strings.

Q: To get the number of characters in a string object, should I use the length()

member function or the size() member function?

A: Both length() and size() return the same value, so you can use either. Q: Whats a predicate function?

A: A function that returns either true or false. The string object member function empty() is an example of a predicate function.

Q: What happens if I try to assign a value to an element beyond the bounds of an array?

A: Cþþ will allow you to make the assignment. However, the results are unpredictable and might cause your program to crash. Thats because youre altering some unknown part of your computers memory.

Q: Why should I use multidimensional arrays?

A: To make working with a group of elements more intuitive. For example, you could represent a chessboard with a one-dimensional array, as in chessBoard[64], or you could represent it with a more intuitive, two- dimensional array, as in chessBoard[8][8].