Complete Question:
What will the following segment of code output?
Assume the user enters a grade of 90 from the keyboard.
cout << "Enter a test score: ";
cin >> test_score;
if (test_score < 60);
cout << "You failed the test!" << endl;
if (test_score > 60)
cout << "You passed the test!" << endl;
else
cout << "You need to study for the next test!";
Answer:
You failed the test!
You passed the test!
Explanation:
With the assumption that test_score has been declared as an int variable
The output of: You failed the test! You passed the test! May be unexpected since the test_score is greater than 90 so it looks like the code has a logical error somewhere.
This error is as a result of putting a semi colon at then end of the first if statement if (test_score < 60);
An if statement does not end a thought as such there should'nt be a semi colon ending it. Take the semi-colon out and the program will give the expected output of You passed the test! since test_score of 90 is greater than 60