- Learn C# in 7 days
- Gaurav Aroraa
- 163字
- 2021-07-08 09:51:29
The for loop
The for loop is similar to other loops that help run a statement or code block repeatedly until an expression evaluates to false. The for loop takes three sections: the initializer, condition, and iterator, where the initializer section executes first and only once; this is nothing but a variable to start a loop. The next section is condition, and if it evaluates to true, then only body statements are executed; otherwise it terminates the loop. The third and most important section is incremental or iterator, which updates the loop control variable. Let's take a look at the following code snippet:
private static void ForStatementExample() { WriteLine("for loop example."); Write("Enter repeatitive length:"); int length = Convert.ToInt32(ReadLine()); for (intcountIndex = 0; countIndex < length; countIndex++) { WriteLine(newstring('*', countIndex)); } }
The preceding code snippet is a working example of a for loop. Here, our code statement within the for loop block will executive repeatedly until the countIndex< length expression evaluates to false.