Declare loop control variable inside the for

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Declare loop control variable inside the for.

using System;

public class ForVar {
public static void Main() {
int sum = 0;
int fact = 1;

// compute the factorial of the numbers through 5
for(int i = 1; i <= 5; i++) { sum += i; // i is known throughout the loop fact *= i; } // but, i is not known here. Console.WriteLine("Sum is " + sum); Console.WriteLine("Factorial is " + fact); } } [/csharp]

The body of a loop can be empty

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// The body of a loop can be empty.

using System;

public class Empty3 {
public static void Main() {
int i;
int sum = 0;

// sum the numbers through 5
for(i = 1; i <= 5; sum += i++) ; Console.WriteLine("Sum is " + sum); } } [/csharp]

Move more out of the for loop

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Move more out of the for loop.

using System;

public class Empty2 {
public static void Main() {
int i;

i = 0; // move initialization out of loop
for(; i < 10; ) { Console.WriteLine("Pass #" + i); i++; // increment loop control var } } } [/csharp]

Parts of the for can be empty

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Parts of the for can be empty.

using System;

public class Empty {
public static void Main() {
int i;

for(i = 0; i < 10; ) { Console.WriteLine("Pass #" + i); i++; // increment loop control var } } } [/csharp]

Loop condition can be any bool expression

image_pdfimage_print
   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Loop condition can be any bool expression. 
using System; 
 
public class forDemo {    
  public static void Main() {    
    int i, j; 
    bool done = false; 
 
    for(i=0, j=100; !done; i++, j--) { 
 
      if(i*i >= j) done = true; 
 
      Console.WriteLine("i, j: " + i + " " + j); 
    } 
 
  }    
}


           
          


Use commas in a for statememt to find the largest and smallest factor of a number

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/* 
   Use commas in a for statememt to find 
   the largest and smallest factor of a number. 
*/ 
 
using System; 
 
public class Comma1 {    
  public static void Main() {    
    int i, j; 
    int smallest, largest; 
    int num; 
 
    num = 100; 
    
    smallest = largest = 1; 
 
    for(i=2, j=num/2; (i <= num/2) &amp; (j >= 2); i++, j--) { 
 
      if((smallest == 1) &amp; ((num % i) == 0))  
        smallest = i; 
 
      if((largest == 1) &amp; ((num % j) == 0))  
        largest = j; 
 
    } 
 
    Console.WriteLine("Largest factor: " + largest); 
    Console.WriteLine("Smallest factor: " + smallest); 
  } 
}

           
          


Use commas in a for statememt

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use commas in a for statememt.

using System;

public class Comma {
public static void Main() {
int i, j;

for(i=0, j=10; i < j; i++, j--) Console.WriteLine("i and j: " + i + " " + j); } } [/csharp]