Simple Statements
In C++, simple statements form the basic building blocks of program execution. These primarily consist of expression statements, null statements, and compound statements.
- Null Statement: Represented by a solitary semicolon
;. It is useful in scenarios where the language syntax requires a statement but no specific action is needed, such as in an empty loop body. - Compound Statement (Block): A sequence of statements and declarations enclosed in curly braces
{}. A block defines a local scope, and notably, it does not end with a semicolon after the closing brace.
Conditional Statements
Conditional statements allow for branching logic based on specific conditions.
- if-else: Executes a block of code if a condition evaluates to true, with an optional else block for false conditions.
- switch: Evaluates an integer expression and transfers control to a matching case label. It is essential to include
breakstatements to prevent fall-through execution.
Iteration Statements
Loops allow for the repeated execution of code blocks.
Standard for Loop
The traditional for loop consists of an initialization, a condition, and an increment expression.
for (int index = 0; index < 5; ++index) {
std::cout << "Current value: " << index << std::endl;
}
Range-based for Loop
Introduced in C++11, this loop iterates over elements in a container, array, or initializer list. It works with any sequence supporting begin() and end() iterators.
std::vector<int> data = {10, 20, 30};
for (const auto& value : data) {
std::cout << value << std::endl;
}
while and do-while
The while loop verifies the condition before execution. The do-while loop guarantees at least one execution by checking the condition after the loop body runs.
Jump Statements
These statements unconditionally alter the flow of control.
- break: Immediately terminates the nearest enclosing loop or switch statement.
- continue: Skips the remainder of the current iteration in a loop and proceeds to the next iteration cycle.
- goto: Transfers control to a labeled statement within the same function. Usage is generally discouraged as it can lead to unreadable code.
// Example of goto usage
if (criticalError) {
goto ErrorHandler;
}
// ... normal logic ...
return;
ErrorHandler:
std::cerr << "Critical failure occurred." << std::endl;
Exception Handling
C++ provides a robust mechanism for handling runtime errors through exception handling.
Throwing Exceptions
The throw keyword is used to signal an error condition. It typically accepts an object derived from std::exception.
throw std::runtime_error("Failed to open configuration file");
Try-Catch Blocks
The try block encloses code that might throw an exception. One or more catch blocks immediately follow to handle specific exception types.
try {
// Code that might throw
processFile();
} catch (const std::ifstream::failure& e) {
std::cerr << "File I/O error: " << e.what() << std::endl;
} catch (const std::exception& e) {
std::cerr << "General error: " << e.what() << std::endl;
}