Flow of Control | What are Control Structures and How build them?
Control Structures:- In computer science, the flow of control (or control flow) is the order in which individual statements, function calls, or instructions of an imperative program are evaluated or executed. therefore, the emphasis on explicit control flow distinguishes an imperative programming language from a declarative programming language.
In a programming language, a control flow statement is a statement that results in a choice being made as to which of two or more paths to follow. For non-strict useful functional languages, and language constructs exist to achieve or realize a similar result, but they are typically not termed control flow statements.
Flow Control Overview:
A control structure is sort of a block of programming that analyses variables and chooses a direction within which to travel supported given parameters. The term flows control details the direction the program takes (which method program control “flows”). thus it’s the essential decision-making process in computing.
In control structures, therefore a set of statements is in turn generally structured as a block, which in addition to grouping, also defines a lexical scope. signals and interrupts are low-level mechanisms that can alter the flow of control in a way similar to a subroutine but usually occur as a response to some external stimulus or event, rather than the execution of an in-line control structures or control flow statement.

The Three Fundamental Control structures
there are three types of following fundamental control structures in structured programming. let us understand the control structures with the diagram.
Decision Control Structures:- It’s depending on whether a condition is true or false, therefore, the decision control structure may skip the execution of an entire block of a statement or even execute one block of statements instead of another.

Sequence Control Structures:- this sequence-structure refers to the line-by-line execution, as you can understand by the name, therefore, in which statements are executed sequentially, in the same order in which they appear in the script. they might, for example, carry out a series of reading or write operations, arithmetic operations, or assignments to variables.

Loop Control Structures:- The Loop control structures are those structure which allows the execution of a block of statements multiple times until when the specified condition is met.

How Control Structures Programs Work, and how can you build them?
therefore, if you want to understand the computer program, then we will need to understand their building blocks, you see, when a program runs, then code is read by the computer line by line from the left to right and top to bottom as like just you would read a book. furthermore, at some point, the program may reach a situation, where it needs to make a decision such as a jump to a different part of the program or re-run a certain piece again. These decisions that affect the flow of the programs are known as control structures.
therefore, we can say that Control structures can be considered as the building blocks of computer programs. thus, a program is typically or usually not limited to a linear sequence of instruction sine its process may bifurcate, bypass sections or repeat code. They are commands that enable a program to “take decisions” following one path or another. The control structures are the blocks that analyze variable variables and choose directions in which to go based on given parameters.
Basic Control Structures
there are the basic control structures in a programming language:
A)- Selection (or Conditionals): these are used to execute one or more statements if a condition is met.
B)- Iteration (or loops): Which purpose is to repeat a statement a certain number of times or while a condition is fulfilled.
therefore, let’s see each of these concepts, which are mostly used languages for data science, and the same ideas and explanations are applied to any other programming language.
A) Selection (or Conditionals)
“Conditionals” or Selection are used in the core programming, the idea behind them is that they are allowed to control the flow of the program code that is executed based on different conditions in the program such as the input taken from the users or the internal state of the machine, the program is running on, therefore, we will explore the Conditionals Control Structures of “If statements” and “If-Else statements”.
1) If Statements:
the "If statements" execute one or more statements when a condition is met. if the testing of that condition is TRUE, the statement gets executed. But if it’s FALSE (this condition occurs when the condition is not met), then nothing happens. let’s understand through the flow chart.

The General Syntax of “If statements” is:
The code to be executed if the condition is true;
}
Program Example:-
|
1 2 3 4 5 6 |
<?php $t = date("H"); if ($t < "20") { echo "Have a good day!"; } ?> |
Output: “Have a good day!” if the current time (HOUR) is less than 20:
furthermore, since the “if statement” only executes a statement if the tested condition is TRUE, otherwise what would happen if the current time (HOUR)is not less than 20, then to execute a statement condition with a FALSE, we need to use “If-Else statement”.
2) If-Else Statements
If-Else Statements control structures allow a program to follow another alternative path of execution, whether a condition matched or not.

The General Syntax of “If-Else statements” is:
code to be executed if the condition is true;
} else {
The code to be executed if the condition is false;
}
Therefore, the “else part” of the instruction is optional and only executed if the condition tests FALSE.
Program Example:-
|
1 2 3 4 5 6 7 8 |
<?php $t = date("H"); if ($t < "20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?> |
Output: Output “Have a good day!” if the current time is less than 20, and “Have a good night!” otherwise.
B) Loops (or Iteration)
The “Loop statements” are those control structures that are the automation of multi-step processes by organizing sequences of actions, and grouping the parts which are required to be repeated. also a central part of programming, Looping provides or gives computers much of their power. they’ll repeat a sequence of steps as often as necessary, and appropriate repetitions of easy steps will solve complex issues.
Therefore, in general terms, there are two types of "Looping methods":
1. For Loops: these are the ones that execute for a prescribed number of times, as controlled by an index or a counter.
2. While Loops and Repeat Loops: these are based on the onset and verification of a logical condition. therefore, the condition is tested at the start or at the end of the loop construct.
Furthermore, take a look at them:
1) For Loops:
The statements are executed one after another in consecutive order over a sequence of values that gets evaluated only when the “For Loop” is initiated (i.e. never re-evaluated). therefore, In this case, the number of iterations is fixed and known in advance.

If the evaluation of the condition on a variable (which can assume values within a specified sequence) results in TRUE, one or more statements will be executed sequentially over that string of values.
Once the first condition test is done (and results TRUE), the statement is executed and the condition is evaluated again, going through an iterative process. furthermore, the “variable in sequence” section that performs this test on each value of the sequence until when it covers the last element.
therefore, If the condition is not met and the resulting outcome is FALSE (e.g. the “variable in sequence” part has finished going through all the elements of the sequence), the loop ends.
furthermore, If the condition test results come FALSE in the first iteration, then the “For Loop” will never be executed.
The General Syntax of “For Loop statements” is:
The code to be executed for each iteration;
}
init counter: Initialize the loop counter value.
test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value.
Program Example 1:-
The given following example below displays the numbers from 0 to 5:
|
1 2 3 4 5 |
<?php for ($x = 0; $x <= 5; $x++) { echo "The number is: $x <br>"; } ?> |
Output:
|
1 2 3 4 5 6 |
The number is: 0 The number is: 1 The number is: 2 The number is: 3 The number is: 4 The number is: 5 |
Example Explained
$x = 0; :- therefore, the initialize the loop counter ($x) and set the start value to 0
$x <= 5; :- mean that, Continue the loop as long as $x is less than or equal to 5
$x++ :- Increase the loop counter value by 1 for each iteration
Program Example 2:-
|
1 2 3 4 5 |
<?php for ($x = 0; $x <= 100; $x+=10) { echo "The number is: $x <br>"; } ?> |
Output:
|
1 2 3 4 5 6 7 8 9 10 11 |
The number is: 0 The number is: 10 The number is: 20 The number is: 30 The number is: 40 The number is: 50 The number is: 60 The number is: 70 The number is: 80 The number is: 90 The number is: 100 |
Example Explained
$x = 0; :- therefore, the initialize the loop counter ($x), and set the start value to 0
$x lesssign= 100; :- means that the Continue the loop as long as $x is less than or equal to 100.
$x+=10 :- Increase the loop counter value by 10 for each iteration.
2) While Loops
In “While Loops” a condition is first evaluated, and if the result of testing that condition is TRUE, One or more statements are repeatedly executed until that condition becomes FALSE.

Unlike “If statements”, in which a condition tested as TRUE executes an expression only once and ends, “While Loops” are iterative statements that execute some expression over and over again until the condition becomes FALSE. If the condition never turns out to be FALSE, the “While Loop” will go on forever and the program will crash. The other way around, if the condition test results FALSE at the beginning of the loop, the expression will never get executed.
The General Syntax of “While Loop” is:
code to be executed;
}
Program Example:-
The given following example below displays the numbers from 1 to 5:
|
1 2 3 4 5 6 7 |
<?php $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?> |
Output:
|
1 2 3 4 5 |
The number is: 1 The number is: 2 The number is: 3 The number is: 4 The number is: 5 |
3) Repeat Loops
There are closely linked to “While Loops”, “Repeat Loops” execute statements iteratively, but until a stop condition is met. This way, statements are executed at least once, no matter what the result of the condition is, and the loop is exited only when the certain condition becomes TRUE:

The General Syntax “Repeat Loop” is:
In control structures, the “Repeat Loops” used for the “Break statements” as a stop condition.
therefore, the “Break statements” are combined with the take a glance at a condition to interrupt cycles within or among loops, since once the program or when the program hits a break.
furthermore, it will pass control to the instruction instantly once the end of the loop (if any).
the “Repeat Loops” will run forever when if the break condition is not met.
The switch statement
A switch statement is commonly convenient for occasions during which there are multiple cases to choose from.
The General Syntax is:
{
case constant:
statements
case constant:
statements… (as many case labels as needed)default: // optional label
statements
}
The switch statement evaluates the expression and after that, they compare it to the values in the case labels. furthermore, If it finds a match, execution of code jumps to that case label.
In the case of labels, the value must be constants, and may only be an integer type, which means that you, therefore, means that only integer types, type char, or enumerations, it also means the case label which must be a literal or a variable declared to be constant.
You may not have case labels with strings, regular variables, operations, floating-point literals, or function calls.
furthermore, If you want to execute code only then in that case you jump to, end the case with a break statement, otherwise, execution of code will “fall through” to the next case.
Conclusion
We’ve seen and explained concepts but “Control Structures” can be combined any way you want:
Loops may contain several internal Loops; Conditionals may contain Loops and Conditionals, the options are endless.
You can develop advanced solutions just by combining the “Control Structures”.Control Structures
constitute the basic blocks for decision-making processes in computing. therefore, They change the
flow of programs (flow-control) and enable us to construct complex or difficult sets of instructions
out of simpler building blocks.