Program Control Strcutures
Program Control Strcutures
Loriano Storchi
http:://www.storchi.org/
FLOWCHART AND
PSEUDOCODE
FLOWCHART AND
PSEUDOCODE
●
We will only go through some basic example: Calculate
the Interest of a Bank Deposit
There are three fundamental structures that are used for the algorithmic resolution
of problems: selection, iterations and sequence (sequence of instructions) (the
GOTO present in machine languages since the 70s has been progressively
discouraged / eliminated)
Examples: https://bitbucket.org/lstorchi/teaching
https://github.com/lstorchi/teaching
SEQUENCES
Sequences
●
Sequence control structure refers to the line-by-line
execution by which statements are executed sequentially,
in the same order in which they appear in the program.
They might, for example, carry out a series of read or write
operations, arithmetic operations, or assignments to
variables.
●
In all programming languages, I can use relationship
operators to compare numbers and variables, for example:
Int a;
a = 4 // operatori di assegnazione
If (a == 5)
{
cout << “Hello” << std::endl;
}
else if (a > 5)
{
cout << a << “ > 5 “ << std::endl;
}
Logical Operators
●
I take for granted the logical operators AND, OR and NOT
5<a<7
In programming languages it is
broken into two elementary
expressions linked by the operator
AND:
●
A simple test just to clarify
Case structure
●
Basically a series of if-then-else with some constraint. In practice, the choice
between the blocks of instruction is guided by the value of a certain variable:
●
There are three different types: while..do , do..while e for
●
Not all languages necessarily have all three of these
structures
●
These structures allow to repeat a block of
instructions until a condition occurs
●
Also in this case it is possible to nest more loops one
inside the other
While..do e Do..while
●
The block of instructions can also never be executed, since the
condition is checked at the beginning, as long as the condition is
true, the block of instructions is executed
WHILE (condition)
statements
END DO
●
do..while instead executes the block of instructions until the
condition is false
DO
statements
WHILE (condition)
Example
●
A simple C example:
int i = 0, N = 10;
while (i != N)
{
printf("%d \n", i);
i++;
}
Example
Example
Int i = 0, N = 10;
do
{
printf (“%d \n”, i);
i++; // i = i + 1
} while (i >= N);
For loop
●
It executes a block of instructions a number of times known
from the beginning. Many programming languages force the
programmer to use a counter.
●
Generally there is a counter which is an integer variable whose
value is "changed" step by step
●
The end condition is generally determined by comparing the
counter variable with a value
for (counter = startingvalue A endvalue STEP = stepvalue )
statements
end for
Example
int i;
for (i=0; i<10; ++i)
{
printf (“%d \n”, i);
}
Example
for i in range(10):
print(i)
ARRAY
Array
●
How can I easily handle information that is structured by
nature? For example a complex number, or a vector or a
matrix?
●
Typical structured variables are arrays
– For example, a vector of floating-point numbers in C can
be declared as: float v [10];
– We can then access the i-th element of the vector: v [i-1] =
3.5;
●
Likewise I can define an array (two-dimensional array) as:
float m [10] [10];
Array
Example
●
Example loops to perform a scalar multiplication between vectors