Repetition+(Loops)

=Do...while loop= _ The do...while loop is our primary post test loop. In my eyes it's useless but we gotta write something about it. The general form for a do...while loop is as follows:

** do ** { ->task1; ->task2; ->. ->. ->. } **while** (testthing);

An example would be adding the sum of the numbers from 50 to 100:

**int** sum = 0; **do** { ->sum += num; ->num += 1; } ** while ** (num < 100); txtSum->Text = sum.ToString;
 * int ** num = 50;

A more in depth explanation of this waste of space can be found on pages 233-235.

=For loop= _ The for loop is an amazing tool, you'll use it to do just about everything in programming and daily life ( I use one for my morning routine ). Its like gravity in physics. It can do everything the do...while loop does but better. It even wrote a song about how it does everything better than the do...while loop:

media type="youtube" key="tfHBPusZg6E" height="385" width="480" You decide which loop is which.

A for loop needs an initialization variable, a condition including that variable to see if the loop will run or not, and an update after the loop runs once ( usually ++ or -- ). The general form for the loop is:

{ ->task1; ->task2; ->. ->. ->. }
 * for ** (initialization; condition; update)

To do what we just did with the do...while loop but more efficiently use this:

txtSum->Text = sum.ToString;
 * int ** sum = 0;
 * for ** (** int ** counter = 1; counter <= 5; counter++) sum += counter;

More information on this miracle can be found on pages 235-237.

= Instance Variables = = _ = Instance Variables are declared directly under #pragma endregion. The purpose of declaring of these variables is so you can use them throughout the entire program and don’t have to declare them over and over again if you use them in multiple events. (page 226)

= Calculating Mean = = _ = Depending on the program calculation mean can be a simple or difficult thing. But for this example on how to we are going to use a variable called “count” to keep count for us. In this code ….. Local VariablesRead num Int32::TryParse(txtNum->Text, num); Process num Display results txtSum->Text =sum.ToString; txtCount->Text=count.ToString; txtMean->Text= mean.ToString; Reset for next number makes the text box blank txtNum->Focus; //puts the cursor in the text box

We need to keep a total count of the numbers we use to add to the total sum. So everytime we would click the add button it would add it to the total and then add 1 to the current “count” total. In which we will use to Calculate mean by taking the total sum divided by the count total. (page 228)

Int count = 0; String^ strOut = “ “; Int random Number; DateTime now = DateTime::Now; Random^ randomNumGenerator; randomNumGenerator = gcnew Random(now.Millisecond); for (int = 0; I < 10; i++) { randomNumber = randomNumGenerator->Next(0,100); strOut += randomNumGenerator + “ “; if (randomNumber > 75) count++; } txtOut->Text = strOut; txtCount->Text += count; || Page 251 ||
 * Counting values with a for loop || An enhancement you can add to the random number generator program. You add an integer variable called count that keeps track of how many values are greater than 75 (for example or any other number you want). An if statement is used inside the loop to increment the count whenever a random number greater than 75 is encountered. || Code: This code is for the btnConcat_Click

While (randomNumber > 75) { count++; randomNumber = randomNumGenerator->Next(0,100); } || Page 251 ||
 * Counting values with a for loop || This is a while loop variation of the for loop. This loop counts the number of consecutive random values greater than 75. The loop will repeat as long as the random number is greater than 75. If you don’t know how many times a loop body will be executed you should use a while or do… while loop. || randomNumber = randomNumGenerator->Next(0,100);

For (row = 0; row < 4; row++) {for (col = 0; col < 3; col++) textBox1->Text += “\t” + (row + col); textBox1->Text += “\r\n”; } || Page 252 || -> is an indent (tab)
 * Creating a table with nested loops || Nesting means putting one control structure inside of another, creating a nested loop. When making a table it is constructed one row at a time. This is code for the example program. The code in the outer loop determines the row. Nested inside of it is another loop that determines the column. The characters “\t” produce a tab and “\r\n” puts the output on the next line. || Int row, col;

For loop to build a string (page 245) We can use a for loop to build strings of characters. The for loop can bring together new items with strOut for every iteration. Here is an example:

String^ strOut= “ “; For (int counter = 0; counter< 10; counter++) strOut += counter; txtOut->Text = strOut;

Generating random numbers (page 247) Creating a random number generator in C++ requires two steps. First, one must declare a random number generator variable, using Random. Next, gcnew is used to make the new random number object. We must give it a unique starting point. This is known as seeding. Date::Time is commonly used for this. Here is an example of random number generator coding: String^ strOut = “ “; Int randomNum; DateTmie now = DateTime::Now; Random^ randomNumGenerator; randomNumGenerator = gcnew Random(now.Millisecond);

for (int counter = 0; counter < 10; counter++) { randomNum = randomNumGenerator->Next(0,100); strOut += randomNum + “ “; } txtOut->Text = strOut;

Finding largest value (page 249) After generating a random number, you may want to find the largest value. A for loop compares random numbers generated and determines the greatest. It is stored in a variable you can name “large.” Example: int large = 0; String^ strOut = “ “; int randomNumber; DateTime now = DateTime::Now; Random^ randomNumGenerator; randomNumGenerator = gcnew Random(now.Millisecond); for (int counter = 0; counter < 10; counter++) { randomNumber = randomNumGenerator->Next(0,100); strOut += randomNumber + “ “; if (large < randomNumber) large = randomNumber; } txtOut->Text = strOut; txtLarge->Text += large;