Learn with Yasir

Share Your Feedback

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:

  1. Read the problem statement.
  2. Examine the given code carefully.
  3. Identify the mistake or mistakes.
  4. Explain why the code is incorrect.
  5. Correct the code.
  6. Compile and run the corrected program.
  7. 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";
}

Question 8: Incorrect Logical Operator

Task: A student passes only if marks are at least 50 and attendance is at least 75.

Incorrect Code

#include <iostream>
using namespace std;

int main()
{
    int marks = 60;
    int attendance = 80;

    if (marks >= 50 || attendance >= 75)
    {
        cout << "Pass";
    }
    else
    {
        cout << "Fail";
    }

    return 0;
}

Question 20: Incorrect Comparison Operator

Task: The program should check whether two numbers are equal.

Incorrect Code

#include <iostream>
using namespace std;

int main()
{
    int a = 20;
    int b = 20;

    if (a = b)
    {
        cout << "Numbers are not equal";
    }
    else
    {
        cout << "Numbers are equal";
    }

    return 0;
}