Csharp/CSharp Tutorial/Statement/For

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A negatively running for loop

using System; 
 
class MainClass {     
  public static void Main() {     
    int x; 
 
    for(x = 100; x > -100; x -= 5) 
      Console.WriteLine(x); 
  } 
}
100
95
90
85
80
75
70
65
60
55
50
45
40
35
30
25
20
15
10
5
0
-5
-10
-15
-20
-25
-30
-35
-40
-45
-50
-55
-60
-65
-70
-75
-80
-85
-90
-95

Declare loop control variable inside the for

using System; 
 
class MainClass {   
  public static void Main() { 
    int sum = 0; 
    int fact = 1; 
 
    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); 
  }   
}
Sum is 15
Factorial is 120

Demonstrate a block of code inside if statement

using System; 
 
class MainClass { 
  public static void Main() { 
    int i, j, d; 
 
    i = 5; 
    j = 10; 
 
    if(i != 0) { 
      Console.WriteLine("i does not equal zero"); 
      d = j / i; 
      Console.WriteLine("j / i is " + d); 
    } 
  } 
}
i does not equal zero
j / i is 2

for loop

You can repeatedly execute a sequence of code by creating a loop.

The general form of the for loop for repeating a single statement is


for(initialization; condition; iteration) 
        statement;

For loop with multiple expressions

using System;
class MainClass
{
   static void Main()
   {
      const int count = 5;
      for (int i = 0, j = 10; i < count; i++, j += 10)
      {
         Console.WriteLine("{0}, {1}", i, j);
      }
   }
}
0, 10
1, 20
2, 30
3, 40
4, 50

Loop condition can be any bool expression.

using System; 
 
class MainClass {    
  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); 
    } 
 
  } 
}
i, j: 0 100
i, j: 1 99
i, j: 2 98
i, j: 3 97
i, j: 4 96
i, j: 5 95
i, j: 6 94
i, j: 7 93
i, j: 8 92
i, j: 9 91
i, j: 10 90

Move "update" out of the for loop

using System; 
 
class MainClass {   
  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 
    } 
 
  }   
}
Pass #0
Pass #1
Pass #2
Pass #3
Pass #4
Pass #5
Pass #6
Pass #7
Pass #8
Pass #9

Nested for loop to calculate prime number

using System; 
 
class MainClass {    
  public static void Main() {    
    int num; 
    int i; 
    int factor; 
    bool isprime; 
 
 
    for(num = 2; num < 20; num++) { 
      isprime = true;  
      factor = 0; 
 
      // see if num is evenly divisible 
      for(i=2; i <= num/2; i++) { 
        if((num % i) == 0) { 
          // num is evenly divisible -- not prime 
          isprime = false; 
          factor = i; 
        } 
      } 
 
      if(isprime) 
        Console.WriteLine(num + " is prime."); 
      else 
        Console.WriteLine("Largest factor of " + num + 
                          " is " + factor); 
    } 
 
  }    
}
2 is prime.
3 is prime.
Largest factor of 4 is 2
5 is prime.
Largest factor of 6 is 3
7 is prime.
Largest factor of 8 is 4
Largest factor of 9 is 3
Largest factor of 10 is 5
11 is prime.
Largest factor of 12 is 6
13 is prime.
Largest factor of 14 is 7
Largest factor of 15 is 5
Largest factor of 16 is 8
17 is prime.
Largest factor of 18 is 9
19 is prime.

Parts of the for can be empty.

using System; 
 
class MainClass {   
  public static void Main() { 
    int i; 
 
    for(i = 0; i < 10; ) { 
      Console.WriteLine("Pass #" + i); 
      i++; // increment loop control var 
    } 
 
  }   
}
Pass #0
Pass #1
Pass #2
Pass #3
Pass #4
Pass #5
Pass #6
Pass #7
Pass #8
Pass #9

The body of a loop can be empty

using System; 
 
class MainClass {   
  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); 
  }   
}
Sum is 15

Use block inside for statement: Compute the sum and product of the numbers from 1 to 10

using System;  
   
class MainClass {   
  public static void Main() {   
    int prod; 
    int sum; 
    int i; 
 
    sum = 0; 
    prod = 1; 
 
    for(i=1; i <= 10; i++) { 
      sum = sum + i; 
      prod = prod * i;       
    } 
    Console.WriteLine("Sum is " + sum); 
    Console.WriteLine("Product is " + prod); 
 
  }   
}
Sum is 55
Product is 3628800

Use byte to control for loop

using System; 
 
class MainClass {    
  public static void Main() {    
    byte x; 
    int sum; 
 
    sum = 0; 
    for(x = 1; x <= 100; x++) 
      sum = sum + x; 
 
    Console.WriteLine("Summation of 100 is " + sum); 
  }    
}
Summation of 100 is 5050

Use commas in a for statement.

using System; 
 
class MainClass {    
  public static void Main() {    
    int i, j; 
 
    for(i=0, j=10; i < j; i++, j--) 
      Console.WriteLine("i and j: " + i + " " + j); 
  } 
}
i and j: 0 10
i and j: 1 9
i and j: 2 8
i and j: 3 7
i and j: 4 6

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

using System; 
 
class MainClass {    
  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) & (j >= 2); i++, j--) { 
 
      if((smallest == 1) & ((num % i) == 0))  
        smallest = i; 
 
      if((largest == 1) & ((num % j) == 0))  
        largest = j; 
 
    } 
 
    Console.WriteLine("Largest factor: " + largest); 
    Console.WriteLine("Smallest factor: " + smallest); 
  } 
}
Largest factor: 50
Smallest factor: 2