loop examples

flowchart

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



.. code snippets...
Sum the numbers 1-10?
      int sum = 0, n = 1;
      do
      {
        // add n to sum
        sum += n;
        
        // increment n        
        n++;
      } while( n <=10 );
      
      // -->  sum becomes 1 + 2 + 3 ... + 10 => 55
Sum the first 5 odd integers?
      int sum = 0, n = 1, oddCount = 1;
      do
      {
        if ( n%2 == ??  ) // is n odd?
        {
           // add odd n to sum
           ??    
                          
           // add 1 to oddCount
           ??
        }
        n++; // next number
      } while( oddCount <=5 );
      
      // --> sum  becomes 1 + 3 + 5 + 7 + 9      

.. code - adding numbers 1 thru 1000 [ C# windows ]
C# Windows 1000 numbers
   // button click code
   private void btnDoIt_Click(object sender, EventArgs e)
   {
      //-------------------------------------------
      //    set number = 1, sum = 0
      //    while number less than or equal to 1000
      //       add number to sum
      //       add 1 to number
      //    end while
      // output the sum

      int num = 1, sum = 0 ; // for sum equation

      // do 1000 times adding i as it increases from 1 thru 1000
      for ( int i = 1; i <= 1000; i++ )
      {
         sum += i; // why don't we need to do i++ ?      
      }
      // tell user!
      MessageBox.Show( "The total is " + sum.ToString() );
   }
Could you allow the user to set the limit, n, to sum 1 thru n??
C# Windows get limit from user via TextBox
   // button click code
   private void btnDoIt_Click(object sender, EventArgs e)
   {
      //-------------------------------------------
      //    get limit
      //    set number = 1, sum = 0
      //    while number less than or equal to limit
      //       add number to sum
      //       add 1 to number
      //    end while
      // output the sum
      
      int limit, num = 1, sum = 0; // for sum equation

      // get limit of numbers & convert
      if( int.TryParse( txtIn.Text, out limit ) )
      {
         // do limit times
         while ( num <= limit )
         {
            // add to sum and bump num
            sum += num++;  
         }      
         // show the sum to user
         MessageBox.Show( "The total is " + sum.ToString() );
      }
      else
      {
         MessageBox.Show( "Bad input" );
      }
   }


    Advanced

.. Looping through CONTROLS: Clearing RadioButtons and CheckBoxes on a form
Loop thru all the controls on the form, and clearing all radioButtons and checkBoxes
   
      private void btnLoopControl_Click(object sender, EventArgs e)
      { 
         // loop thru ALL controls on form
         foreach ( Control c in this.Controls )
         {
            // is it a radioButton?
            if ( c is RadioButton )
            {
               // show the control
               MessageBox.Show(c.ToString());
               
               // force the control into a radioButton & uncheck
               RadioButton r = (RadioButton)c;
               r.Checked = false;
            }
            // is it a checkBox?
            if ( c is CheckBox )
            {
               // show the control
               MessageBox.Show(c.ToString());
               
               // force the control into a checkButton & uncheck
               CheckBox cb = (CheckBox)c;
               cb.Checked = false;
            }
         }  
      }

.. Looping through all CheckBoxes on a form AND in containers (panels, groupboxes)
Loop thru all the controls on the form, and clearing all checkBoxes INCLUDING those in PANEL or GROUPBOX ================================================================= private void btnLoopControl_Click(object sender, EventArgs e) { // loop thru ALL controls on the form foreach ( Control c in this.Controls ) // is it a checkBox? if ( c is CheckBox ) { // force the control into a checkButton & uncheck CheckBox cb = (CheckBox)c; cb.Checked = false; } // loop thru ALL controls on panel foreach ( Control c in panel1.Controls ) // is it a checkBox? if ( c is CheckBox ) { // force the control into a checkButton & uncheck CheckBox cb = (CheckBox)c; cb.Checked = false; } // loop thru ALL controls on groupBox foreach ( Control c in groupBox1.Controls ) // is it a checkBox? if ( c is CheckBox ) { // force the control into a checkButton & uncheck CheckBox cb = (CheckBox)c; cb.Checked = false; } }

.. RECURSION (slick): Loop through all Controls on a form AND the containers (panels, groupboxes)
Loop thru all the controls on the form, INCLUDING those in PANEL or GROUPBOX ============================================================================ private void button1_Click(object sender, EventArgs e) { ClearRec( this ); } private void ClearRec(Control o) { // loop thru ALL controls on form foreach( Control c in o.Controls ) if( c is CheckBox ) { // force the control into a checkButton & uncheck CheckBox cb = (CheckBox)c; cb.Checked = false; } // if there are more, call again with the next control else if ( c.Controls.Count > 0 ) // only true if IT is a container also ClearRec( c ); }