What are the two methods for loop control In case when you d
Solution
Problem 6)
The while Loop
A while loop will check a condition and then continues to execute a block of code as long as the condition evaluates to a boolean value oftrue. Its syntax is as follows: while (<boolean expression>) { <statements> }. The statements can be any valid C# statements. The boolean expression is evaluated before any code in the following block has executed. When the boolean expression evaluates to true, the statements will execute. Once the statements have executed, control returns to the beginning of the while loop to check the boolean expression again.
When the boolean expression evaluates to false, the while loop statements are skipped and execution begins after the closing brace of that block of code. Before entering the loop, ensure that variables evaluated in the loop condition are set to an initial state. During execution, make sure you update variables associated with the boolean expression so that the loop will end when you want it to.
 using System;
class WhileLoop
 {
     public static void Main()
     {
         int myInt = 0;
        while (myInt < 10)
         {
             Console.Write(\"{0} \", myInt);
             myInt++;
         }
         Console.WriteLine();
     }
 }
 It begins with the keyword while, followed by a boolean expression. All control statements use boolean expressions as their condition for entering/continuing the loop. This means that the expression must evaluate to either a true orfalse value. In this case we are checking the myInt variable to see if it is less than (<) 10. Since myInt was initialized to 0, the boolean expression will return true the first time it is evaluated. When the boolean expression evaluates to true, the block immediately following the boolean expression will be executed.
 Within the while block we print the number and a space to the console. Then we increment (++) myInt to the next integer. Once the statements in the while block have executed, the boolean expression is evaluated again. This sequence will continue until the boolean expression evaluates to false. Once the boolean expression is evaluated as false, program control will jump to the first statement following the while block. In this case, we will write the numbers 0 through 9 to the console, exit the while block, and print a new line to the console.
The for Loop
A for loop works like a while loop, except that the syntax of the for loop includes initialization and condition modification. for loops are appropriate when you know exactly how many times you want to perform the statements within the loop. The contents within the for loop parentheses hold three sections separated by semicolons (<initializer list>; <boolean expression>; <iterator list>) { <statements> }.
The initializer list is a comma separated list of expressions. These expressions are evaluated only once during the lifetime of the for loop. This is a one-time operation, before loop execution. This section is commonly used to initialize an integer to be used as a counter.
Once the initializer list has been evaluated, the for loop gives control to its second section, the boolean expression. There is only one boolean expression, but it can be as complicated as you like as long as the result evaluates to true or false. The boolean expression is commonly used to verify the status of a counter variable.
When the boolean expression evaluates to true, the statements within the curly braces of the for loop are executed. After executing for loop statements, control moves to the top of loop and executes the iterator list, which is normally used to increment or decrement a counter. The iterator list can contain a comma separated list of statements, but is generally only one statement
using System;
    class ForLoop
     {
         public static void Main()
         {
             for (int i=0; i < 20; i++)
             {
                 if (i == 10)
                     break;
                 if (i % 2 == 0)
                     continue;
                 Console.Write(\"{0} \", i);
             }
             Console.WriteLine();
         }
     }
Normally, for loop statements execute from the opening curly brace to the closing curly brace without interruption. However, in Listing 4-3, we’ve made a couple exceptions. There are a couple if statements disrupting the flow of control within the for block.
Its depends on your code condition which loop you will prefer


