Chapter 3: Loops and Strings:
Writing an Effective While Loop - Designing an Appropriate Condition
If the condition is always true, the loop will never quit.
A Closer Look at Strings
In C#, a string variable is actually an object, which is a little more powerful than a normal variable.
Describe the use of the ForEach loop in C#.
The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.
Incrementing the Variable in a for loop
The sentry variable must be incremented for the loop to exit.
Varying the For Loop's Behavior
You can set up for loops that skip over numbers, count by two, or count backwards. The basic design of the for statement is the same, but you can change the way the three parts of the for loop are written to change the way the loop acts.
Checking for an Upper Limit (3 things)
1. A condition that checks whether the variable is past a limit. 2. As long as the condition is true the loop will continue. 3. As soon as the condition is evaluated to false, the loop will stop, and the next line of code after the right brace (}) will execute.
Using a For Loop (4 things)
1. A line starting with the keyword for indicates the beginning of a for loop. 2. The keyword for is followed parenthesis that contains three elements. 3. After the parenthesis, the for line is followed by a set of braces containing one or more lines of code. 4. The code inside the braces will repeat a certain number of times, based on the way the for loop is set up.
Creating a Sentry Variable (3 things)
1. A special variable that will be used to control how long the loop continues. 2. It is frequently referred to as a sentry variable. 3. For loops almost always use integers as sentry variables.
Using the Object Browser(5 things)
1. All the interesting variables and commands in C# are related to objects, so one key to becoming a good C# programmer is learning how to investigate the various objects you encounter. 2. You can reach the Object Browser in the IDE by selecting View, Other Windows or pressingCtrl+Alt+J. 3. All the objects available to your program are accessible from this tool. 4. You find the .NET objects (such as string) under the mscorlib (Microsoft Core Library) branch of the tree. 5. The mscorlib is the library of all .NET objects. Open this library by clicking on its name, and you see a list of the namespaces. Find the System namespace, and you see a list of objects in the namespace.
Writing an Effective While Loop - Changing the Variable inside the Loop (2 things)
1. Another common problem is to forget changing the value of a variable inside a loop. 2. The basic rule is to make sure that inside your loop you have code that makes it possible for the loop to exit.
Performing Common String Manipulations (2 things)
1. C# is very picky about case when comparing string values; therefore, "whoo hoo" is not considered the same as "WHOO HOO". 2. The real focus here is not to show you every method of the string object. Instead, you need to see how to investigate the string object (or any other object you might encounter) so that you can exploit its properties, methods, and events.
Investigating The String Object (2 things)
1. C# provides a special object called the String which manipulates text values in a number of ways. 2. The string object can capitalize the text, search for one phrase inside another, find out the length of the text, and many other things.
The Three Items in the Parenthesis of a for Loop
1. Creating a Sentry Variable 2. Checking for an Upper Limit (a condition) 3. Incrementing the Variable
Writing an Effective While Loop - Creating and Intializing a Sentry Variable (2 things)
1. Generally, the sentry variable is designed to change while the loop is running. 2. If you're not careful, you can have a loop that never runs.
Using a While Loop (5 things)
1. It is possible to use for loops for any kind of looping situation. For loops are best for situations in which you know how many times something will happen or when you want to take advantage of the counting nature of the loop. For other kinds of looping situations, you use a variation of the while loop. 2. The while is followed by a condition. For example: while (response != theAnswer){ 3. If the condition is evaluated to true, everything in the braces that follows the looping structure is executed. 4. When the code reaches the right brace, it returns to the beginning and checks the condition again. 5. If the condition is true, the code inside the loop happens again. If the condition is evaluated as false, program control passes to the next line after the right brace that follows the loop.
Using a Foreach Loop to Break Up a Sentence (8 things)
1. Its full value will be apparent only after you learn arrays in Chapter 8, "Arrays..." 2. The foreach loop extracts specific elements from a group. 3. In a foreach loop, you can use any kind of variable as the sentinel variable, but you must get that variable from a group. 4. The Split() method of a string automatically splits a phrase into a group of words. 5. This foreach loop repeats one time for each word in a phrase. 6. Each time through the loop, the word variable contains the next word in the sentence. 7. When you need to break a phrase or sentence into words, use the foreach loop. 8. For now, simply remember that the foreach code combined with the string.Split() method makes a loop that features each word in a sentence.