David Prieto
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 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/

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully