1. The foreach statement of the iteration statement
1. Foreach provides a shortcut for a for statement, and also facilitates a more consistent collection class.
2. Format:
for (initial value; judging condition; growth value) { code body;}
foreach (Type variable in collection) { code body;}
Supplement: String types can be considered as a collection of char types
String String char character
My name is Snowskywalker
Examples:
According to the English sentence you entered, the program implements the word line output to the screen;
2, the new statement.
Char. Iswhitespace (c);
3. foreach statement
Each time the included code is executed, the loop variable reads one of the elements in the collection in turn.
The loop variable here is just a read-only local variable, and if the compiler is modified, an error occurs.
Program:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacethe foreach{of _5._8 iteration statementsclassProgram {Static voidMain (string[] args) { //for (int i = 1;i <= 50;i++)//{ //Console.WriteLine (i); //} //for (int i = 0; I <=; i++)//{ //Console.WriteLine (i); //} //recognize statements as words and output rows by line//statement with a string type, with char in the letterConsole.WriteLine ("Please enter an English sentence:"); stringstr =Console.ReadLine (); Console.WriteLine ("The sentence you entered contains the following words:"); foreach(CharWordinchstr) { if(Char. Iswhitespace (Word)) {Console.WriteLine (); } Else{console.write (word);
Word = ' t '; The iteration variable of the foreach statement does not allow the re-assignment of the value}} console.readkey (); } }}
Operation Result:
C # "Five" Process Control (2)