In this section, we will learn the basics of a program's structure. In different programming languages, the names (syntax) will be different, but the general format will be similar. There are two main parts to every program, setup and loop.

Transcript

Hi, welcome back to the Circuit Playground Express online workshop, in this section, we are going to learn about what is a program and how does it work.

 

So, a program has two main parts, we are going to simplify to its bare bone, there is Setup and there's a Loop.

 

So, the Setup portion of the code initializes all the settings that we run and only happens once when we start or turn on our board and then doesn't start again.

 

The other section of our Code is a Loop, loops are necessary to our code cause if we don't have a loop it will run through the code once and then it will reach the end and stop. So, what we need it to do is reach the end of a code and then start at the beginning again. So, this way it will continuous looking and this is important because a program will run 1000s of times per second through your code, so if you want to listen for a button press, you get that button press if you only ran through it once before you could push the button the program will have reached the end and stop. So, instead of with a loop, it will continue you to run until you press the button. So, let’s look at MakeCode.

 

So, when we open MakeCode, we always start with a forever loop already in the workspace and this will be where your main body of work will be. So, everything that goes in here is put into a loop, if we have set up portions of the code will put them in a "start", on the start we will run through it once and it won’t run again. The sort of things you want to put in here are lights, we are going to take set brightness and we are going to put that on start. So, when we first power the board we will initialize the lights we want them to be, assuming we don't want them to change brightness throughout the program. If we have them inside the loop, then it would work the same, but you would be setting the code brightness every time the program restarted and can result to slowing your program and burdening your code, so it’s good to have those one-time changes out of your loop.

 

So, a good example of something that belongs in your loop is having an animation for 0.5 of a second. if we look at our virtual board, we can see rainbow animations playing and it's continuing to play because after 0.5 seconds the loop starts over without a gap. If we were to take it out of the loop and put it on start it will run once and then stops.

 

the main body of code needs to go within the loop, so it continuously runs. Something that can get a little confusing about MakeCode is that it’s a little different from a standard program is that it can have as many loops as you want, and it will still work just the same. So even though I'm throwing a bunch of loops on the virtual board, they will all run as if its one loop, all this does it make it easier to see all your code.

 

That completes this section on the basis of a program, stick around for inputs and outputs.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.