說(shuō)明迭代場(chǎng)景的簡(jiǎn)單場(chǎng)景是顯示數(shù)字列表。
for循環(huán)的一般模式是:
for(init_condition; control_condition ; action_per_iteration){ loop_statement; } next_statement;
要重復(fù)的語(yǔ)句由loop_statement表示。
init_condition
通常將一個(gè)初始值設(shè)置為一個(gè)循環(huán)控制變量。
循環(huán)控制變量跟蹤循環(huán)已經(jīng)完成了多少次。
您可以聲明和初始化這里用逗號(hào)分隔的同一類型的幾個(gè)變量。在init_condition
中定義的所有變量都是循環(huán)本地的,循環(huán)結(jié)束后不會(huì)存在。
control_condition是一個(gè)邏輯表達(dá)式,其值為true或false。
這決定循環(huán)是否應(yīng)該繼續(xù)執(zhí)行。
只要這個(gè)條件的值為true,循環(huán)就會(huì)繼續(xù)。
它通常檢查循環(huán)控制變量的值。
control_condition在循環(huán)開始時(shí)進(jìn)行測(cè)試,而不是結(jié)束。
如果control_condition開始為false,則loop_statement將不會(huì)被執(zhí)行。
action_per_iteration在每個(gè)循環(huán)迭代結(jié)束時(shí)執(zhí)行。通常是一個(gè)或多個(gè)循環(huán)控制變量的遞增或遞減。
在每次循環(huán)迭代時(shí),執(zhí)行l(wèi)oop_statement。
循環(huán)被終止,并且一旦control_condition為false,就繼續(xù)執(zhí)行next_statement。
以下代碼顯示如何將來(lái)自1的整數(shù)與用戶指定的數(shù)字相加。
#include <stdio.h>
int main(void)
{
unsigned long long sum = 0LL; // Stores the sum of the integers
unsigned int count = 0; // The number of integers to be summed
// Read the number of integers to be summed
printf("\nEnter the number of integers you want to sum: ");
scanf(" %u", &count);
// Sum integers from 1 to count
for(unsigned int i = 1 ; i <= count ; ++i)
sum += i;
printf("\nTotal of the first %u numbers is %llu\n", count, sum);
return 0;
}
我們不需要在for循環(huán)語(yǔ)句中放置任何參數(shù)。最小的循環(huán)看起來(lái)像這樣:
for( ;; ){ /* statements */ }
上面的代碼生成以下結(jié)果。
以下代碼是進(jìn)行迭代,直到數(shù)值小于10。
#include <stdio.h>
int main() {
int i;
for(i=0;i<10;i++){
printf("data %d\n",i);
}
return 0;
}
上面的代碼生成以下結(jié)果。
您可以使用for循環(huán)執(zhí)行語(yǔ)句多次。
要顯示從1到10的數(shù)字,您可以這樣寫:
#include <stdio.h>
int main(void)
{
int count = 0;
for(count = 1 ; count <= 10 ; ++count)
{
printf(" %d", count);
}
printf("\nAfter the loop count has the value %d.\n", count);
return 0;
}
上面的代碼生成以下結(jié)果。
您不需要指定第一個(gè)控制表達(dá)式,而for循環(huán)可能如下所示:
#include <stdio.h>
int main(void)
{
int count = 1;
for( ; count <= 10 ; ++count) {
printf(" %d", count);
}
printf("\nAfter the loop count has the value %d.\n", count);
return 0;
}
上面的代碼生成以下結(jié)果。
下面的代碼使用for循環(huán)來(lái)繪制一個(gè)盒子。
#include <stdio.h>
int main(void)
{
printf("\n**************"); // Draw the top of the box
for(int count = 1 ; count <= 8 ; ++count)
printf("\n* *"); // Draw the sides of the box
printf("\n**************\n"); // Draw the bottom of the box
return 0;
}
上面的代碼生成以下結(jié)果。
這個(gè)例子演示如何在for循環(huán)中的第三個(gè)控制表達(dá)式中進(jìn)行計(jì)算:
#include <stdio.h>
int main(void)
{
unsigned long long sum = 0LL; // Stores the sum of the integers
unsigned int count = 0; // The number of integers to be summed
// Read the number of integers to be summed
printf("\nEnter the number of integers you want to sum: ");
scanf(" %u", &count);
// Sum integers from 1 to count
for(unsigned int i = 1 ; i <= count ; sum += i++);
printf("\nTotal of the first %u numbers is %llu\n", count, sum);
return 0;
}
上面的代碼生成以下結(jié)果。
以下代碼顯示將循環(huán)控制變量遞增一定數(shù)量,并將前n個(gè)整數(shù)向后舍入。
#include <stdio.h>
int main(void)
{
unsigned long long sum = 0LL; // Stores the sum of the integers
unsigned int count = 0; // The number of integers to be summed
// Read the number of integers to be summed
printf("\nEnter the number of integers you want to sum: ");
scanf(" %u", &count);
// Sum integers from count to 1
for(unsigned int i = count ; i >= 1 ; sum += i--);
printf("\nTotal of the first %u numbers is %llu\n", count, sum);
return 0;
}
上面的代碼生成以下結(jié)果。
您可以使用for循環(huán)來(lái)限制用戶的輸入量。
#include <stdio.h>
int main(void)
{
int chosen = 5; // The lucky number
int guess = 0; // Stores a guess
int count = 3; // The maximum number of tries
printf("\nGuessing game.");
for( ; count > 0 ; --count)
{
printf("\nYou have %d tr%s left.", count, count == 1 ? "y" : "ies");
printf("\nEnter a guess: "); // Prompt for a guess
scanf("%d", &guess); // Read in a guess
// Check for a correct guess
if(guess == chosen)
{
printf("\nCorrect!\n");
return 0; // End the program
}
else if(guess < 1 || guess > 10) // Check for an invalid guess
printf("between 1 and 10.\n ");
else
printf("Sorry, %d is wrong. My number is %s than that.\n",
guess, chosen > guess ? "greater" : "less");
}
printf("\nYou have had three tries and failed. The number was %d\n", chosen);
return 0;
}
上面的代碼生成以下結(jié)果。
你可以將一個(gè)循環(huán)放在另一個(gè)循環(huán)中。
以下代碼顯示如何輸出給定寬度和高度的框。
#include <stdio.h>
int main(void) {
int width = 10;
int height = 30;
// Output the top of the box with width asterisks
for(unsigned int i = 0 ; i < width ; ++i)
printf("*");
// Output height-2 rows of width characters with * at each end and spaces inside
for(unsigned int j = 0 ; j < height - 2 ; ++j) {
printf("\n*"); // First asterisk
// Next draw the spaces
for(unsigned int i = 0 ; i < width - 2 ; ++i)
printf(" ");
printf("*"); // Last asterisk
}
// Output the bottom of the box
printf("\n"); // Start on newline
for(unsigned int i = 0 ; i < width ; ++i)
printf("*");
printf("\n"); // Newline at end of last line
return 0;
}
上面的代碼生成以下結(jié)果。
以下代碼使用嵌套循環(huán)來(lái)獲得連續(xù)整數(shù)序列的和。
#include <stdio.h>
int main(void)
{
unsigned long sum = 0UL; // Stores the sum of integers
unsigned int count = 5; // Number of sums to be calculated
for(unsigned int i = 1 ; i <= count ; ++i)
{
sum = 0UL; // Initialize sum for the inner loop
// Calculate sum of integers from 1 to i
for(unsigned int j = 1 ; j <= i ; ++j)
sum += j;
printf("\n%u\t%5lu", i, sum); // Output sum of 1 to i
}
printf("\n");
return 0;
}
上面的代碼生成以下結(jié)果。
更多建議: