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?
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;
}
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