Here we learn a cyclic structure
while loop
grammar:
while (loop condition)
{
Loop body;
}
Execution: while the program is running, the first determined cycle conditions while brought in parentheses is satisfied, returns true is satisfied, the loop body is executed, executing the
After once again determine the loop condition, was established to continue the loop, if not established, a quick jump out.
Let's look a little practice:
. 1 the using the System; 2 the using the System.Collections.Generic; . 3 the using the System.Linq; . 4 the using the System.Text; . 5 the using System.Threading.Tasks; . 6 . 7 namespace Panel . 8 { . 9 class Program 10 { . 11 static void the Main ( String [ ] args) 12 is { 13 is // find all integers and 1-100 14 @ loop: accumulation process 15 @ cycling conditions: I <= 100 16 int i = 1; 17 int sum = 0; 18 while (i<=100) 19 { 20 sum += i; 21 i++; 22 } 23 Console.WriteLine(sum); 24 Console.ReadKey(); 25 } 26 } 27 }
Here to talk about a very important break keyword
(1) switch-case structure can bounce
(2) out of the current cycle.
Consider the following example of a
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Panel 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 int i = 1; 14 int j = 1; 15 while(I <= 10 ) 16 { . 17 while (J <= 10 ) 18 is { . 19 Console.WriteLine ( " I was inside the while loop " ); 20 is J ++ ; 21 is BREAK ; 22 is } 23 is Console.WriteLine ( " I am out the while loop " ); 24 I ++ ; 25 } 26 is the Console.ReadKey (); 27 } 28 } 29 }
This example illustrates the effect of a good break of
break generally not used alone, but used in conjunction with determining if expressed, when certain conditions are met, the not recycled.
Everyone to practice several codes
Exercise 1
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Panel 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string userNmae = string.Empty; 14 string userPwd = string.Empty; 15 the while (! UserNmae = " ADMIN " ! || UserPwd = " 888888 " ) 16 { . 17 Console.WriteLine ( " Please enter the user name " ); 18 is userNmae = Console.ReadLine (); . 19 Console.WriteLine ( " Enter password " ); 20 is UserPwd = Console.ReadLine (); 21 is } 22 is Console.WriteLine ( " login success " ); 23 Console.ReadKey(); 24 } 25 } 26 }
Exercise 2: Enter the number of classes, followed by input student achievement, grade point average and class calculate the total score.
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Panel 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Console.WriteLine("请输入班级人数"); 14 int count =Convert.ToInt32 (Console.ReadLine ()); 15 int I = . 1 ; 16 int SUM = 0 ; . 17 18 is the while (I <= COUNT) . 19 { 20 is Console.WriteLine ( " Please enter {0} examinations INSTITUTE " , I); 21 is int score = Convert.ToInt32 (Console.ReadLine ()); 22 is SUM + = score; 23 is } 24 Console.WriteLine ( " {0} class personal total score is the average score of {1} {2 } " , COUNT, SUM, SUM /count); 25 Console.ReadKey(); 26 } 27 } 28 }