# 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.&#x20;

\\

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.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXeQND6AqYsY6u3pC3hsNLUfzDe3D3XwwxVaOXzex3hLOWOayfDQjDdRu-emeYZJ82D9zEjhSqlpXxcsDWj-t4BWrHYaGbaplu5AgQW1IpJ-4t0FsSbOjVHJ2KgKpj24JcxDlCXf-YLcb8gybIF0UMujilM?key=Ee2RD60f3fm05qJUyjaoHQ)

Here is it in flowchart form.

<figure><img src="https://content.gitbook.com/content/v8hfOD2FAX4jGYqMD1k9/blobs/grueL7syFpKxviMc0mYC/image.png" alt=""><figcaption></figcaption></figure>

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.&#x20;

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

Start program&#x20;

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXdDw9Txcsuct-GcI1qetQ59Yt9_sHbNrZ_21sEBw65SZ20S_ZCwx8kxm9FaY26r9Zb-lLX9GcgGZK63FjoeHddKoI3iVB6AVCQ15WYCiWVzXBOe_7hZ4iec4g8YLID1XUcOU0gW6l8pMQOYo94GkK7FkxM?key=Ee2RD60f3fm05qJUyjaoHQ)

Get Color Sensor RGB Value

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXeGIQItaE3B9CjDzktSznxvGdP6NSu8VhGDuBy-psmsZ0-79A08kyIbwjIO4ru9KolqHQlL2gX99g9UDG5C4GlllLlVvWPfgEJJI2Bd0R4YPow_tltsM5JHWReqSyEQdu_tOknA6yNmWmnKQcby93KGIXNP?key=Ee2RD60f3fm05qJUyjaoHQ)

Get hue from RGBA value

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXePiVIzgFQ2ZB7arDTUt5AVvRPmgMszWGcadoNKa44OSyRWWyS2n1YsJp8vgHAqfk2sW9N8WP4HR0f_QfkuD4trnT7N0hEAganmwVBSi-IgZ6MCi26fr326F7FEc2cnUNHJ9okI33KWCCb-hOMMZDUESwvE?key=Ee2RD60f3fm05qJUyjaoHQ)

Is hue < 30 (Red)?

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXdcoZCsT411rWyhhTVaQZwwe6w0nmE71CoVaAMc_kLV9apnnU4qsEUY9BtDaUoOzVsnJVz_FWUoq8_wuPFmpcwjYOb01gB9Rk9TpNqnWqIEYwE3ioTUXEbfNqIF2FGdtHsaKPMHg0dr_aUyD-Zu3R3-KvE?key=Ee2RD60f3fm05qJUyjaoHQ)

If yes:

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXeN9nTOB2XdxqTsSYhU7sWX-Olt1oRk0_D_S_9gkiN6aoPFGx9NxeCARoa-86Dt7xhxhE8Wv07e84XiIVwoWep7OSQKmEoooD7zIqEElRrQF-RCqwd_ryRNopu3Sud30FbRaXeZnsjg1mSZLl3ebcTFzCrx?key=Ee2RD60f3fm05qJUyjaoHQ)

If no:

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXeQND6AqYsY6u3pC3hsNLUfzDe3D3XwwxVaOXzex3hLOWOayfDQjDdRu-emeYZJ82D9zEjhSqlpXxcsDWj-t4BWrHYaGbaplu5AgQW1IpJ-4t0FsSbOjVHJ2KgKpj24JcxDlCXf-YLcb8gybIF0UMujilM?key=Ee2RD60f3fm05qJUyjaoHQ)

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!
