我们能够独立地在 for 循环开头使用分号吗?

huangapple go评论77阅读模式
英文:

Can we use semicolon independently at the beginning of a for-loop?

问题

public class ReverseNumber {

    public static void main(String[] args) {

        int num = 1234567, reversed = 0;

        for (; num != 0; num /= 10) { // <-- meaning of the first ; ?
            int digit = num % 10;

            reversed = reversed * 10 + digit;
        }

        System.out.println("Reversed Number: " + reversed);
    }
}

这个for循环中第一个分号之前的部分是循环的初始化部分,第二个分号之前的部分是循环的条件部分,决定是否继续循环,第二个分号之后的部分是循环的迭代部分,用于更新循环控制变量。

英文:

What is the meaning of this type of syntax in the for-loop?

public class ReverseNumber {

    public static void main(String[] args) {

        int num = 1234567, reversed = 0;

        for (; num != 0; num /= 10) { // <-- meaning of the first ; ?
            int digit = num % 10;
            
            reversed = reversed * 10 + digit;
        }

        System.out.println("Reversed Number: " + reversed);
    }
}

答案1

得分: 2

一个 for 语句由三个部分组成 for(语句A; 语句B; 语句C)

  • 语句A 是初始化语句。它只执行一次。通常情况下,您在for循环中创建要使用的变量。
  • 语句B 是停止条件。它在每次开始for循环时执行,用于确定是否应该再次执行for循环。
  • 语句C 是在每次for循环结束时执行的操作(比如递增您的变量)。

它们都是可选的。
以下是一些for循环的示例:

for (;;) {
    // 无限循环
}

for (int i = 0; i < 10; i++) {
    // 循环10次,并在每次循环时递增i
}
int b = i; // 不可能的,i不再存在
// 这个for循环等同于:
int i = 0;

for (; i < 10; i++) {
    // 几乎相同。不同之处在于在for循环之后可以访问i
}
int b = i; // 可以,因为i仍然可见。

for (int a = 0;; a++) {
    // 无限循环(因为没有停止条件)来递增变量a
    System.out.println(a);
}
for (int constant = 0;;) {
    // 无限循环(没有停止条件),您可以使用常量变量(但始终为0)
    System.out.println(constant);
}
英文:

A for statement is composed of 3 parts for(statement A; statement B; statement C):

  • Statement A is the initializer statement. It is executed only one time. Usually, you create your variables you want to use in the for loop
  • Statement B is the stop condition. It is executed every time at the beginning of the for loop, to determine if you should do the for loop one more time
  • Statement C is the operation to execute every time at the end of the for loop (like incrementing your variable for instance).

All of them are optional.
Here some examples of for loop:

for(;;) {
    // infinite loop
}

for(int i = 0; i &lt; 10; i++) {
    // Loop 10 times and increment i each time
}
int b = i; // impossible. i does not exist anymore
// this for is equivalent to :
int i = 0;

for(; i &lt; 10; i++) {
    // Almost the same. The difference here is that I can be accessed after the for loop
}
int b = i; // possible, because i is still visible. 

for(int a = 0;; a++) {
    // Infinite loop (because no stop condition) to increment variable a
    System.out.println(a);
}
for(int constant = 0;;) {
    // Infinite loop (no stop condition) and you can use constant variable (but will always be 0)
    System.out.println(constant);
}

答案2

得分: 0

如果您在循环开始时使用分号;,这意味着您尚未初始化其值。
在下面的示例中:

int num = 1234567, reversed = 0;
for(;num != 0; num /= 10) {
int digit = num % 10;
reversed = reversed * 10 + digit;
}

它已经在获取变量“num”的值。

如果您注释掉“num”的赋值,您将会得到一个错误。

英文:

if you are using ; in the beginning of the loop that means , you don't have already initialized its value
in the below exapmle:

int num = 1234567, reversed = 0;
for(;num != 0; num /= 10) {
int digit = num % 10;
reversed = reversed * 10 + digit;
}

it is already picking the value of "num"

In case if you comment the num value you will get an error.

huangapple
  • 本文由 发表于 2020年7月25日 21:08:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63088711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定