C++ CH 3
C++ CH 3
CONTROL STRUCTURE
Sure! Here are the definitions and examples for all the main control structures in
C++:
1. Sequential Control
In sequential control, statements are executed one after another in the order in which
they appear in the program. This is the default behavior where the program runs from
top to bottom, without any branching or looping.
Definition:
This is the simplest form of control flow, where the code executes line by line.
Example:
cpp
Copy
#include <iostream>using namespace std;
int main() {
cout << "This is the first statement." << endl;
cout << "This is the second statement." << endl;
return 0;
}
In this example, the program executes the first statement, then the second statement,
one after the other.
Selection control structures allow the program to make decisions based on certain
conditions. They execute different blocks of code depending on whether a condition is
true or false.
2.1 if statement
Definition:
The if statement checks if a condition is true and, if so, executes the associated block of
code.
Example:
cpp
Copy
#include <iostream>using namespace std;
int main() {
int age = 18;
In this example, the program checks if age is 18 or greater. If true, it prints "You are
an adult."
Definition:
The if-else statement executes one block of code if the condition is true, and another
block if the condition is false.
Example:
cpp
Copy
#include <iostream>using namespace std;
int main() {
int age = 16;
Here, if age is less than 18, the program prints "You are not an adult."
Definition:
The if-else if-else chain allows multiple conditions to be checked sequentially. It
runs the first true condition, or the last block if none are true.
Example:
cpp
Copy
#include <iostream>using namespace std;
int main() {
int age = 25;
This checks multiple ranges of age and prints the corresponding message.
Iteration control structures are used to repeat a block of code multiple times as long
as a condition is true.
Definition:
The for loop repeats a block of code a specific number of times, based on the provided
initialization, condition, and increment/decrement.
Example:
cpp
Copy
#include <iostream>using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Iteration " << i << endl;
}
return 0;
}
This loop prints "Iteration 1" through "Iteration 5" by incrementing i from 1 to 5.
Definition:
The while loop repeats a block of code as long as the condition is true.
Example:
cpp
Copy
#include <iostream>using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << "Iteration " << i << endl;
i++;
}
return 0;
}
This loop prints "Iteration 1" through "Iteration 5" using a condition to continue
looping.
Definition:
The do-while loop guarantees the code block is executed at least once, and then repeats
based on the condition.
Example:
cpp
Copy
#include <iostream>using namespace std;
int main() {
int i = 1;
do {
cout << "Iteration " << i << endl;
i++;
} while (i <= 5);
return 0;
}
This also prints "Iteration 1" through "Iteration 5", but the loop runs at least once even
if the condition were false initially.
Jump control structures allow you to break out of loops or skip iterations.
4.1 break
Definition:
The break statement immediately exits the current loop or switch case.
Example:
cpp
Copy
#include <iostream>using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i equals 3
}
cout << "Iteration " << i << endl;
}
return 0;
}
This loop prints "Iteration 1" and "Iteration 2" and then exits the loop when i equals 3.
4.2 continue
Definition:
The continue statement skips the current iteration of the loop and proceeds with the next
iteration.
Example:
cpp
Copy
#include <iostream>using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the iteration when i equals 3
}
cout << "Iteration " << i << endl;
}
return 0;
}
This loop prints "Iteration 1", "Iteration 2", "Iteration 4", and "Iteration 5" while
skipping iteration 3.
4.3 return
Definition:
The return statement exits the current function, returning control to the calling function.
Example:
cpp
Copy
#include <iostream>using namespace std;
void printMessage() {
cout << "Before return." << endl;
return; // Exit the function
cout << "This will not be printed." << endl; // Code after
return is not executed
}
int main() {
printMessage();
return 0;
}
In this example, the message after the return statement is never printed because the
function exits immediately.
Each control structure serves a specific purpose depending on how you need your
program to flow. Would you like any specific examples or explanations for any of
these structures?