Void function problem

I was told by my professor that the output would be "The value of total when i == 5 is -4
The final value of total is: 0
but I don't exactly understand why, can anyone help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 void func4() {
	cout << "func4(): " << endl;

	int total = 5;

	for ( int i = 1; i <= 10; i++ ) {
		switch( i ) {
			case 1:
				total += i;
				break;

			case 4:
				total -= i;
				break;
			
			case 5:
				cout << "The value of total when i == " << i << " is " << total << endl;
			case 6:
				total *= total;
				break;

			default:
				total = 0;
		}
	}

	cout << "The final value of total is: " << total << endl;
}

closed account (48T7M4Gy)
i = 1, case 1, total = 6
i = 2, case 2 default, total = 0
i = 3, case 3 default, total = 0
i = 4, case 4, total = 0-4 = -4
i = 5, case 5, total unchanged = -4
i = 6, case 6, etc etc
Topic archived. No new replies allowed.