Control Flow
Control flow in C++ is managed through a variety of control structures, such as conditional statements (if
, else
, switch
) and loops (for
, while
, do-while
).
If Else
int x = 10;
if (x > 0) {
std::cout << "x is positive." << std::endl;
} else if (x < 0) {
std::cout << "x is negative." << std::endl;
} else {
std::cout << "x is zero." << std::endl;
}
Switch
swtich
jumps to a matching value.
int day = 3;
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
// ... other cases
default:
std::cout << "Invalid day" << std::endl;
}
For Loops
In a for
loop, the initialization, condition, and update expressions are specified within the parentheses. The loop body is executed repeatedly as long as the condition is true.
for (int i = 0; i < 5; ++i) {
std::cout << "Iteration " << i << std::endl;
}
break and continue Statements
for (int i = 0; i < 10; ++i) {
if (i == 5) {
break; // exit the loop
}
if (i % 2 == 0) {
continue; // skip even iterations
}
std::cout << i << std::endl;
}
Range-Based For Loops
C++ introduced the range-based for loop in C++11, providing a concise and convenient way to iterate over elements in a container, such as arrays, vectors, or other iterable structures. The syntax is straightforward and makes the code more readable.
#include <iostream>
#include <vector>
int main() {
// Example with an array
int numbers[] = {1, 2, 3, 4, 5};
// Iterate over each element in the array
for (int number : numbers) {
std::cout << number << " ";
}
std::cout << std::endl;
// Example with a vector
std::vector<std::string> words = {"apple", "banana", "orange"};
// Iterate over each element in the vector
for (const std::string& word : words) {
std::cout << word << " ";
}
std::cout << std::endl;
return 0;
}
While Loops
In a while
loop, the condition is evaluated before entering the loop, and the loop continues executing as long as the condition is true.
int count = 0;
while (count < 3) {
std::cout << "Count: " << count << std::endl;
++count;
}
Do While Loops
In a do-while
loop, the loop body is executed at least once, and the condition is checked after each iteration. If the condition is true, the loop continues; otherwise, it terminates.
int num = 5;
do {
std::cout << "Number: " << num << std::endl;
--num;
} while (num > 0);
Goto
The goto
statement allows you to transfer control to another labeled part of the program. However, the use of goto is generally discouraged because it can lead to code that is harder to read and maintain.
int value = 0;
if (value < 0) {
goto negative;
}
std::cout << "Value is non-negative." << std::endl;
negative: // label 'negative'
std::cout << "Value is negative." << std::endl;
Tenary Operator
The ternary operator (? :
) in C++ is a concise way to write a simple conditional expression. It takes three operands: a condition, a value to be returned if the condition is true
, and a value to be returned if the condition is false
.
The syntax is as follows:
condition ? expression_if_true : expression_if_false;
Here's an example:
#include <iostream>
int main() {
int x = 10;
int y = 20;
// Using the ternary operator to assign the smaller of x and y to result
int result = (x < y) ? x : y;
std::cout << "The smaller of " << x << " and " << y << " is: " << result << std::endl;
return 0;
}
Code Challenge
Write a C++ program that uses the ternary operator to determine and print the corresponding letter grade.
Grading scale:
90-100: A 80-89: B 70-79: C 60-69: D 0-59: F