The for loop is used to repeatedly execute a sequence of statements, to execute components a known number of times, iterate through data, count and accumulate values. Rules include: initialization (start of loop execution), condition (check before loop iteration), and increment (update after loop iteration).
Usage and rules of for loop in C language
What is for loop?
A for loop is a loop structure used to repeatedly execute a series of statements if a specific condition is met.
Syntax
<code class="c">for (initialization; condition; increment) { 循环体语句 }</code>
Rules
Usage
The for loop is usually used in the following situations:
Example
<code class="c">// 打印 1 到 10 的数字 for (int i = 1; i <= 10; i++) { printf("%d\n", i); }</code>
<code class="c">// 计算数组中所有元素的和 int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; }</code>
Note
The above is the detailed content of Usage and rules of for loop in C language. For more information, please follow other related articles on the PHP Chinese website!