Comments and Syntax

Reading and Commenting on Code

SparkiDuino
Easy

Lessons You Should Know

What’s with All the Weird Slashes and Semicolons and Stuff?

So, as you may have figured out by now, code is weird to start reading and writing if you’ve never seen it before. There are a whole bunch of symbols all over the place that probably don’t make sense to you right now. Let’s take a little time to figure out some of the ones you’re going to be seeing over and over again. They’re all there for a reason and when you’re missing even just one of them the computer or robot won’t understand what you’ve written! Here’s what we’ll be covering in this lesson:  

Comments: They Help! (No Really, They Seriously Do)

Ok. Take a deep breathe, if this is your first time to write code you first need to realize that it’s like learning a new language (it actually is a new language). It’s going to be a while before you can easily read and write in this language, even longer before you can write novels or poems. But it’s definitely worth it! With your new found language you will be able to control robots, video games, internet servers and communicate all over the world. world robot Luckily for you there are these things called “comments” they are your buddies and the only reason they exist is to help you out. Comments don’t effect anything in the code as far as the robot is concerned. In fact- the computer actually throws them out completely before giving the code to the robot! So why do we care? Because they give us useful information and if you’ve never seen them before they’ll be confusing until you know that they don’t do anything to the code. Here are two example of how to write comments: There are two different ways to write comments. The first, which you will see the most, is by typing two slashes //. Any words written on the same line after these two slashes is a comment and is intended to help humans trying to figure out the code. This way of writing code can be used on a line after some code, but it is only good to write comments on a single line and you can’t write any code on that same line after you’ve written a comment. The second way to write comments is by using the /* and */ commands.  Anything you write between these two commands will automatically be a comment. You could make just one line of comments (but why would you when you could just use //?) or you could make a million lines of comments. It doesn’t matter how many lines of comments you make to the computer and robot because comments don’t take up any space after the computer throws them out. (So they’ll take up a little space in your saved file, but none in the code on the actual robot.) Any code between these comment commands (/* and */) will be ignored by the robot just like your comments. So, while it may seem like a pain at first, make sure you get in the habit of using comments. Use them to keep track of information that is important to your code or to help people who may look at your code in the future. Even if you write the most amazing code in the world to make a sweet robot that cleans up your room for you, it’s not very useful if no one after you can understand or use the code because there are no comments! Comments are great for something else while you’re writing code- taking pieces of code out of the robot’s commands but leaving them where you can still see them. For example- Don’t worry if you don’t understand any of this code yet. Focus on the comment. Because the command sparki.moveRight(); is commented out Sparki won’t ever turn to the right. BUT! We can still see the code written there. So we can have Sparki run this code and see how it works without the section we commented out, but it will be very easy to put the command sparki.moveRight(); back into the code by deleting the //. You’ll often want to remove parts of your code as you learn to write and test code. Comments are the best way to do this. Lastly, you can also use comments to provide information about your code, such as who wrote it, when it was written and what it is intended to do. Very important since you won’t always be there to explain your code!

Semicolons: (They’re not just a wink)

You’ve probably noticed the semicolons everywhere in the code you’re learning how to write. If you haven’t noticed them, take a look at any of the examples of code supplied on this website- they’re everywhere! So what exactly do they mean? Semicolons are basically a way for the robot or computer to understand that it has come to the end (the termination) of a simple set of instructions and that it should have enough information to execute a complete command. For this reason semicolons are referred to as a statement terminator. They tend to come at the end of almost every line of code except for loop related lines of code. You’ll see them at the end of all the commands that declare (create) or assign (change) variables. You’ll also see them at the end of lines that use input or output commands to work with things like sensors and motors.  

Parentheses: (with Ketchup)

Parenthesis are usually used in one of two ways- either to ask questions (these are called conditional) or to pass information to chunks of code. To ask a question with parentheses a programmer will type a variable or equation inside of a set of parentheses, if the variable or equation is equal to true then the code that comes after the question in the parentheses is executed. If statements are a perfect example of this usage of parentheses. When passing information using parenthesis the parentheses usually come after a function command. You can even pass multiple pieces of information inside a single set of parentheses! The function then takes this information and uses it somehow in the chunk of code the function executes. For example, a sparki.moveForward(10); command will pass the number ten to the code inside the moveForward function making Sparki move forward ten centimeters. If the function was passed a value of two, instead of ten, then Sparki would only move forward two centimeters. Anytime there is a left hand parenthesis ‘(‘ in code there will always be a right hand parenthesis ‘)’ following soon after. You can think of parentheses in code like hamburger buns- whenever there is one facing one way you’ll find the meat of the code between them and then another parenthesis on the other side of the meat. Here’s an example of parenthesis usage-

What? Robots in burgers are totally normal.

Curly Brackets: (Yummy!)

Curly brackets are used to let the robot or computer know when a chunk of code that belongs to a particular function or command begins and ends. This is especially useful with looping code since the robot or computer needs to know when to go back to the beginning of the looping section and start over. For loops are a perfect example of this usage of curly brackets. Curly brackets are also used for sections of non-looping code so that the computer knows how much code to skip over when that section of code will not be executed. If statements are a perfect example of this usage of curly brackets. You can also think of curly brackets in code like hamburger buns- whenever there is one facing one way you’ll find the meat of the code between them and then another curly bracket on the other side of the meat. You’ll also often see sets of curly brackets inside of curly brackets! This is called “nested” brackets- it’s kind of like a quadruple (or more) layer hamburger because there are always an even number of hamburger buns, never an odd number! Here’s an example of curly bracket usage and nested curly brackets: burgerBunsEvenOdd2

 

Recap of What We’ve Covered So Far

or

Next Step:

If you’ve made it through this far you’ve definitely got what it takes to learn how to code! Keep going and learn about how to use information next, after all- information is power.

Next Lesson – A Couple More Tricks & Tips