# Flow control in programming _in IB pseudocode and Arduino_ :::info For the new syllabus (starting in May 27) I've made a revision of this note being specific in python. You can check it here: https://hackmd.io/@dprieto/flow-control-python ::: 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. ![vag-know-regelventile-control_valves](https://hackmd.io/_uploads/rykhyPWkyg.jpg) _([source](https://www.vag-group.com/en/did-you-know/what-is-the-best-installation-position-for-pilot-operated-control-valves))_ 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. ## if statement The if statement has 2 main parts. * The condition (something that has to be resolve to either true or false) * The statement that's going to be executed in case that the condition is actually true. ### The condition 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 ```= X = true if X then output "OwO" end if //"OwO" is output ``` :::info 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. ```= X = 32 if X<2 then output "OwO" end if // nothing is output ``` :::info 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 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. ```= if N<2 then output "OwO" end if output "POTATO" ``` :::info 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) ```python= if x>3: print("hi there, ") print("how are you?") ``` :::info 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. ```cpp= if (number > 5) { Serial.println("CS"); } Serial.println("IB"); ``` :::info In this case "CS" is output only if number is bigger than 5, but "IB" is going to be output always ::: ## Else 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 ```= if N<2 then output "OwO" else then output "UwU" end if output "POTATO" ``` :::info 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 ::: ## Else if 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 ```= if N<2 then output "OwO" else if X>3 then output "UwU" end if output "POTATO" ``` :::info 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 ::: ::: success In python this "else if" is written `elif`. [_source_](https://www.w3schools.com/python/python_conditions.asp) ::: ### Final else The final else (written just "else") can be added after an else if to be executed if the else if didn't triggered. Example ```= if N<2 then output "OwO" else if X>3 then output "UwU" else output "n.n" end if ``` :::info 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. ::: ## Nested if 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 ```= if POTATO == true then X = X + 2 if EGG == true then smashEggsWithPotatos() Y = Y + 1 end if end if ``` :::info 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: ```cpp= if (potato == true) { x = x+2; if (egg == true){ smashEggWithPotatos(); y = y+1; } } ``` In Python ```python= if potato == True: x = x+2 if egg == True: smashEggWithPotatos() y = y+1 ``` ::: success 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 ## Compounded 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. :::info 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) ```= X = 20 if X<18 AND X<40 then output "OwO" end if // "OwO would be output" ``` The same :::danger :no_entry: :no_entry: :no_entry: :no_entry: The following example is a common error, in pseudocode (and in almost all programming languages) we need to split the conditions and use the operator AND. ```= X = 20 if 18<X<40 then // WRONG output "OwO" end if // "OwO would be output" ``` This **mistake** occurs because in math we can say $18<x<40$ as a way to determine a range of values. :no_entry: :no_entry: :no_entry: :no_entry: ::: 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 ```= C = INPUT if C = 'A' or C = 'E' or C = 'I' or C = 'O' or C = 'U' then output "You have input a vowel!" end if ``` We can write it like this: ```= C = INPUT if C = 'A' or C = 'E' or C = 'I' or C = 'O' or C = 'U' then output "You have input a vowel!" end if ``` 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. ## Switch :::info :information_source: This is not needed for the IB exam but is very common in many programming languages ::: 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) ## Other examples (from students) // TO-DO ### References https://www.geeksforgeeks.org/decision-making-c-cpp/