loop intro: do while, while, for

flowchart

A loop allows a program to repeat statements based on some condition.
Statements might be repeated millions of times.


. do while (condition)
do while ======== Post test loop - execute, THEN test condition loop executed ONE or more times do { do thing 1; do thing 2; ... do thing n; } while ( condition is true );
// set initial number int sum = 0; int num; num = 1; do { // add to sum and bump counter sum = sum + num; num++; } while (num <= 5); // output sum here txtOut.Text = sum.ToString(); // outputs 15
// set initial number int sum = 0, num = 1; do { // add to sum and bump counter sum += num; // works same as above! num++; } while (num <= 5); // output sum here txtOut.Text = sum.ToString(); // outputs 15
// set initial number int sum = 0, num = 1; do { // add to sum and bump counter sum += num++; // looks weird, but works same as above! } while (num <= 5); // output sum here txtOut.Text = sum.ToString(); // outputs 15 MessageBox.Show( num.ToString() ); // outputs 6!!
// print numbers 1-100 int i = 1; do { txtOut.AppendText( i.ToString() + " " ); i++; } while( i <= 100 ); output in txtOut: 1 2 3 4 5 6 7 ... 100

. while (condition)
while ===== Pre Test - test condition, THEN execute loop executed ZERO or more times
while (condition is true) { do thing 1; do thing 2; ... do thing n; }
int sum = 0; int num = 1; while (num <= 5)   only enters loop if initial value of mbi is bigger than 52.7 { // add to sum and bump counter sum += num; num++; } txtOut.Text = sum.ToString(); // outputs 15
int x; x = 1; while ( x < 5 ) // Condition MUST eventually become false!! { MessageBox.Show( x.ToString() ); x++; }
// What does this do? int total = 0, number = 1; // declare and INIT variables while ( number <= 10 ) { /// add the number to the total total += number; // same as total = total + number; number+=2; /// same as number = number + 2; } // 1 + 3 + 5 + 7 + 9 MessageBox.Show( total.ToString() ); // in windows, MUST explicitly convert to string
// Write code that will print out the square root of // all the numbers starting at 5 and going thru 20 int number = 5; while ( number <= 20 ) { // print number and root and increment number MessageBox.Show( "Root of " + number + " is " + Math.Sqrt(number).ToString("N") ); number++; }

. for loops
for Loops allow repetition of 1 or more statements ================================================== // i goes from 0 to 9 - 10 times for ( int i=0; i<10; i++ ) { MessageBox.Show( i.ToString() ); } Output: ------- 0 1 2 3 4 5 6 7 8 9
// loop n times int n = int.Parse(txtIn.Text); for(int i=0; i<n; i++) { txtOut.AppendText(i + " "); } Output: ------- Enter an integer: 7 0 1 2 3 4 5 6
// print out numbers 1-100 in txtOut for ( int i=1; i<=100; i++ ) { txtOut.AppendText( i.ToString() + " " ); }
// loop n times and sum numbers from 1 thru n int sum=0; int n = int.Parse(txtIn.Text); for( int i=1; i<=n; i++ ) { txtOut.AppendText( i + " " ); sum += i; } txtOut.AppendText( Environment.NewLine + "The sum of 1 thru " + n + " is " + sum ); Output: ------- Enter an integer: 5 1 2 3 4 5 The sum of 1 thru 5 is 15
// nested loops int i, j; txtOut.AppendText(Environment.NewLine); for( i=1; i<=6; i++ ) { for( j=1; j<=i; j++ ) { txtOut.AppendText( "*" ); } txtOut.AppendText(Environment.NewLine); } Creates the following pattern * ** *** **** ***** ******

. break and continue
break ====== 'break' used to leave a loop. // What does this do? number = 1; while ( true ) { txtOut.AppendText( number++ ); // same as number = number + 1; if ( number > 5 ) break; } txtOut.AppendText( number ); OUTPUT ------ 123456
continue ========= 'continue' causes the program to skip the rest of the loop in the current iteration and jumps to the start of the next iteration. // continue loop example for ( int n = 10; n > 0; n-- ) { if ( n == 5 ) { txtOut.AppendText( "five ,"); continue; } txtOut.AppendText( n + ", " ); } txtOut.AppendText( "FIRE!" ); OUTPUT: 10, 9, 8, 7, 6, five, 4, 3, 2, 1, FIRE!