Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C For 循环示例


现实中的示例

为了演示 for 循环 的实际示例,让我们创建一个程序,它以十为单位计数到 100

示例

for (i = 0; i <= 100; i += 10) {
  printf("%d\n", i);
}
自己尝试 »

在这个示例中,我们创建一个程序,它只打印 0 到 10(包含 0 和 10)之间的偶数

示例

for (i = 0; i <= 10; i = i + 2) {
  printf("%d\n", i);
}
自己尝试 »

这里我们只打印奇数

示例

for (i = 1; i < 10; i = i + 2) {
  printf("%d\n", i);
}
自己尝试 »

在这个示例中,我们打印 2 的幂,直到 512

示例

for (i = 2; i <= 512; i *= 2) {
  printf("%d\n", i);
}
自己尝试 »

在这个示例中,我们创建一个程序,它打印指定数字的乘法表

示例

int number = 2;
int i;

// 打印数字 2 的乘法表
for (i = 1; i <= 10; i++) {
  printf("%d x %d = %d\n", number, i, number * i);
}

return 0;
自己尝试 »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.