Making a decision based on a single value (if statements)
When the computer needs to “decide” which branch of a flowchart (or algorithm to follow) it evaluates a variable against some condition that we place on it. These decisions are frequently documented in a condition/action table.For instance for the decision below: if age >= 18 we could document it like this:
Download Flowgorithm 2.28 Windows. Currently, Flowgorithm only runs under Windows. An Android and iPhone version are under development, but are not ready yet. Flowgorithm only has a few reserved words that are used in expressions. And nottrue falseor modpi Data Type Keywords To prevent confusion, the system also prevents identifiers from using the data type names. Booleanreal integer string Illegal Keywords (used in functions) Flowgorithm does not permit the names of intrinsic functions to be used.
path | condition | Action |
---|---|---|
User is 18 or older | age >= 18 | Print “Go vote!” |
User is less than 18 | age < 18 | Print “Sorry, not yet” |
The diamond shape is used to designate a decision. The “if” is not listed, just the condition.
NOTE: Flowgorithm does not allow you to use multiple lines in an Output display, so you have to do each line with a separate Output symbol. Ask the user to input a menu choice. If the user enters 1, 2, 3 or 4, display the appropriate greeting. .not supported in Flowgorithm 9/18/2015 Sacramento State - CSc 10A Different Types of Decisions Decision structures rely on having the computer compare data and make a decision based on the result For this, computers use Boolean Logic.basis of all computer (and human) logic.Boolean values can be either True or False.
- Declare a variable for age
- Output the instruction to the user and store the result in age
- Add an IF shape, then double-click the shape to add the condition
- Back on the flowchart, click on each branch of the if statement and add an action that the program will take (i.e. the message that is output to the user)
Making a decision based on multiple values (if statements with Boolean operators)
In the case of multiple conditions that need to be met, we would use Boolean operators to chain two or more conditions together. Boolean operators:
- AND – all conditions must be true in order to execute the code
- OR – only one of the conditions must be true in order to execute the code
- ! - NOT means that the code will only execute if the condition is not true
For instance for the decision below: if age >= 18 AND enrolled = true we could document it like this:
path | condition | Action |
---|---|---|
User is 18 or older | age > = 18 AND enrolled = true | Print “Go vote!” |
User is less than 18 or not enrolled | age < 18 OR enrolled = false | Print “Sorry, not yet” |
Making decisions using multiple values with != (if statements checking for a negative condition)
Many times we need to force the user to enter a certain value that we are looking for. This is especially true when we need to validate the users input before continuing on with the program.
The != operator (not equal) is very useful for this task.The flowchart below asks the user to enter a specific character from the menu. If the condition is met (the user didn’t enter an A, B or C) the true path is chosen. If the condition is not met (user entered an A, B or C) the false path is taken.
Note: We use AND rather than OR here because all conditions need to evaluate to true. For example, they didn’t enter any of the valid inputs (A, B and C).
Nested If (a condition within a condition)
At times we may need to test for a certain condition, and then run a further test on another condition. However, if we want the program to be able to keep track of these individual evaluations we will need a nested conditional statement.
Think of this as an if statement inside another if statement.
In a previous section we were evaluating if a person was old enough to vote AND was enrolled to vote. The condition action table looked like the following:
Flowgorithm &
path | condition | Action |
---|---|---|
User is 18 or older | age > = 18 AND enrolled = true | Print “Go vote!” |
User is less than 18 or not enrolled | age < 18 OR enrolled = false | Print “Sorry, not yet” |
In this case we can only respond to the two conditions:
- Either the user is old enough to vote and is enrolled
- Resulting in message: “Go vote”
- Or one of the conditions is false
- Resulting in the message: “Sorry, not yet”
If we want to be able target each individual condition, we will need a nested structure.
Path | Condition | Action |
---|---|---|
User is 18 or older and enrolled | age >= 18 AND enrolled = true | Print “Go vote!” |
User is 18 or older and is not enrolled | age >= 18 AND enrolled = false Print | “You are old enough to vote, but are not enrolled.” |
User is not 18 or older and is enrolled | age < 18 AND enrolled = true | Print “You are not yet 18 and are enrolled” |
User is not 18 or older and is not enrolled | age < 18 18 AND enrolled = false | Print “You are not yet 18 and are not enrolled” |
Only one dimensional arrays are supported in the Flowgorithm and
limited to the integer, real (floats), boolean and string types.
However, two dimensional arrays are natively required in many algorithms.
Vectors and matrices are indexed from '1' not from '0' .
This page describes, how two dimensional matrices can be defined, used and printed.
It is recommended to understand how the Flowgorithm source files ( *.fprg files) can be modified directly for additional libraries inclusion.
Please, see EDITING Flowgorithm Files
(unfortunately, a library handling is not available in the current Flowgorithm version, perhaps a specialized tool for *.fprg files will be available)
The Flowgorithm uses indexing from ZERO, i.e. the A[0], ... , A[N-1], which might cause some problems in mathematical formula use.
The following functionality is available (indexing [1..N] is to be used consistently).
In mathematics, usually the first index is ONE, i.e. A[1] in the vector case, resp. A[1,1] in the case of matrices
Declaration (N, M can be integer constants or variables)
- one dimensional arrays: A[1:N] - declare A[N] - generates actually a vector
- two dimensional arrays: A[1:N,1:M] - declare A[N*M] - generates actually vector for a matrix storing.
Indexing
As the Flowgorithm supports one dimensional arrays indexed from ZERO, each element of two dimensional array has to be re-indexed independently with checking the bounds of a vector or a matrix,
e.g. a mathematical formula k = A[i,j] is to be rewritten as k = A[ Index ( i, j, N, M )] in the case of matrix with integer elements.
All indexing starts from ONE, i.e. [1..N].
Vector and matrix support (indexing from '1')
The following re-indexing functions can be used for matrices; in the case of a vector,
elements can be accessed directly by decreasing index by '1', but without bounds detailed control.
(a vector A has N elements, a matrix A - has N rows, M columns):
The vector case (indexing from '1')
- Index1 ( i, N ) - returns integer value of the physical index for the integer vector element A[i] (matrix type independent)
The matrix case [2D arrays] (indexing from '1')
- Index ( i, j, N, M ) - returns the integer value of the physical index for the matrix element A[i,j] of the size NxM (matrix type independent)
- Index2 ( i, j, N ) - returns the integer value of the physical index for the matrix element A[i,j] of the size NxN (square matrix; matrix type independent)
Printing vectors and matrices with formatting (indexing from '1', resp. '1,1')
The Flowgorithm supports only 'non-formatted' output of one value only. However, formatted output is required in many cases.
Also for matrix output formatting is needed to see columns.
Unfortunately, output natively uses proportional font type on the screen.
However if output is stored, a non-proportional font type, e.g. Consolas in the Notepad, can be used.
For formatted print the following calls can be used:
Print of a value (it is expected, that d <= length of characters printed)
- PrintI ( i, d ) - prints integer value on the d positions
- PrintR ( r, d1, d2 ) - prints integer value on the d1 positions with d2 decimal points
- PrintS ( s, d ) - prints integer value on the d positions
- PrintB ( b, d ) - prints integer value on the d positions
Print of a vector
Flowgorithm Functions
- PrintVectorI (A, N, d ) - prints integer vector elements on d positions (N rows, M columns)
- PrintVectorR (A, N, d1, d2 ) - prints real vector elements on d1 positions with d2 decimals (N rows, M columns)
- PrintVectorS (A, N, d ) - prints string vector elements on d positions (N rows, M columns)
- PrintVectorB (A, N, d ) - prints boolean vector elements on d positions (N rows, M columns)
Print of a matrix
- PrintMatrixI (A, N, M, d ) - prints integer matrix A elements on d positions (N rows, M columns)
- PrintMatrixR (A, N, M, d1, d2 ) - prints real matrix A elements on d1 positions with d2 decimals (N rows, M columns)
- PrintMatrixS (A, N, M, d ) - prints string matrixA elements on d positions (N rows, M columns)
- PrintMatrixB (A, N, M, d ) - prints boolean matrix A elements on d positions (N rows, M columns)
Flowgorithm Examples
Operations with vectors and matrices
- CopyVector (A, B, N) - copy a vector A to vector B of the size N
- CopyMatrix (A, B, N, M) - copy a matrix A to matrix B of the size N rows, M columns
- Transpose (A, B, N, M) - transpose a matrix A (size N rows, M columns) to a matrix B (size M rows, N columns)
- DotProduct (A, B, N) - result is a real value
- CrossPruduct (A, B, C, N) - result is a vector C (actually the outer product in the case N>3)
- GeometricProduct (A, B, C, D, N) - result is a bi-vector C and a scalar value D
Solution of a linear equations
- SolveGaussSeidel (A, x, b, N) - solution of a system of linear equations Ax = b, size of the matrix A is N by N, x is vector of unknowns, b is the right side
(the Gauss elimination method is used) - Solve (A, x, b, N) - solution of a system of linear equations Ax = b, size of the matrix A is N by N, x is vector of unknowns, b is the right side
(the iterative method is used, matrix A must be strictly positive definitive)