This last cycle is most commonly used for our future cycle, so lengthy, please understand.
I do not know, everyone in the while loop when the write control cycles of time, is not always forget to write i ++, so while still sometimes not very easy to use,
So, in case we know the number of cycles, we can use a for loop to avoid.
for loop
grammar:
for (Expression 1; 2 Expression; Expression 3)
{
Loop body;
}
Expression 1: Declare variables, recording cycles.
Expression 2: cycling conditions.
Expression 3: Change the code loop conditions, so that eventually no longer valid.
Here we write about such an exercise: Find a few daffodils between 100 to 999 (one hundred 3 + ten 3 + bits 3 = the number)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 14 for (int i = 100; i <=999; i++) 15 { 16 int bai = i / 100 ; 17 int shi = i% 100 / 10 ; 18 int ge = i% 10 ; 19 20 an if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i) 21 { 22 Console.WriteLine ( " Narcissus flower number Yes 0} { " , i); 23 } 24 Console.ReadKey (); 25 } 26 } 27 } 28 }
Exercise 2: multiplication formulas (rectangular output)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 for (int i = 1; i <= 9; i++) 14 { 15 for (int j = 1; j <= 9; j++) 16 { 17 Console.Write("{0}*{1}={2}\t",i,j,i*j); 18 } 19 Console.WriteLine(); 20 } 21 Console.ReadKey(); 22 } 23 } 24 }
Exercise 3: Cycle 5 entry information and calculate the average age, if the input data appear negative or greater than 100, stop immediately and report an error input
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int sum = 0; 14 bool b = true; 15 for( Int I = 0 ; I < . 5 ; I ++ ) 16 { . 17 Console.WriteLine ( " Please enter {0} individual performance " , I + . 1 ); 18 is the try . 19 { 20 is int Age = Convert.ToInt32 (Console.ReadLine ()); 21 is IF (Age> = 0 && Age <= 100 ) 22 is { 23 is SUM + = Age; 24 25 } 26 is the else 27 { 28 Console.WriteLine ( " Age input error " ); 29 B = to false ; 30 BREAK ; 31 is } 32 } 33 is the catch 34 is { 35 36 Console.WriteLine ( " Age entered is incorrect, the program exits !!! " ); 37 [ B = to false ; 38 is BREAK ; 39 } 40 41 is } 42 is IF (B) 43 is { 44 is Console.WriteLine ( " . 5 is the average age of individuals 0} { " , SUM / . 5 ); 45 } 46 is the Console.ReadKey (); 47 } 48 } 49 }
In the previous sections, we introduced the keyword break the cycle control program flow, let's re-introduce a.
continue: an immediate end to this cycle, determine the loop condition, if true, then enter the next cycle, or exit the loop.
Exercise 1: Achieved 7 divisible be calculated in addition to all integers between 1 and 100 while continue with
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int sum = 0; 14 int i = 1; 15 while (i < 100) 16 { 17 if (i % 7 == 0) 18 { 19 i++; 20 continue; 21 } 22 sum += i; 23 i++; 24 } 25 Console.WriteLine(sum); 26 Console.ReadKey(); 27 } 28 } 29 }
Exercise 2: Find out all the prime numbers within the 100 (divisible only by 1 and itself Digital)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 草稿 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 for (int i = 2; i <= 100; i++) 14 { 15 BOOL B = to true ; 16 for ( int J = 2 ; J <I; J ++ ) . 17 { 18 is IF (% I J == 0 ) // divisible by a prime number not . 19 { 20 is B = to false ; 21 is BREAK ; 22 is } 23 is } 24 IF (B) 25 { 26 is Console.WriteLine (I); 27 } 28 29 } 30 Console.ReadKey(); 31 } 32 } 33 }
Section 1-6 above all, we introduce the flow of the program control, the following primer we make a small, difficult and a focus lead-out after our method and object-oriented.
The following describes a first function: a random number (the Random ())
Proceed as follows:
(1) create an object capable of generating random numbers. Random r = new Random ();
(2) to make this object invokes a method of generating random numbers generated random numbers. int rNumber = r.Next (a, b);