java和c中的for循环有什么区别吗?

时间:2022-03-13 22:28:32

for loop in c

for循环在c语言中

int i;
int n = 20;
for(i = 0; i + n; i-- ) {
     printf("-\n");

}

for loop in java

java for循环的

int i;
int n=20;
for (i = 0; i + n; i--) {
   System.out.println("-\n");
}

In the above example for loop in c is working fine(will print "-" 20 times).But for loop in java shows error as

在上面的例子中,c中的循环运行良好(将打印“-”20次)。但是在java中for循环显示错误。

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from int to boolean

线程“main”java.lang中的异常。错误:未解决的编译问题:类型不匹配:不能从int转换为boolean

why it shows this kind of error?

为什么会出现这种错误?

2 个解决方案

#1


10  

In C, 0 is considered false and the rest of number are interpreted as true. In Java, this doesn't work since it has a boolean type that is not an int, and an int cannot be directly converted into a boolean.

在C中,0被认为是假的,其余的数被解释为真。在Java中,这不起作用,因为它有一个不是int类型的布尔类型,并且int不能直接转换为boolean类型。

To fix the Java code, you should write the second part as a boolean expression:

要修复Java代码,应该将第二部分写成布尔表达式:

for (i = 0; (i + n) != 0; i--) {
    System.out.println("-\n");
}

While (i + n) != 0 may work, I would prefer to use (i + n) > 0, because if n starts at -1, this loop will work until i goes down to Integer.MIN_VALUE value, underflows to Integer.MAX_VALUE and go down to 1. To prevent that behavior (in case is undesired), it would be better to write it like this:

当(i + n) != 0可以工作时,我更喜欢使用(i + n) >,因为如果n从-1开始,这个循环将一直工作到我降到整数。MIN_VALUE值,欠流为整型。MAX_VALUE一直到1。为了防止这种行为(在不希望发生的情况下),最好这样写:

for (i = 0; (i + n) > 0; i--) {
    System.out.println("-\n");
}

From @Lundin's comment, looks like your C code should be fixed as well:

从@Lundin的评论,看起来您的C代码也应该是固定的:

//or use my proposed fix by using > rather than !=
for(i = 0; (i + n) != 0; i-- ) {
    printf("-\n");
}

#2


3  

The original version of C didn't have booleans, so everyone used integers instead. The language would interpret anything non-zero as true, and zero as false.

C的原始版本没有布尔值,所以每个人都使用整数。语言会把任何非零的东西解释为真,而把零解释为假。

But Java is stricter about what's a boolean and what's an integer - you actually need to use a boolean expression (such as i + n != 0) to check whether the loop should continue.

但是Java对什么是布尔值,什么是整数更严格——实际上需要使用布尔表达式(比如i + n != 0)来检查循环是否应该继续。

#1


10  

In C, 0 is considered false and the rest of number are interpreted as true. In Java, this doesn't work since it has a boolean type that is not an int, and an int cannot be directly converted into a boolean.

在C中,0被认为是假的,其余的数被解释为真。在Java中,这不起作用,因为它有一个不是int类型的布尔类型,并且int不能直接转换为boolean类型。

To fix the Java code, you should write the second part as a boolean expression:

要修复Java代码,应该将第二部分写成布尔表达式:

for (i = 0; (i + n) != 0; i--) {
    System.out.println("-\n");
}

While (i + n) != 0 may work, I would prefer to use (i + n) > 0, because if n starts at -1, this loop will work until i goes down to Integer.MIN_VALUE value, underflows to Integer.MAX_VALUE and go down to 1. To prevent that behavior (in case is undesired), it would be better to write it like this:

当(i + n) != 0可以工作时,我更喜欢使用(i + n) >,因为如果n从-1开始,这个循环将一直工作到我降到整数。MIN_VALUE值,欠流为整型。MAX_VALUE一直到1。为了防止这种行为(在不希望发生的情况下),最好这样写:

for (i = 0; (i + n) > 0; i--) {
    System.out.println("-\n");
}

From @Lundin's comment, looks like your C code should be fixed as well:

从@Lundin的评论,看起来您的C代码也应该是固定的:

//or use my proposed fix by using > rather than !=
for(i = 0; (i + n) != 0; i-- ) {
    printf("-\n");
}

#2


3  

The original version of C didn't have booleans, so everyone used integers instead. The language would interpret anything non-zero as true, and zero as false.

C的原始版本没有布尔值,所以每个人都使用整数。语言会把任何非零的东西解释为真,而把零解释为假。

But Java is stricter about what's a boolean and what's an integer - you actually need to use a boolean expression (such as i + n != 0) to check whether the loop should continue.

但是Java对什么是布尔值,什么是整数更严格——实际上需要使用布尔表达式(比如i + n != 0)来检查循环是否应该继续。