Let's go through the answer together.
When I run this program, we start out with x being 0,
and then this while loop says as long as True is true, keep incrementing x.
So now x is 1, it's 2, it's 3, it's 4, it's 5,
and we never actually get to this line about the print.
So does this program print 0? Not so much.
Control flow goes here, here, here, then back,
and we stay in this while loop forever.
It doesn't print out this huge number 4,294,967,296,
which is, I think, pretty close to, let's say, 2 to the 32.
This is a reasonable guess if you think,
"Oh, the integer is going to max out at some value based on computer hardware." But no.
There's a thought that it raises an exception.
Some badly made Python interpreters might raise an exception on this, but it should not.
If we had lots of computing resources, this would run forever.
And similarly, it also never prints out -1.
The correct answer is none of these things. It loops forever.
All right. So if this program naturally loops forever and we're writing an interpreter
that exactly simulates this program by following all of its steps,
our interpreter is also going to loop forever.
If someone had written this program in JavaScript
and put it in the middle of a web page, our web browser would loop forever
and never actually render the resulting web page.
答えを一緒に見ていきましょう
このプログラムを走らせるとxは0で始まります
このループはTrueが真の間はxを加算し続けます
xは1、2、3…
そして絶対にこのprint文にはたどり着きません
つまりこれは0を出力しません
制御はここに来て 戻ります
このwhileループに留まり続けるのです
この巨大な数も出力しません
ちなみに2の32乗だと思います
これを選んだ人は この数値が
コンピュータのハードウェアに起因する値の中で
最大になるのだと思ったのでしょう
いい予想ですが違います
例外を発生させるという予想もできます
できの悪いPythonインタプリタなら
例外を発生させるかもしれませんが本来は違います
計算資源がある限り走り続けます
同様に-1も出力しません
正解は“この中にはない”です
永遠にループし続けます
さて このプログラムが永遠にループするとして
各ステップを正確にシミュレートする
プログラムを書くとします
そうするとインタプリタも
永遠にループしてしまいます
もし誰かがJavaScriptで
Webページの中に書いていたとしたら
私たちのWebブラウザも永遠にループしてしまい
Webページの表示ができなくなってしまいます
Vamos ver as respostas juntos.
Quando eu executo este programa, começamos com x = 0,
e então este while loop diz: enquanto True é True, continue incrementando x.
Então, agora x é 1, 2, 3, 4, 5,
e nunca chegamos de fato a esta linha do print.
Então, este programa imprime 0? Claro que não.
O controle de fluxo vem para cá, aqui, aqui, depois de volta,
e permanecemos neste loop para sempre.
Ele não imprime este grande número 4294967296,
que é, eu acho, muito próximo de, digamos, 2 elevado a 32.
Esta seria uma opção razoável, se você pensasse que
"Oh, o inteiro será o máximo valor possível, com base no hardware do computador". --
Se pensasse que isso resulta em uma exceção.
Alguns interpretadores Python mal feitos podem levantar uma exceção neste caso, mas não deveriam.
Se temos recursos computacionais ilimitados, isso deveria executar para sempre.
De modo análogo, isso nunca imprime -1.
A resposta ocorrera é nenhuma dessas opções. O programa fica em loop para sempre.
Ok. Então, se este programa fica em loop para sempre, e estamos escrevendo um interpretador
Que simula exatamente este programa, seguindo todos os seus passos,
Nosso interpretador também irá ficar em loop para sempre.
Se alguém escrevesse este programa em JavaScript
e o colocasse no meio de uma página web, nosso browser ficaria em loop para sempre,
e nunca iria exibir a página resultante.