C++ Conditional Structures: Fix the Mistakes
Practice identifying and correcting common C++ beginner mistakes in syntax, variables, operators, and output with simple debugging exercises.
Instructions
For each question:
- Read the problem statement.
- Examine the given code carefully.
- Identify the mistake or mistakes.
- Explain why the code is incorrect.
- Correct the code.
- Compile and run the corrected program.
- Explain how your correction solves the problem.
Important: Try to find the mistake yourself before checking the solution.
Section 1: if Statement
Question 1: Missing Semicolon
Task: Find and fix the mistake in the following program.
Incorrect Code
int x = 10;
if (x > 5);
{
cout << "Greater";
}Question 2:
Task: Find and fix the mistake in the following program.
Incorrect Code
int x = 10;
if x > 5
{
cout << "Greater";
}Question 3:
Task: Find and fix the mistake in the following program.
Incorrect Code
int marks = 60;
if (marks >= 50)
{
cout << "Pass";
}
else (marks < 50)
{
cout << "Fail";
}Question 4:
Task: The program should print Eligible for people aged 18 or older.
Incorrect Code
int age = 16;
if (age >= 18)
{
cout << "Eligible";
}
else
{
cout << "Not Eligible";
}
cout << "Eligible";Question 5:
Task: The program should print Pass when marks are 50 or above. Identify the mistake.
Incorrect Code
int marks = 50;
if (marks > 50)
{
cout << "Pass";
}
else
{
cout << "Fail";
}Question 6:
Task: The program should print the greater number. What is the mistake?
Incorrect Code
int a = 10;
int b = 20;
if (a > b)
{
cout << b;
}
else
{
cout << a;
}Question 7:
Task: The program should print Even for an even number. What is the mistake?
Incorrect Code
int n = 7;
if (n % 2 == 1)
{
cout << "Even";
}
else
{
cout << "Odd";
}