Help with code please

can anyone explain to me what this line of code does

count --;

much appreciated
i have used it in a countdown program (a friend of mine said it would help break up the countdown on separate lines) but I cannot get in contact with him atm.
it's a quick way to write count = count - 1.

It's sister operation, --count, behaves the mostly the same way, but with the following difference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int i = 1;
int j = 1;

if (i--)  // This statement checked if i was not zero, and then subtracted 1 from it
 {
    // Code here will execute
 }

if (--j)  // This statement subtracted 1 from j and then checked if it was not zero
{
  // code here will not execute
}

// i and j now both equal zero

About 1/3 the way down the page:

http://www.cplusplus.com/doc/tutorial/operators/
Last edited on
so what part of this code breaks the lines up instead of having 1 line of 10 9 8 7 6 5 4 3 2 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
    int count; 
    count = 10;
    while ( count > 0 ) 
    {
          cout << count << endl; /
          count --;
          }
          cout << "BLAST OFF" << endl; 
          system("Pause"); 
          return 0;
}
Fun thing about programing, just delete things and see what happens :)
I have deleted it prior to posting, the result is the code just repeats the number 10 an infinite amount of times, what I am confused with is which part of my code actually breaks the lines up, my program breaks lines for each integer it counts down and I am not sure what line of code specifies it to do so.
cout << count << endl;

The endl is actually what does a line break which is what I believe you're asking about.

When you set count = 10; you're giving that variable the value of 10.

The while ( count > 0 ) line actually loops everything between { and }, as long as count is a positive number, 1 through infinity.

The line cout << count << endl; actually displays the value of count on the screen with a line return, breaking the lines up. This is why you see count countdown.

The next line count --; subtracts 1 from the value of count each time which will eventually result in the value of count reaching 0 and breaking out of the while loop.

The line cout << "BLAST OFF" << endl; will display the message BLASTOFF after the number 1.

system("Pause"); displays a message similar to "Press any key to continue..." on the screen and waits for a keypress of some sort. This is typically used to keep the console window open and is known as a bad practice.

return 0; returns the value of 0 to the operating system. This informs the operating system that everything ran fine and there were no errors.
Last edited on
Topic archived. No new replies allowed.