[go: up one dir, main page]

0% found this document useful (0 votes)
36 views7 pages

C++ CH 3

The document provides an overview of control structures in C++, including sequential control, selection control (if, if-else, if-else if-else), iteration control (for, while, do-while), and jump control (break, continue, return). Each control structure is defined and illustrated with examples to demonstrate its functionality. The summary emphasizes the purpose of each structure in managing program flow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views7 pages

C++ CH 3

The document provides an overview of control structures in C++, including sequential control, selection control (if, if-else, if-else if-else), iteration control (for, while, do-while), and jump control (break, continue, return). Each control structure is defined and illustrated with examples to demonstrate its functionality. The summary emphasizes the purpose of each structure in managing program flow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

2. Selection Control (Conditional Statements)

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;

if (age >= 18) {


cout << "You are an adult." << endl;
}
return 0;
}

In this example, the program checks if age is 18 or greater. If true, it prints "You are
an adult."

2.2 if-else statement

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;

if (age >= 18) {


cout << "You are an adult." << endl;
} else {
cout << "You are not an adult." << endl;
}
return 0;
}

Here, if age is less than 18, the program prints "You are not an adult."

2.3 if-else if-else statement

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;

if (age < 13) {


cout << "You are a child." << endl;
} else if (age < 18) {
cout << "You are a teenager." << endl;
} else {
cout << "You are an adult." << endl;
}
return 0;
}

This checks multiple ranges of age and prints the corresponding message.

3. Iteration Control (Loops)

Iteration control structures are used to repeat a block of code multiple times as long
as a condition is true.

3.1 for loop

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.

3.2 while loop

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.

3.3 do-while loop

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.

4. Jump Control (Break, Continue, and Return)

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.

Summary of Control Structures:

 Sequential Control: Executes statements one by one.


 Selection Control: Makes decisions using if, else, and switch.
 Iteration Control: Repeats code using loops: for, while, and do-while.
 Jump Control: Allows you to exit loops or skip iterations using break, continue, and
return.

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?

You might also like