Think Like a Coder

Think Like a Coder

Aug 23, 2024

Hey there! Let’s keep it going with the basics. So far, we’ve covered how computers represent information. Now, let’s dive deeper into how machines make decisions. First things first - variables.

If you want to make a more complex program, you need a way to store information in memory. That’s where variables come in. You can think of a variable as a box that holds something. A variable has a name (or label) and contains a value. When you assign a value to a name, you create a variable. You can then use that name to refer to the value later in your program. For example, you might use a variable to store the reading of a temperature sensor.

In Python, you could do something like this:

temperature = 25

Variables can be reassigned as many times as you want. You can also perform operations with them. Let’s imagine you have a temperature control system in your house. You could write code that checks the temperature and adjusts the heating system accordingly:

temperature = 25  # current temperature
desired_temperature = 22  # target temperature

if temperature > desired_temperature:
    print("Turning on the AC...")
elif temperature < desired_temperature:
    print("Turning on the heater...")
else:
    print("Temperature is just right!")

Combining basic operations like this allows computers to handle more complex tasks.

As computer processing power increases, machines can tackle even more complicated problems. But for a machine to perform a complex task, a few things need to happen:

  • If the task is complex, it must be broken down into simpler steps.

  • The instructions need to be given in the right order.

  • The instructions need to be clear and precise.

  • The instructions must be given in a language the machine can understand.

Machines can complete tasks for us, but first, they need to know how. The best way to break a task down for a machine is by creating an algorithm.

An algorithm is a set of steps that precisely describe how to complete a task - just like a cooking recipe.

Algorithms can be represented in various forms. If you’re new to algorithms, natural language is a great place to start. For example:

  1. Check the current room temperature.

  2. Compare the current temperature to the desired temperature.

  3. If the current temperature is higher than the desired temperature, turn on the air conditioning.

  4. If the current temperature is lower than the desired temperature, turn on the heater.

  5. If the current temperature matches the desired temperature, do nothing.

Flowcharts can help you visualise algorithms, while more complex algorithms might involve loops for repetition or branches for decisions.

  1. Start

  2. Read current temperature

  3. Compare current temperature with desired temperature

    • Is current temperature > desired temperature? → Turn on AC

    • Is current temperature < desired temperature? → Turn on heater

    • Else → Maintain current state

  4. End

(Imagine a simple flowchart diagram here: Starting with "Read temperature", branching into three decision paths based on whether the temperature is higher, lower, or equal to the desired temperature.)

Pseudocode is another way to represent algorithms, using a simplified language that’s closer to the programming language. Pseudocode describes the steps a machine should take, in a way that’s easier for humans to understand but still precise.

START
  Read current temperature
  IF current temperature > desired temperature THEN
    Turn on AC
  ELSE IF current temperature < desired temperature THEN
    Turn on heater
  ELSE
    Maintain current state
  END IF
END

Regardless of the programming language you use, programming concepts and constructs are transferable. Once you know how to code in one language, you can easily pick up another.

Programming always involves three steps:

  • Writing code.

  • Executing or running the program.

  • Fixing errors, also known as debugging.

Learning to code is easier than you think. Patience and determination are key to overcoming challenges, especially when debugging. Fortunately, you’re not alone. Keep learning, and don’t give up!

Cheers, Irina 😊

¿Te gusta esta publicación?

Comprar Irina Vasilița un tea

Más de Irina Vasilița