Code Indentation
Code indentation is an important practice that makes code more readable and clear. While C++ doesn't require indentation for the program to work correctly, it's a standard convention used by all programmers.
Poorly indented code:
if (number > 0) {
std::cout << "Positive" << std::endl;
} else {
std::cout << "Negative" << std::endl;
}
Properly indented code:
if (number > 0) {
std::cout << "Positive" << std::endl;
} else {
std::cout << "Negative" << std::endl;
}
Basic indentation rules:
- Code inside curly braces
{}
is indented one level to the right - Use either 4 spaces or a tab for indentation
- Use indentation consistently throughout the entire program
- Write curly braces either on the same line or below each other
Indentation is especially important with nested code blocks:
if (number > 0) {
if (number > 100) {
std::cout << "Large positive number" << std::endl;
} else {
std::cout << "Small positive number" << std::endl;
}
} else {
std::cout << "Negative number" << std::endl;
}
Instructions
Fix the code indentation to make it more readable. Use 4 spaces for each indentation level.
Start programming for free
4/10