-4

I'm new to C and don't understand this type of for loop syntax.

for(int i(9), j(0); i > j; i--, j++)
cout << i;

This gives the result of 98765, but why?

I'm used to loops like this:

for(int i = 9, int j = 0; i>j; i--, j++){
 System.out.println(i);
}

I see that i is being initialized to 9 and j to 0, but how does it get a number that big?

David Yaw
  • 27,383
  • 4
  • 60
  • 93
LC12382
  • 97
  • 2
  • 10
  • 5
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Jan 13 '16 at 15:36
  • `cout << i << endl;` will produce same output as the one in Java source – StahlRat Jan 13 '16 at 15:49
  • 1
    What are you actually confused about? The integer initialisations, the use of comma operator, or why your output isn't flushed to the terminal? – πάντα ῥεῖ Jan 13 '16 at 15:52

6 Answers6

10

It is printing 9,8,7,6,5 as expected without any commas. Its not one big number.

System.out.println() - Prints a new line at the end.

cout - Behaves in a similar way like System.out.print() where it doesn't print a newline at the end.

Atri
  • 5,511
  • 5
  • 30
  • 40
  • Perhaps you can mention that `cout <<` unlike `System.out.println` does not add the `\n` character ;) – MrHug Jan 13 '16 at 15:37
  • `\n` is wrong. `endl` is right... http://stackoverflow.com/questions/7324843/why-use-endl-when-i-can-use-a-newline-character – Centril Jan 13 '16 at 15:45
  • I agree, but I think you'll find I never said that `\n` should be included. Instead I only remarked that `cout` does not include it, which is correct ;) – MrHug Jan 13 '16 at 15:46
  • 6
    @Centril I disagree. `std::endl` should only be used when you *need* to flush the stream. You shouldn't use it all the time. – TartanLlama Jan 13 '16 at 15:47
  • @TartanLlama Well... That is partially true - since \n is converted to CRLF where needed http://stackoverflow.com/questions/7372012/line-feed-and-carriage-return But one should explain the difference and you should at least flush the stream at the end. – Centril Jan 13 '16 at 15:53
3

Because you aren't printing a new line character:

for(int i(9), j(0); i > j; i--, j++)
    cout << i << '\n';
Simple
  • 13,992
  • 2
  • 47
  • 47
2

I'd just like to add something to the (good) answers already posted here:

int i(9);

does the same thing as

int i = 9;

It is called direct initialization. Take a look: Constructor of type int

I think this is what confused you in the first place.

Community
  • 1
  • 1
Eutherpy
  • 4,471
  • 7
  • 40
  • 64
0

Indentation does not matter in C++ since whitespace is ignored...

Thus,

for(int i(9), j(0); i > j; i--, j++)
cout << i;

Is the same as:

for(int i(9), j(0); i > j; i--, j++)
    cout << i;

Which is:

for(int i(9), j(0); i > j; i--, j++) {
    cout << i;
}

So it is not 1 number, but many.

This is the same in Java and most C-family languages.

Unlike println from System.out (a PrintStream)... << only writes what you pass to cout, which is usually STDOUT. To properly write a newline to cout you should use:

cout << i << endl;

See Why use endl when I can use a newline character? for more.

Community
  • 1
  • 1
Centril
  • 2,549
  • 1
  • 22
  • 30
  • 1
    This is not what OP was confused about I think – MrHug Jan 13 '16 at 15:40
  • @MrHug Well - it is not explicit from his question, so it might as well be what he asked about... The question is titled "C++ for loop syntax" not: "C++: How do I print a newline?" And `endl` is the most correct way to do it... NOT `\n`. Answers should bear everyone in mind, not just the OP... Those down votes are way way unfair. – Centril Jan 13 '16 at 15:44
  • You are correct, but then I would suggest tackling multiple issues in one post, not one that is least likely (as his confusion was about the "large number" not the fact that it compiled) – MrHug Jan 13 '16 at 15:47
  • Well; My edited answer has notes on both problems... yours does not. This makes me not want to answer things in SO... getting down votes for a formally correct answer because your assumption about what the questioner happened to ask happened to be more correct even tho the name of the question has nothing to do with newline and everything to do with C++ loops.. – Centril Jan 13 '16 at 15:51
  • I actually do not have an answer to this question, just hanging out here ;) Also I removed my downvote but am not willing to give you an upvote either. As the post you link indicates, you most often do not want to use `endl`, see the [accepted answer](http://stackoverflow.com/questions/5492380/what-is-the-c-iostream-endl-fiasco/5492605#5492605) – MrHug Jan 13 '16 at 15:53
  • @MrHug that is an answer on another question, not the one I linked... Besides - holding things in an internal buffer is not free either - so it entirely depends on the use case. – Centril Jan 13 '16 at 15:56
  • Oops you are right, it was linked in the top comment, I assumed it was the same question. My apologies. And I agree it depends on the usecase, but saying that using `\n` is wrong is a bit of a stretch then ;) Regardless, I'm moving on to the next question :) – MrHug Jan 13 '16 at 15:58
0

it's not printing a big number, it is just printing i one after another in each iteration/loop. To see use this

cout << i << endl;

or try different number for i or j

Yousuf Azad
  • 414
  • 1
  • 7
  • 17
0

This is not a big number. Actually, the output is 5 different numbers (9,8,7,6,5). You just have to print them in different lines by following methods:

cout<<i<<endl;

OR

cout<<i<<"\n";

OR

printf("%d\n",i);

and one more thing: int i(9) simply assigns value 9 to i. It is same as int i=9.

Vivek Mangal
  • 532
  • 1
  • 8
  • 24