C # "Five" Process Control (2)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.