Introduction to Programming
At its core, programming is simply the act of writing a strict set of instructions for a machine. Computers are infinitely fast, but they are infinitely stupid. They do exactly what you tell them to do—no more, no less. They cannot guess your intentions.
Imagine writing directions for a robot to make a cup of coffee. You can't say "make coffee." You have to say: "Walk to the kitchen. Open the cabinet. Take one mug. Place it on the counter. Open the coffee jar..." and so on, step by step. That is programming.
The "Blame" Rule
When a program crashes, who is to blame? Always the person who wrote the code. Even when AI (ChatGPT, GitHub Copilot) writes code for you—if the code fails, it is your fault as the prompter. You must verify the logic. The computer never makes mistakes; it only exposes ours.
Visualizing Logic: Flowcharts
Before writing code, programmers plan the logic using flowcharts. Each shape represents a different type of instruction:
Here is a classic real-world scenario: "Should I bring an umbrella?"
Example 2: Multi-Branch Logic (BMI Calculator)
Most programs deal with more than just a simple Yes/No choice. A BMI (Body Mass Index) Calculator demonstrates how a program takes multiple user inputs, performs a mathematical calculation, and evaluates multiple conditions to find the correct category.
How the Logic Flows:
- Input: Ask the user for their Height (meters) and Weight (kilograms).
- Process: Calculate the BMI value using the
formula:
Weight / (Height²). - Decision Tree: Check if the score is greater than or equal to 25. If yes, classify as Overweight. Otherwise, classify as Normal/Underweight.
Example 3: Repetition & Game Loops (Guess the Number)
So far, our flowcharts only run once from top to bottom and then stop. But interactive applications, websites, and games need to run continuously. They use Loops to repeat sections of code until an exit condition is met.
Consider a game of "Guess the Number (1 to 100)". This demonstrates a classic loop where the arrow literally routes back to repeat the input phase until the player finds the correct number.
The Game Loop Cycle:
- Setup: The game generates a random secret number once.
- Input: Ask the player to enter a guess.
- Win Check: If the guess matches the secret, the player wins, and the loop breaks.
- Hint Process: If incorrect, determine if the guess was too high or too low, give a hint, and loop back to Step 2.