C++ Conditional Statements Interview Questions

Conditional Statements: if, if-else, else-if ladder, nested if

Basic Level (15 Questions)

What is the basic syntax of an if statement in C++?
The basic syntax of an if statement is:
if (condition) {
  // Code to execute if condition is true
}
What is the syntax and purpose of if-else statement?
The if-else statement provides two alternative execution paths:
if (condition) {
  // Code to execute if condition is true
} else {
  // Code to execute if condition is false
}
What is an else-if ladder and when is it used?
An else-if ladder handles multiple conditions sequentially:
if (condition1) {
  // Code for condition1 true
} else if (condition2) {
  // Code for condition2 true
} else if (condition3) {
  // Code for condition3 true
} else {
  // Code if all conditions are false
}
What are nested if statements?
Nested if statements are if statements inside other if statements:
if (outerCondition) {
  if (innerCondition) {
    // Both conditions true
  }
  // Outer condition true, inner may be false
}
What is the difference between = and == in conditional statements?
Common mistake: Using assignment (=) instead of equality (==):
// WRONG - This always assigns 5 to x and condition is always true
if (x = 5) { // Assignment, not comparison
  cout << "x is 5";
}

// CORRECT - This compares x with 5
if (x == 5) { // Equality comparison
  cout << "x is 5";
}
Can we omit braces {} in if statements?
Yes, but only for single statements:
// Valid - single statement
if (x > 0)
  cout << "Positive";

// Invalid - multiple statements without braces
if (x > 0)
  cout << "Positive";
  cout << "Number"; // This always executes!

// Best practice: Always use braces
if (x > 0) {
  cout << "Positive";
}
What is dangling else problem and how does C++ handle it?
Dangling else: When it's unclear which if an else belongs to.
if (condition1)
  if (condition2)
    statement1;
else
  statement2; // Which if does this else belong to?
How to check multiple conditions using logical operators?
Use logical operators (&&, ||, !):
// AND operator (both must be true)
if (age >= 18 && age <= 65) {
  cout << "Working age";
}

// OR operator (at least one true)
if (grade == 'A' || grade == 'B') {
  cout << "Good grade";
}

// NOT operator (reverse condition)
if (!isRaining) {
  cout << "Go outside";
}
What is short-circuit evaluation in conditional statements?
Short-circuit evaluation stops evaluating as soon as the result is known:
// If ptr is nullptr, ptr->value is NOT evaluated (safe)
if (ptr != nullptr && ptr->value > 0) {
  // Safe from null pointer dereference
}

// If found is true, searchMore() is NOT called
if (found || searchMore()) {
  // searchMore() only called if found is false
}
What is the ternary conditional operator and how is it different from if-else?
Ternary operator (?:) is a shorthand for simple if-else:
// Ternary operator
int max = (a > b) ? a : b;

// Equivalent if-else
int max;
if (a > b) {
  max = a;
} else {
  max = b;
}
How to implement grade classification using else-if ladder?
Practical example of else-if ladder:
char grade;
if (score >= 90) {
  grade = 'A';
} else if (score >= 80) {
  grade = 'B';
} else if (score >= 70) {
  grade = 'C';
} else if (score >= 60) {
  grade = 'D';
} else {
  grade = 'F';
}
cout << "Your grade is: " << grade;
What is a nested if example for login validation?
Example of nested if for multi-level validation:
string username, password;
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;

if (username == "admin") {
  if (password == "secret123") {
    cout << "Login successful!";
  } else {
    cout << "Incorrect password";
  }
} else {
  cout << "User not found";
}
What are common mistakes in conditional statements?
Common mistakes to avoid:
// 1. Assignment instead of comparison
if (x = 5) {} // Wrong, should be x == 5

// 2. Missing braces for multiple statements
if (condition)
  statement1; // Only this is conditional
  statement2; // This always executes!

// 3. Semicolon after if condition
if (x > 0); { // Semicolon ends the if statement
  cout << "Positive"; // This always executes
}

// 4. Floating point equality comparison
if (x == 0.3) {} // Bad for floating point
if (fabs(x - 0.3) < 0.00001) {} // Better
How to check if a number is positive, negative or zero?
Example using if-else-if:
int number;
cout << "Enter a number: ";
cin >> number;

if (number > 0) {
  cout << "Positive number";
} else if (number < 0) {
  cout << "Negative number";
} else {
  cout << "Zero";
}
What is the difference between if-else and switch statement?
Comparison:
// if-else (can use any condition)
if (grade == 'A') {
  cout << "Excellent";
} else if (grade == 'B') {
  cout << "Good";
} else if (grade == 'C') {
  cout << "Average";
} else {
  cout << "Below Average";
}

// switch (only for equality with constants)
switch (grade) {
  case 'A': cout << "Excellent"; break;
  case 'B': cout << "Good"; break;
  case 'C': cout << "Average"; break;
  default: cout << "Below Average";
}

Tricky Level (10 Questions)

How to implement leap year check using nested if?
Leap year logic using nested conditions:
int year;
bool isLeapYear = false;

if (year % 4 == 0) {
  if (year % 100 == 0) {
    if (year % 400 == 0) {
      isLeapYear = true;
    }
  } else {
    isLeapYear = true;
  }
}

if (isLeapYear) {
  cout << year << " is a leap year";
} else {
  cout << year << " is not a leap year";
}
What is the conditional (ternary) operator syntax for nested conditions?
Nested ternary operator example:
// Find largest of three numbers
int a = 10, b = 20, c = 15;
int largest = (a > b) ? // Outer condition
              (a > c ? a : c) : // True branch
              (b > c ? b : c); // False branch
cout << "Largest: " << largest;
How to optimize multiple if statements?
Optimization techniques:
// Instead of multiple separate ifs
if (x == 1) func1();
if (x == 2) func2();
if (x == 3) func3();

// Use else-if ladder (more efficient)
if (x == 1) {
  func1();
} else if (x == 2) {
  func2();
} else if (x == 3) {
  func3();
}

// Or switch for exact matches
switch (x) {
  case 1: func1(); break;
  case 2: func2(); break;
  case 3: func3(); break;
}
What is the role of braces in nested if statements?
Braces define scope and prevent dangling else issues:
// Without braces - ambiguous
if (condition1)
  if (condition2)
    statement1;
else
  statement2; // Belongs to inner if

// With braces - clear intent
if (condition1) {
  if (condition2) {
    statement1;
  }
} else {
  statement2; // Clearly belongs to outer if
}
How to validate user input using conditional statements?
Input validation example:
int age;
cout << "Enter age (0-120): ";
cin >> age;

if (cin.fail()) {
  cout << "Invalid input! Not a number.";
  cin.clear();
  cin.ignore(1000, ' ');
} else if (age < 0) {
  cout << "Age cannot be negative!";
} else if (age > 120) {
  cout << "Age cannot be more than 120!";
} else {
  cout << "Valid age: " << age;
}
What is the ternary operator syntax in C++?
condition ? expr1 : expr2 returns expr1 if true, else expr2. Both branches must be compatible types.
When should you prefer `switch` over `if-else` chains?
When comparing one integral or enum expression against many constants—compilers may emit jump tables for dense cases.
What is `[[fallthrough]]` attribute?
C++17 attribute documenting intentional fall-through between switch cases, suppressing compiler warnings.
Can `if constexpr` replace all runtime `if`?
if constexpr chooses branches at compile time in templates; discarded branches must be valid for SFINAE but need not be linkable.
What happens if no `default` in `switch` and no case matches?
Control falls through the entire switch with no action—unlike some languages, no error is raised.
Note: These questions cover core interview topics. Pair with the tutorial and MCQ quiz for this section. This page lists 15 basic and 10 tricky questions—use the tutorial and MCQ links above and below.