Explain process

Can someone do me the favor of explaining how this output is 4? Thanks in advance. Struggling with the basics ain't fun!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "stdafx.h"
#include <iostream>

using namespace std;

int main() {
	int sum = 0; // variable declared
	for (int i = 1;i < 10;i += 2) { //init,argument,?
		if (10 / i == 2) //statement
			break; // exits the for loop?
		sum += i; // calculates the sum?
	}
	cout << sum; // outputs 4
	system("pause"); // "press any key to continue..."
}
Hi,

You can work this out yourself by adding some extra cout statements to see what the values of various variables or expressions are are each time:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "stdafx.h"
#include <iostream>

using namespace std;

int main() {
	int sum = 0; // variable declared
	for (int i = 1;i < 10;i += 2) { //init,argument,?
                cout << "variable i is " << i <<"\n";
		if (10 / i == 2) { // always uses braces even when there is 1 statement
                        // put cout statement here, the value of 10 / i
			break; // exits the for loop? // yes
                }
		sum += i; // calculates the sum?
                // put cout statement here to print value of sum variable
	}
	cout << sum; // outputs 4
	system("pause"); // "press any key to continue..."
}


Hope this helps a little bit :+)
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
   int sum = 0;
   
   for (int i = 1; i < 10; i += 2)
   { 
      std::cout << "i: " << i << ", sum: " << sum << "\n";

      if (10 / i == 2)  //<--- what is 10 / 5?
      {
         break;
      }

      sum += i;
      std::cout << "\tnew sum: " << sum << "\n";
   }

   std::cout << "\nfinal sum: " << sum << "\n";
}


i: 1, sum: 0
        new sum: 1
i: 3, sum: 1
        new sum: 4
i: 5, sum: 4

final sum: 4
Thank you!

Now whether I did it properly or not remains to be *heard* but as I understand it when compiling is that "i" adds up to become 5, then 10 / 5 == 2,

and then sum += i; will be 2 + 2? I don't grasp yet though, how i may be 2 now if the for loop is not taken into account again.

The more I think about this the more stupid I feel this is on my part, I'll just keep asking rookie questions :P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "stdafx.h"
#include <iostream>

using namespace std;

int main() {
	int sum = 0; // variable declared
	for (int i = 1;i < 10;i += 2) { //init,condition,increment
		cout << "variable i is " << i << "\n";
		if (10 / i == 2) { // always uses braces even when there is 1 statement
			cout << 10 / i << endl;			   // put cout statement here, the value of 10 / i
			break; // exits the for loop
		}
		sum += i; // calculates the sum?
		cout << sum << endl;		  // put cout statement here to print value of sum variable
	}
	cout << sum << endl; // outputs 4
	system("pause"); // "press any key to continue..."
}
Missed your reply at first furryguy, haha thanks! I'll be understanding this now =) I guess never mind my bad code above
Hi,

Put some text in your cout statement, so you know what is what. It is confusing otherwise - because of the if statement.

cout << "variable i is " << i << "\n";
A more sophisticated way is to use an actual debugger. Hopefully there is a GUI version in your IDE. Set up a watch list of variables to keep an eye on, then step through the code 1 line at a time, see how the variables values change and deduce where it all goes wrong (or see what is happening).

This will be very useful once your programs are bigger - it's a pain to put cout statements everywhere, then have to delete them all at the end. Will also save you days of staring at code wondering why it's wrong.
Topic archived. No new replies allowed.