有办法在运行for循环之前判断它会执行多少次吗?

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

Is there a way to tell how many times a for loop will execute before running it?

问题

以下是翻译好的部分:

我正在学习 Java 入门课程,并参加了一个练习测验。这些测验不计分,不会计入我们的成绩,所以我想在这里发帖。

在正常情况下,我通常会复制代码并将其粘贴到集成开发环境(IDE)中,但我想知道是否有一种逻辑思考的方法,以便我可以找出答案。在真正的考试中,我们不允许使用 Eclipse 或类似工具来找出答案,而手动计算循环次数会花费太长时间。

以下是问题:

在 for 循环中,打印语句会执行多少次?

for(int i = 0; i < 347589; i++) {
   System.out.print("Give me coffee!");
}
英文:

I am taking an intro to java course and I am taking a practice quiz. These quizzes are not graded and do not count towards our grade so I thought I would post here.

Under normal circumstances, I would just copy the code and paste it into an IDE but I wanted to know if there is a way to think about this logically so I can figure out the answer. On real exams, we are not allowed to use eclipse or anything like that to figure out an answer and this question would take way too long to do it by hand and count how many times it goes through the for loop.

Here is the question:

How many times does the print statement in the for loop execute?

    for(int i = 0; i &lt;347589; i++) {
       System.out.print(&quot;Give me coffee!&quot;);
    }

答案1

得分: 2

你基本上可以将for循环的上限视为答案,即在这种情况下为347,589。为了得出这个答案,考虑一个上限小得多的for循环:

for (int i=0; i < 5; ++i) {
    System.out.println("给我咖啡!");
}

通过观察,我们可以看到这个循环将在以下情况下执行:

i = 0
i = 1
i = 2
i = 3
i = 4

i = 5时,它将不会执行,因为不满足条件。因此,它执行了五次,这与for循环的上限相同。我们可以将这个推广到你的问题中的循环,得出347,589作为答案。

英文:

You can basically just take the upper bound of the for loop as an answer, which is 347,589 in this case. To arrive at this answer, consider a for loop with a much smaller bound:

for (int i=0; i &lt; 5; ++i) {
    System.out.println(&quot;Give me coffee!&quot;);
}

By inspection, we can see that this loop will execute when:

i = 0
i = 1
i = 2
i = 3
i = 4

It won't execute when i = 5, because that fails the condition. So, it executes five times, which is the same as the upper bound of the for loop. We can extrapolate this to the loop in your problem to get 347,589 as the answer.

答案2

得分: 1

没有通用的方法来确定在运行循环之前循环将执行多少次。这个事实被称为停机问题的不可判定性。对于循环只是while循环的语法糖,因此这也适用于for循环。停机问题是许多精彩的书籍的主题,其中包括Douglas Hofstadter所著的*《哥德尔、艾舍尔、巴赫》*。

然而,你给出的for循环非常简单:它增加i直到i不再满足i &lt; 347589。变量i从0开始逐渐增加:i++相当于i = i + 1;。因此,i取所有连续的值:0, 1, 2, 3, 4, ...,347588,每次都会执行System.out.println("Give me coffee!");这一行。

然后i取值347589,不再满足i &lt; 347589,所以循环停止。

总共打印了347589次“Give me coffee!”。

如果你不喜欢for循环的语法,你的代码等同于使用while循环的以下代码:

int i = 0;
while (i &lt; 347589)
{
    System.out.print("Give me coffee!");
    i = i + 1;
}
英文:

There is no general way to figure out how many times a while loop will execute before running it. This fact is known as the undecidability of the halting problem. For loops are just syntactic sugar for while loops, so this applies to for loops as well. The halting problem is the subject of a great many awesome books, among which Gödel, Escher and Bach by Douglas Hofstadter.

However, the for loop you give is extremely simple: it increases i until i no longer satisfies i &lt; 347589. The variable i starts at 0 and increases one by one: i++ is equivalent to i = i + 1;. So i takes all the successive values: 0, 1, 2, 3, 4, ...,347588 and every time, the line System.out.println(&quot;Give me coffee!&quot;); is executed.

Then i takes the value 347589 and no longer satisfies i &lt; 347589, so the loop halts.

In total, "Give me coffee!" was printed 347589 times.

If you don't like the syntax for for loops, your code is equivalent to the following code using a while loop:

int i = 0;
while (i &lt; 347589)
{
    System.out.print(&quot;Give me coffee!&quot;);
    i = i + 1;
}

答案3

得分: 0

因为你有一个简单的for循环,它只执行同一条语句多次,所以你可以简单地计数。你可能知道循环以i=0开始,在第一次执行时添加1,直到条件i<347589为false为止。所以:

i=0 -> 一次 (1.)
i=1 -> 两次 (2.)
...
i=347588 -> 347589次 (347589.)
i=347589 -> 循环停止

所以打印语句将被执行347589次。

英文:

As you have a simple for-loop that executes only one statement multiple times, you can simply count. You might know that the loop starts with i=0 with the first execution and adds 1 after each execution until the condition i&lt;347589 is false. So:

i=0 -&gt; once (1.)
i=1 -&gt; twice (2.)
...
i=347588 -&gt; 347589 times (347589.)
i=347589 -&gt; loop stops.

so the print statement will be executed 347589 times.

答案4

得分: 0

很明显,FOR循环不是Java特定的问题。

为了正确理解FOR循环,让我们来看一个简单的示例。

for (int i = 0; i < 10; i++) {
    System.out.println(i); 
}

这段代码将打印出从0到10(不包括10)的所有数字。
因此,范围0-10有10个不同的值:

0
1 
2
3 
4 
5 
6 
7 
8 
9

i变成10时,FOR循环的语句(i < 10)将不满足,循环将被中断。

所以,在你的情况下,使用相同的逻辑,"Give me coffee!"将被打印出347589次。

英文:

Obviously, FOR loop is not Java specific question.

To understand FOR loop properly let's take an easy example. <br>

  for (int i = 0; i &lt; 10; i++) {
     System.out.println(i); 
  }

This code will print all number from 0 to 10 (Excluding). <br>
So, the range 0-10 has 10 different values:<br>

 0
 1 
 2
 3 
 4 
 5 
 6 
 7 
 8 
 9

When i becomes 10 FOR loop statement (i<10) will not be satisfied and the loop will be broken. <br>

So, in you case &quot;Give me coffee!&quot; will be printed 347589 times with the same logic.

答案5

得分: 0

在课程中,他们可能只是想检查你对编码基础的了解。在这些类型的问题中,你应该特别注意以下4-5个要点:

1- 索引的起始点。

2- 索引的结束点,还要检查该值是否被包括在内。

3- 索引的增加/减少,索引是增加/减少1还是其他某个值或模式。

4- 循环内是否有任何中断语句,这也将表明循环将持续运行直到特定条件。

在你提供的示例中,起始点是0,结束点是347589(不包括在内),增量为1,因此循环将运行。347589 - 0 + 1(基于0的索引)= 347589次。

英文:

In the course they might just want to check your knowledge towards the coding basics. In these type of problems you should notice most importantly 4-5 things.

1- Starting point of index.

2- Ending point of index, also check whether that value in included or not.

3- Increment/Decrement of index, whether index is increased/ decreased by 1 or some other value or pattern.

4- Is their any break statement inside the loop, which will also indicate that loop will run till specific condition.

In the example you gave starting point is 0 and end point is 347589 (excluded) with increment of 1, so loop will run. 347589 - 0 + 1( 0 based index) = 347589 times.

huangapple
  • 本文由 发表于 2020年9月15日 22:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63904175.html
匿名

发表评论

匿名网友

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

确定