Welcome to The Coding College! In this guide, we’ll explore statements in C++, the fundamental instructions that direct a program’s flow. By understanding the different types of statements, you’ll be better equipped to write effective and functional programs.
What Are Statements in C++?
In C++, a statement is a single line of code that performs a specific action. Each statement ends with a semicolon (;
). Statements form the core of a program by dictating its operations and flow.
Types of Statements in C++
1. Declaration Statements
Used to declare variables, constants, or other entities.
Example:
int age = 25; // Declares an integer variable named 'age'
float height = 5.9; // Declares a floating-point variable
2. Expression Statements
Evaluates an expression and performs an action, such as assigning a value or performing a calculation.
Example:
int sum = 5 + 10; // Expression evaluates to 15 and assigns it to 'sum'
std::cout << sum << std::endl; // Outputs the value of 'sum'
3. Compound Statements (Blocks)
A group of statements enclosed in curly braces {}
. These statements execute together.
Example:
if (age > 18) {
std::cout << "You are an adult." << std::endl;
std::cout << "Welcome to adulthood!" << std::endl;
}
4. Conditional Statements
Direct program flow based on a condition.
- If Statement: Executes if a condition is
true
.
if (age > 18) {
std::cout << "You can vote!" << std::endl;
}
- If-Else Statement: Provides an alternative path if the condition is
false
.
if (age > 18) {
std::cout << "You are an adult." << std::endl;
} else {
std::cout << "You are a minor." << std::endl;
}
- Switch Statement: Handles multiple conditions.
switch (age) {
case 18:
std::cout << "You just became an adult!" << std::endl;
break;
case 21:
std::cout << "You are officially an adult everywhere." << std::endl;
break;
default:
std::cout << "Enjoy your age!" << std::endl;
}
5. Loop Statements
Repeat a block of code multiple times.
- For Loop:
for (int i = 0; i < 5; i++) {
std::cout << i << " ";
}
// Output: 0 1 2 3 4
- While Loop:
int count = 0;
while (count < 5) {
std::cout << count << " ";
count++;
}
- Do-While Loop:
int count = 0;
do {
std::cout << count << " ";
count++;
} while (count < 5);
6. Jump Statements
Control the flow of execution by transferring control to other parts of the program.
- Break Statement: Exits the loop or switch statement.
for (int i = 0; i < 5; i++) {
if (i == 3) break;
std::cout << i << " ";
}
// Output: 0 1 2
- Continue Statement: Skips the current iteration of a loop.
for (int i = 0; i < 5; i++) {
if (i == 3) continue;
std::cout << i << " ";
}
// Output: 0 1 2 4
- Return Statement: Exits a function and optionally returns a value.
int add(int a, int b) {
return a + b;
}
Writing Good C++ Statements
- Be Clear and Concise: Write straightforward statements that are easy to understand.
- Comment When Needed: Add comments to explain complex logic.
- Use Proper Indentation: Enhance readability and maintainability.
Learn More with The Coding College
At The Coding College, we break down coding concepts into simple, actionable lessons. Explore our detailed C++ tutorials and learn how to write efficient, error-free statements.
Next Steps
- Practice writing different types of statements in small programs.
- Learn about advanced concepts like functions, pointers, and classes.