for循环中使用&&多条件判断无输出,如何打印6-9?
问题分析与修复方案
Hey there! Let's break down why your code isn't producing any output and fix it up.
First, let's look at your original code:
#include<stdio.h> int main(void) { for(int i = 0; (i > 5) && (i < 10); i++) // 打印数字 printf("%d\n",i); return 0; }
Why no output?
The core issue is your loop's starting value and condition. You initialize i to 0, but your loop only runs when (i > 5) && (i < 10) is true. Since 0 is way below 5, this condition fails immediately—your loop never even starts, so the printf line never gets executed.
Fixed Code
To print numbers 6 through 9, we need to adjust the loop to start at the right value and stop at the correct point:
#include<stdio.h> int main(void) { // Start i at 6, run as long as i is less than 10 (so i hits 6,7,8,9) for(int i = 6; i < 10; i++) printf("%d\n",i); return 0; }
Alternative Approach (if you want explicit range checks)
If you prefer to keep a broader loop and filter values inside it, this works too—though the first approach is more efficient:
#include<stdio.h> int main(void) { for(int i = 0; i < 10; i++) { if(i >= 6) { // Only print when i is 6 or higher printf("%d\n",i); } } return 0; }
内容的提问来源于stack exchange,提问作者SQLLER
相关产品推荐
相关产品推荐

