Live version: http://cpp.sh/953y6
The code:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
// Complete the code.
int num1 = 8, num2 = 11;
for(int n = num1; n <= num2; n++){
if(n <= 9){
switch(n){
case 1: cout << "one\n";
case 2: cout << "two\n";
case 3: cout << "three\n";
case 4: cout << "four\n";
case 5: cout << "five\n";
case 6: cout << "six\n";
case 7: cout << "seven\n";
case 8: cout << "eight\n";
case 9: cout << "nine\n";
}
}
else if(n % 2 == 0){ //even
cout << "even\n";
}
else if(n > 9 && n %2 == 1){ //odd
cout << "odd\n";
}
}
return 0;
}
The numbers 8 through 11 are looped through on the for-loop. if(n <= 9) should only be triggered twice, when n is 8 and when n is 9. Instead, the output is:
eight
nine
nine
even
odd
Why?