C++ Conditional Statements Interview Questions
Conditional Statements: if, if-else, else-if ladder, nested if
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
}
The condition can be any expression that evaluates to true (non-zero) or false (zero).
// 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
}
Flow:
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Start → Condition → True → if-block → End
↓ False
else-block → End
↓ False
else-block → End
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
}
Used when there are multiple exclusive conditions to check.
// 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
}
Used for checking multiple dependent conditions.
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";
}
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";
}
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.
Solution: Use braces to clarify intent.
if (condition1)
if (condition2)
statement1;
else
statement2; // Which if does this else belong to?
C++ rule: else is associated with the nearest preceding unmatched if.if (condition2)
statement1;
else
statement2; // Which if does this else belong to?
Solution: Use braces to clarify intent.
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";
}
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
}
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;
}
Differences: Ternary returns a value, can be used in expressions.
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;
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";
}
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
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";
}
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";
}
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";
}
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";
}
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;
Note: Nested ternary can be hard to read - use judiciously.
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;
}
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
}
Always use braces for clarity and to avoid bugs.
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, '\n');
} 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;
}
cout << "Enter age (0-120): ";
cin >> age;
if (cin.fail()) {
cout << "Invalid input! Not a number.";
cin.clear();
cin.ignore(1000, '\n');
} 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;
}
Best Practices Summary:
- Always use braces {} for if statements, even for single statements
- Use meaningful conditions and variable names
- Avoid deep nesting (max 2-3 levels)
- Use else-if ladder for mutually exclusive conditions
- Be careful with assignment (=) vs equality (==)
- Consider using switch for multiple exact value comparisons
- Test edge cases and boundary conditions