CONTROL FLOW STRUCTURES
order in which instructions are executed.
Categorized into three main types:
1. 1. Sequential
2. 2. Conditional
à a. Decision
making(selection) b.
Looping (iteration)
3. 3. Un-conditional (Jump) )Ã a. Goto b. Break c.
Continue
1. Sequential
Control Flow
- default
flowà : statements execution flowà one after the other.
Example:-
#include <iostream>
using namespace std;
int main() {
cout << "This is sequential execution." << endl;
cout << "Next statement executes." << endl;
return 0;
}
2. Selection
Control Flow
- Used
for decision-making (branching).
Types :-
a) simple if
Statement
·
Executes
a block of code if a condition is true.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
#include <iostream>
using namespace std;
int main() {
int x = 10;
if (x > 5) {
cout << "x is greater than
5" << endl;
}
return 0;
}
b) if-else
Statement
·
Provides
an alternative block if the condition is false.
Syntax:
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
Example:
#include <iostream>
using namespace std;
int main() {
int x = 3;
if (x > 5) {
cout << "x is greater than
5" << endl;
} else {
cout << "x is not greater
than 5" << endl;
}
return 0;
}
c) if-else if-else
Statement
·
Used
for multiple conditions.
Syntax:
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else {
// Code if none of the conditions are true
}
Example:
#include <iostream>
using namespace std;
int main() {
int x = 8;
if (x > 10) {
cout << "x is greater than
10" << endl;
} else if (x > 5) {
cout << "x is greater than 5
but less than or equal to 10" << endl;
} else {
cout << "x is 5 or
less" << endl;
}
return 0;
}
d) switch
Statement
Used for selecting among multiple
options.
Syntax:
switch (variable) {
case value1:
// Code for case value1
break;
case value2:
// Code for case value2
break;
default:
// Code if no case matches
}
Example:
#include <iostream>
using namespace std;
int main() {
int day = 3;
switch (day) {
case 1:
cout << "Monday"
<< endl;
break;
case 2:
cout << "Tuesday"
<< endl;
break;
case 3:
cout << "Wednesday"
<< endl;
break;
default:
cout << "Invalid
day" << endl;
}
return 0;
}
e. Nested-if
·
if
statement inside another if statement.
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
Flowchart
Example:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
if (i ==
10) {
//
First if statement
if
(i < 15)
cout
<< "i is smaller than 15\n";
//
Nested - if statement
//
Will only be executed if
//
statement above is true
if
(i < 12)
cout
<< "i is smaller than 12 too\n";
else
cout
<< "i is greater than 15";
}
return 0;
}
Output:
i is smaller than 15
i is smaller than 12 too
3. Iteration
(Loops)
·
Used
to repeat a block of code.
Two Types :-
A.
entry
controlled. Ã a. while loop. b. for loop
B.
Exit
controlled à do…while loop
a) for Loop
·
Executes
a block of code a specific number of times.
Syntax:
for (initialization; condition;
increment/decrement) {
// Code to execute
}
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
cout << "i: " <<
i << endl;
}
return 0;
}
b) while Loop
·
Executes
a block of code as long as the condition is true.
Syntax:
while (condition) {
// Code to execute
}
Example:
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 5) {
cout << "i: " <<
i << endl;
i++;
}
return 0;
}
c) do-while Loop
Executes the block of code at least
once and then repeats while the condition is true.
Syntax:
do {
// Code to execute
} while (condition);
Example:
#include <iostream>
using namespace std;
int main() {
int i = 0;
do {
cout << "i: " <<
i << endl;
i++;
} while (i < 5);
return 0;
}
4. Jump Statements
·
Control
the flow by jumping to another part of the code.
a) break
·
Exits
a loop or switch statement.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
if (i == 3) break;
cout << "i: " <<
i << endl;
}
return 0;
}
b) continue
·
Skips
the current iteration of the loop.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
cout << "i: " <<
i << endl;
}
return 0;
}
c) goto
·
Transfers
control to a labeled statement.
Example:
#include <iostream>
using namespace std;
int main() {
int x = 1;
if (x == 1) goto label;
cout << "This won't execute" << endl;
label:
cout << "Jumped to label" << endl;
return 0;
}
0 Comments