Logic and Loops

Logic and loops are vital for responsive code. Logic is most commonly seen with the if statement, they allow the program to make decisions based on conditions. They generally follow the pattern of if something is true then do this otherwise do this. These if statements can be chained together to then form complex logic structures.

Loops are most prevalent with the repeat loop. They simply repeat some chuck of code several times until a condition is no longer true. This is useful for not only saving yourself from copy and pasting the same steps over and over but also making code more readable. This helps allow code to become smaller and coding faster.

By combining loops and logic, complex code can be created.

All of this is sounding a little confusing. Luckily, programmers have an easy way of visualizing it: flowcharts. Flowcharts are a graphical representation of a process or workflow. It uses various shapes connected by arrows to illustrate the sequence of steps, decisions, and actions involved. Flowcharts are often used in business, engineering, and programming to visualize complex processes and make them easier to understand.

Here is an example:

This is a simple color detection algorithm.

Here is it in flowchart form.

Do you see how much easier it is to understand the code now? It is much more intuitive and the meaning is clearer. Without even learning how flowcharts are read you can already understand it!

Now let's dive deep and analyze what each part means. Each shape of the flowchart represents different things:

Oval: Represents the start and end of a process.

Rectangle: Represents a process or activity.

Diamond: Represents a decision point.

Arrow: Represents the flow of the process.

Parallelogram: Represents input or output.

Reading from the flowchart it is easy to see that this is meant to continuously monitor if the color red has been detected or not. Now implementing this into code becomes easy.

Here is a breakdown of the process adding each block from the flow chart:

Start program

Get Color Sensor RGB Value

Get hue from RGBA value

Is hue < 30 (Red)?

If yes:

If no:

As you can tell, flowcharting can be lifesaving when creating complex code with multiple steps. Good coders will often use these flow charts to map out what they will write before coding. If you wish to write both efficiently and effectively you should use flowcharts too!