Curly brackets after for statement

前端 未结 4 1528
渐次进展
渐次进展 2021-01-16 16:35

I\'m a newbie. Wrote a code to print sum of number from 1 to 10. Here\'s what happened;

for(a = 1; a<=10; a++)
sum += a;
cout<
<
4条回答
  •  北海茫月
    2021-01-16 16:54

    because:

    for(a = 1; a<=10; a++)
    sum += a;
    cout<

    is like saying:

    for(a = 1; a<=10; a++) {
        sum += a;
    }
    cout<

    When you do this, it prints the number once rather than upon each iteration.

提交回复
热议问题