πŸ’©

programierds

Back to course
Lessons 1 / 12

Basic Concepts and Algorithms

Basic Programming Concepts

The Algorithm

An algorithm is a set of steps that helps us solve a problem. It is like a cooking recipe, where each step must be followed in order to get the desired result.

For example, if we want to bake a cake, we have to follow an algorithm that tells us the steps to take: mix the ingredients, put the mixture into a pan, bake the cake, and then decorate it. If we follow the steps correctly, we will have a delicious cake to enjoy.

In Java, we can write algorithms as code:

int number1 = 5;
int number2 = 3;
int result = number1 + number2;
System.out.println("The result is: " + result);

Here, we are declaring two variables, number1 and number2, adding them, and storing the result in a third variable, result.

Instructions

Instructions are the set of commands we give to a computer program so it performs a specific task. Each instruction represents an action the program must carry out.

Some examples:

  • Value assignment: int number = 5;
  • Math operations: int result = 5 + 3;
  • Conditions:
if (number1 > number2) {
    System.out.println("Number 1 is greater than number 2");
}

Good Programming Practices

  1. Descriptive names: Use names that reflect the purpose of the variable or function.
  2. Clear comments: Explain the β€œwhy”, not only the β€œwhat”.
  3. Divide and conquer: Split your code into small, cohesive functions.
  4. Avoid duplication: If you see repeated code, encapsulate it.
  5. Keep it simple: Avoid overly complicated solutions.
  6. Error handling: Use mechanisms such as try-catch.
  7. Formatting and style: Keep indentation and style consistent.
  8. Test your code: Run thorough tests.
  9. Learn from others: Read code written by other developers.
  10. Practice regularly: Consistency is the key to improving.