Programs are useless if they execute the exact same instructions every time they run. They need the ability to make decisions based on dynamic variables. The if...else statement is the fundamental building block of logic in C++.
It evaluates a boolean expression (a statement that resolves to true or false). If the condition is true, it executes the block of code inside the curly braces. If false, it completely ignores that block and moves on to the else block. In highly optimized, performance-critical C++ applications (like Unreal Engine), developers must be incredibly careful with complex if statements due to a hardware concept called 'Branch Prediction', where the CPU tries to guess which path the if statement will take to save time.
int score = 85;
// 1. Basic If-Else
if (score >= 90) {
std::cout << "Grade: A\n";
}
// 2. Chained Conditions
else if (score >= 80) {
std::cout << "Grade: B\n";
}
// 3. Fallback
else {
std::cout << "Grade: C or lower\n";
}if (score = 100) is a catastrophic logic error. The single = is an assignment operator. It physically changes the score to 100, which evaluates to true, meaning the 'If' block will always execute! You MUST use the double == for equality checking: if (score == 100).{}. If you write an if statement without curly braces, C++ will only apply the condition to the very first line of code beneath it. The second line will execute unconditionally, leading to incredibly confusing bugs.Explain what 'Branch Prediction' is and why highly unpredictable if statements inside massive loops slow down C++ programs.
Modern CPUs utilize a 'Pipeline' to execute multiple instructions simultaneously. When the CPU hits an if statement, it doesn't know which path to take, so it guesses (Branch Prediction) to keep the pipeline full. If it guesses wrong (a Branch Misprediction), it must flush the entire pipeline and start over, losing 10-20 CPU cycles. Sorting an array before looping through it with an if statement can make the code run 3x faster because it makes the branch highly predictable!
Validating user input boundaries securely to prevent crashes before executing business logic.
#include <iostream>
int main() {
int age;
std::cout << "Enter age: ";
std::cin >> age;
// Guard Clause: Instantly reject invalid data
if (age < 0 || age > 120) {
std::cout << "Error: Invalid age.\n";
return 1; // Terminate early
}
if (age >= 18) {
std::cout << "Access granted to adult restricted area.\n";
} else {
std::cout << "Access denied.\n";
}
return 0;
}Test your understanding of If...Else with these quick questions.
Question 1
What is the result of executing the following C++ code? `bool isAdmin = false; if (isAdmin = true) { cout << "Welcome"; }`
Programs are useless if they execute the exact same instructions every time they run. They need the ability to make decisions based on dynamic variables. The if...else statement is the fundamental building block of logic in C++.
It evaluates a boolean expression (a statement that resolves to true or false). If the condition is true, it executes the block of code inside the curly braces. If false, it completely ignores that block and moves on to the else block. In highly optimized, performance-critical C++ applications (like Unreal Engine), developers must be incredibly careful with complex if statements due to a hardware concept called 'Branch Prediction', where the CPU tries to guess which path the if statement will take to save time.
int score = 85;
// 1. Basic If-Else
if (score >= 90) {
std::cout << "Grade: A\n";
}
// 2. Chained Conditions
else if (score >= 80) {
std::cout << "Grade: B\n";
}
// 3. Fallback
else {
std::cout << "Grade: C or lower\n";
}if (score = 100) is a catastrophic logic error. The single = is an assignment operator. It physically changes the score to 100, which evaluates to true, meaning the 'If' block will always execute! You MUST use the double == for equality checking: if (score == 100).{}. If you write an if statement without curly braces, C++ will only apply the condition to the very first line of code beneath it. The second line will execute unconditionally, leading to incredibly confusing bugs.Explain what 'Branch Prediction' is and why highly unpredictable if statements inside massive loops slow down C++ programs.
Modern CPUs utilize a 'Pipeline' to execute multiple instructions simultaneously. When the CPU hits an if statement, it doesn't know which path to take, so it guesses (Branch Prediction) to keep the pipeline full. If it guesses wrong (a Branch Misprediction), it must flush the entire pipeline and start over, losing 10-20 CPU cycles. Sorting an array before looping through it with an if statement can make the code run 3x faster because it makes the branch highly predictable!
Validating user input boundaries securely to prevent crashes before executing business logic.
#include <iostream>
int main() {
int age;
std::cout << "Enter age: ";
std::cin >> age;
// Guard Clause: Instantly reject invalid data
if (age < 0 || age > 120) {
std::cout << "Error: Invalid age.\n";
return 1; // Terminate early
}
if (age >= 18) {
std::cout << "Access granted to adult restricted area.\n";
} else {
std::cout << "Access denied.\n";
}
return 0;
}Test your understanding of If...Else with these quick questions.
Question 1
What is the result of executing the following C++ code? `bool isAdmin = false; if (isAdmin = true) { cout << "Welcome"; }`