Project IfAndLoop

 

The for Loop

The for loop looks like this:

 // IA040:                   int[] intArrayNumbers[] = new int[] {12, 43, 77, 88, 123, 4, 77, 11, 13, 77,                                              4, 33};                   int intReferenceNumber = 77; // IA042:         int intLocation = -1; // IA043:         for(int kk=0;kk<12;kk++)                   { // IA045:           if(intArrayNumbers[kk] == intReferenceNumber)                     {                       intLocation = kk;                       // break;                     } // IA050:         } // IA051:         if(intLocation = -1) // Reference Number not found in 'Array Numbers'.                     MessageBox.Show("Msg#5: Reference Number " + intReferenceNumber +                     " not found in ArrayNumbers[] .");                   else // Number found.                     MessageBox.Show("Msg#6: Reference Number " + intReferenceNumber +                     " found at location 'ArrayNumbers[" + intLocation.ToString() + "] ."); 

Line IA042 is necessary to capture the location of ArrayNumbers[] if the number in ArrayNumbers equals intReferenceNumber. It is set to - 1 so line IA051 can detect if the statement in IA045 is ever found to be true. The number - 1 is a safe number here because the numbers of the ArrayNumbers[] elements will be 0 through 11.

Line IA043 defines the for loop. Initially, kk is set to 0, and the loop executes if kk is less than 12. The iterator, kk++, is not executed until the end of the loop (after IA050).

Line IA045 checks to see if ArrayNumbers[0] is equal to intReferenceNumber, which equals 77 in this case. If true, then location 0 is placed into intLocation. Then the iterator kk is increased to 1 and the test in line IA045 is repeated. No matter what happens, this test is performed 12 times, from kk = 0 to kk = 11. At the end of 12 repetitions kk is set to 12 for the thirteenth repetition, but the test kk < 12 in line IA043 fails and the loop stops.

Line IA051 determines if any of the ArrayNumbers[] were equal to 77 in this case. If true, then the location of that number is placed in intLocation.

1.  

What if three of the 12 numbers in ArrayNumbers[] were 77?

2.  

How can I quit the process the first time that line IA045 is satisfied (true)?

3.  

What does break mean?

Answers

1.  

Line IA045 would place the locations in intLocation three times, but only the last entry would appear in intLocation when the loop ended.

2.  

Lines IA042-045 are modified with lines IA046-049:

 IA042:     int intLocation = -1; IA043:     for(int kk = 0 ; kk < 12 ; kk++) IA044:     { IA045:       if(intArrayNumbers[kk] == intReferenceNumber) IA046:       { IA047:         intLocation = kk; IA048:         break; IA049:       } IA050:     } 

3.  

When the if statement in line IA045 is satisfied, then the break statement in line IA048 is executed. The break statement causes the program to exit from the if statement in line IA045 and the for statement in line IA043. So the next line executed is the one below IA050.

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net