in IB pseudocode and Arduino
Flow control refers to the tools that we have to make some code to be executed and some that we don't to execute with some conditions.
We can also see it as "valves" that regulate what happens and what doesn't.
(source)
This is mostly common as the if statements and their colleagues. There are several ways to write the same thing in different programming languages but the syntax is very similar.
The if statement has 2 main parts.
A condition in an if (or elsewhere) needs to be something that it's either true or false.
A variable that is a Boolean can be either true or false. For example
In this case we know that X is true but we can have some other part of the code that changes its value.
An expression that evaluates if something is equal to something else or compares its value can also be either true or false.
In this case X is not lesser than 2 so nothing is output
This conditions can be combined with logic gates (most common are AND, OR, NOT) that are covered later.
The statement is just code that is executed in case the if happens. It's important that many programming languages have different ways to confine this part and make it distinct from the rest of the code.
For example, in IB pseudocode we have to use "then" to start writing the conditionally executed code and "end if" after it.
In this case OwO is output only if N is lesser than 2, but POTATO is going to be output always
In python the start of the if is after a ":" and some indentation (line 2 has some space before the actual word)
In this case "hi there, " is output only if x is bigger than 3, but "how are you?" is going to be output always
In C++ or Java we use curly braces "{}" to mark this space, they can be inline or in different lines but we usually write them in different lines and also use indentation to make the code easier to read.
In this case "CS" is output only if number is bigger than 5, but "IB" is going to be output always
The else statement will be executed only if the condition is not met. It's a short hand to write if the opposite of the previous if doesn't happen. It's written after the statement of the if (What happens if the if condition is met)
Example
In this case OwO is output only if N is lesser than 2, if N is 2 or bigger, "UwU" will be output but POTATO is going to be output always
We can have and if statement that will not be met and then and else if will be executed only when a second condition is met.
Example
In this case "OwO" is output only if N is lesser than 2, if N is 2 or bigger and X>3, "UwU" will be output . If N is 2 or bigger but X is equal or less than 3 "UwU" will not be output nor "OwO". POTATO is going to be output always
In python this "else if" is written elif
. source
The final else (written just "else") can be added after an else if to be executed if the else if didn't triggered.
Example
In this case "OwO" is output only if N is lesser than 2, if N is 2 or bigger and X>3, "UwU" will be output . If N is 2 or bigger but X is equal or less than 3 "n.n" will be output.
In one if you can add (in most programming languages) as many else if as needed this creates
In many cases we can have several ifs one inside other if. This is called to "nest" if into each other and can have all the levels that we need, but the more levels that we have the more difficult is to read the code. All of them should have their own sintax. In the case of IB pseudocode the "end if" but in other programming languages their own identation or curly braces. Let's see some examples of nested ifs
In this case X is going to augment by 2 if POTATO is true and if POTATO is true and EGG is true, we're also going to smashEggsWithPotatos() and augment by 1 the variable Y
The same example in c++ arduino flavor:
In Python
In my opinion is more difficult to spot the nested ifs in python since the only difference between blocks of code is the identation. But nothing you can't get used to!
These nested ifs can get more complex because each of them can have their own else and else if
It's something usual that when using an if we don't use only one condition but several that have a relationship using logic gates (most common AND, OR, NOT) in those cases the structure is the same but the condition will be a little bit more complex.
Remember that in Pseudocode we use AND, OR and NOT but in many programming languages they have their specific symbols. Most common are "&&" for AND, "||" for OR and "!" for NOT
One common example is use for range (for example if we want a certain value to be over a value but less than other)
The same
This mistake occurs because in math we can say as a way to determine a range of values.
If the complexity of the condition is too complex or too long it's common to see the compounded condition to be split in different lines. Let's see this example where we want to check if a certain letter is a vowel
We can write it like this:
This is common to be seen when the conditions are easy to be understood in parts.
You can use this technique in exams and tests and it will be understood. Remember to write clearly the logic gates (in this case the "or" gates) in between but not at the first line and use the "then" clearly at the end.
We have seen that sometimes we have one value that we can do a specific action depending on it's value. This usually leads to a very long algorithms.
You can see it here
https://www.w3schools.com/java/java_switch.asp
https://www.w3schools.com/cpp/cpp_switch.asp
In Python there is no switch (https://ellibrodepython.com/switch-python)
// TO-DO