Studyfin Studyfin ✨ Sign up free

Comments & code documentation

Grade 7 · Coding · Free lesson

Hey there! Ready to learn how to write secret notes in your code that only humans can read?

When we write code, computers follow our steps exactly. But sometimes, other humans need to read our code to understand our plan. We use comments to leave helpful notes that the computer completely ignores.

Think of a comment like a sticky note on a map. The computer skips over it, but a human programmer reads it to understand what the code does. This helps us design better programs and solve problems without getting confused.

moveForward() turnRight() moveForward() # This makes the # robot turn around # the red obstacle!

We can use comments to plan our code before we even write it. This is called pseudocode. By writing our steps in plain English first, we can design loops, conditionals, and events to solve any everyday problem easily.

Step 1: # Ask user for their age Step 2: # If age is over 12, allow game Step 3: # Else, show friendly warning
✏️ Worked example

Imagine we are designing a program to water a plant when the soil gets dry. Let's use comments to plan our design step-by-step before we write the actual code.

  1. First, analyze the problem: We need to check if the soil is dry, and if it is, turn on the water pump.
  2. Write a comment to state our main goal: '# Goal: Water plant when dry.' This keeps us focused.
  3. Write a comment to plan the sensor check: '# Step 1: Read the moisture level from the sensor.'
  4. Write a comment to plan the decision: '# Step 2: If moisture is below 30, turn on the pump.' This is our conditional statement.
  5. Write a comment to plan the loop: '# Step 3: Repeat this check every hour.' This is our loop statement.
  6. Review the comments. Now, any programmer can read our plan and turn it into real code easily!
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons