Why is this code printing "123" instead just 3?

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

Why is this code printing "123" instead just 3?

问题

这段代码返回 '123' 而不是 '3' 的原因是因为在循环中,它使用了一个 for 循环来打印从 1 到 3 的数字。这导致在循环的每次迭代中,数字 1、2 和 3 都被打印出来,最终形成 '123'。

对于你的疑问,尝试通过谷歌搜索找到有用信息是一个好方法,但在这种情况下,这个问题相对基础,可能没有很多相关的结果。如果你想让循环仅打印 '3',你可以修改代码以适应你的需求。

英文:

I've got some code here:

int n = 0;
for(n = 1;n<4;n++)
printf("%d",n);
return 0;

Why does it return '123' instead of just '3'?

I tried to Google for this issue, but I couldn't find anything useful.

答案1

得分: 5

以下是翻译好的部分:

"After asking my question, I think I see what you are getting at."

"你在提问后,我想我明白你的意思了。"

"What you have with is functionally the same as"

"你所拥有的与功能上相同"

"Since the for loop expects a statement, either a block of statements enclosed in braces, or a single one terminated with a semicolon as you have in your example."

"因为for循环期望一个语句,可以是由大括号括起来的一组语句,也可以是以分号结尾的单个语句,就像你在示例中所做的那样。"

"If you wanted it to just print 3 and for whatever reason wanted to use a loop just to increment a number, you would want to provide it with an empty statement as such:"

"如果你只想要打印3,而出于某种原因想要使用循环来递增一个数字,你可以提供一个空语句,就像这样:"

"Both of which will only print 3."

"这两种方式都只会打印3。"

"Please note that because the variable n gets incremented and then checked, using your original bounds n < 4, the loop would end when n = 4 and thus 4 would be printed. I changed this in my last two examples."

"请注意,由于变量n被递增然后被检查,使用你的原始界限n < 4,循环将在n = 4时结束,因此会打印出4。我在我的最后两个示例中做了更改。"

"Also note the incorrect use of the term return, as some comments pointed out."

"还要注意,正如一些评论指出的那样,术语return的使用是不正确的。"

英文:

After asking my question, I think I see what you are getting at.

What you have with

    int n = 0;
    for(n = 1;n&lt;4;n++)
        printf(&quot;%d&quot;,n);
    return 0;

is functionally the same as

    int n = 0;
    for(n = 1;n&lt;4;n++)
    {
        printf(&quot;%d&quot;,n);
    }
    return 0;

Since the for loop expects a statement, either a block of statements enclosed in braces, or a single one terminated with a semicolon as you have in your example. If you wanted it to just print 3 and for whatever reason wanted to use a loop just in increment a number, you would want to provide it with an empty statement as such:

    int n = 0;
    for(n = 1;n&lt;3;n++);
    printf(&quot;%d&quot;,n);
    return 0;

or

    int n = 0;
    for(n = 1;n&lt;3;n++){}
    printf(&quot;%d&quot;,n);
    return 0;

Both of which will only print 3.

Please note that because the variable n gets incremented and then checked, using your original bounds n &lt; 4, the loop would end when n = 4 and thus 4 would be printed. I changed this in my last two examples. Also note the incorrect use of the term return, as some comments pointed out.

huangapple
  • 本文由 发表于 2023年2月9日 03:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390784.html
匿名

发表评论

匿名网友

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

确定