The for statement is a loop statement used to repeatedly execute a block of statements. Components: 1) Initialization: Executed at the beginning of the loop. 2) Condition: Check before each iteration; if true, continue, if false, end. 3) Increment: Executed after each iteration. Rules: 1) All three parts are legal C expressions. 2) Omitting the condition will result in an infinite loop by default. 3) If increment is omitted, the default is 1. 4) The scope of loop variables is limited to the for statement.
Usage of for statement in C language
The for statement is a loop statement that allows you to execute it repeatedly A series of statements.
For statement syntax:
<code class="c">for (initialization; condition; increment) { // 要重复执行的语句 }</code>
For statement components:
Rules for for statements:
Usage of the for statement:
The for statement can be used in a variety of situations, including:
Example:
<code class="c">// 遍历一个数组 int arr[] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } // 重复执行代码块 10 次 for (int i = 1; i <= 10; i++) { printf("执行第 %d 次\n", i); } // 控制循环的步长 for (int i = 0; i < 10; i += 2) { printf("%d ", i); }</code>
The above is the detailed content of Usage and rules of for statement in C language. For more information, please follow other related articles on the PHP Chinese website!