Lesson 1 of 140

Checking lesson access...

Video and written lesson · 27 min

Introduction to Programming

Learn what programming, source code and algorithms are, how to think in input, processing and output, and how to tell syntax, runtime and logic errors apart.

PreviewCaptionsSoon

You are viewing a free preview lesson.

Lesson Overview

Every week, Nur gets marks in three subjects and wants to know her total and her average. With paper or a calculator this takes a few minutes each time, and one slip of a finger gives a wrong answer that still looks right. A program does the same work instantly, and it does it the same way every time: once its steps are correct, they stay correct.

Here is a complete Java program that solves Nur's problem. You will understand every line by the end of module 1; for now, read it like a recipe.

java
public class Main {

    public static void main(String[] args) {
        int mathematics = 80;
        int english = 90;
        int science = 85;
        int total = mathematics + english + science;
        int average = total / 3;
        System.out.println("Total: " + total);
        System.out.println("Average: " + average);
    }
}

Output:

text
Total: 255
Average: 85

You can follow the idea without knowing Java. The three lines that start with int store Nur's marks under names. The next line adds them into total, the one after it divides the total by the number of subjects, and the two System.out.println lines print each result with a label in front of it. Next week Nur changes three numbers and runs the program again. (These marks divide evenly by 3; lesson 1.12 shows what Java does when they do not.)

In this lesson, we use this small program to meet the ideas behind every program you will write: what programming and source code are, how an algorithm describes the steps, how input, processing and output give a problem its shape, how programmers work, and which kinds of errors can go wrong.

Learning Objectives

After completing this lesson, you will be able to:

  • Explain programming and source code
  • Understand what an algorithm is
  • Think about a problem in terms of Input, Processing, and Output
  • Understand the difference between a program and an application
  • Explain a common programming workflow
  • Distinguish between syntax, runtime, and logical errors

What Is Programming?

Programming is the process of designing and writing instructions for a computer. Nur's program is a list of such instructions: store these marks, add them, divide the total, print the results. A computer carries out instructions very quickly, but it decides nothing by itself. The programmer must say exactly what it should do, step by step.

The same skill makes computers do far more than add marks. Programs perform calculations, save and search data, process what a user types, take payments, send emails and notifications, and communicate with other systems. Each of these is, underneath, a set of precise instructions someone wrote.

The instructions a programmer writes, in a form people can read, are called source code. The program above is Java source code, and so is a single line such as System.out.println("Welcome to Java");, which prints Welcome to Java. A developer can read source code, but the processor cannot execute Java source code directly. The next lesson shows how it is turned into instructions a computer can run.

What Is a Programming Language?

A programming language is a structured language for writing instructions for a computer. Well-known ones include Java, Python, JavaScript, Go, C, C++, C#, Rust and Kotlin. Each has its own syntax, keywords, data types and code structure. Here is the same greeting in Java and in Python:

java
System.out.println("Hello");
python
print("Hello")

Both instructions do a similar job, but their syntax is different. What carries over from one language to another is the thinking behind the instructions, which is what the rest of this lesson is about.

Programming and Problem Solving

Before Nur's program could be written, someone had to understand the problem and break it into steps a computer can follow. Writing the syntax comes last. For her average mark, the steps are:

  1. Receive the marks
  2. Add the marks together
  3. Determine the number of subjects
  4. Divide the total by the number of subjects
  5. Display the average

This ordered sequence of steps is called an algorithm.

What Is an Algorithm?

An algorithm is a clear, specific and correctly ordered sequence of steps that solves a problem or completes a task. It does not depend on a programming language: the five steps above could be implemented in Java, Python or any other language, and the Java program is just one way of writing them down.

A good algorithm is clear, so each step means one thing. It is complete, with no step left for the reader to guess. Its steps are in the correct order: dividing before adding would give nonsense. And each step is possible to perform in practice.

Input, Processing, and Output

Many programming problems can be thought of in three parts. Input is the information a program receives; for Nur's program it is the three marks. Processing is what the program does with that input: here, adding the marks and dividing the total. Output is the result the program produces after processing: the printed total and average.

The same three parts appear everywhere. Input could be a product price, an email address a user types, an account balance or data received from another system. Processing could be comparing a password, searching for a product or checking a condition. Output could be a search result, an error message, a confirmation or a generated report. Asking "what comes in, what happens to it, what goes out?" is often the fastest way to understand a new problem.

Example: Area of a Rectangle

Suppose we need to calculate the area of a rectangle with the formula area = length × width. The input is length and width, the processing is multiplying them, and the output is the calculated area. The algorithm is:

  1. Receive the length
  2. Receive the width
  3. Multiply the length and width
  4. Display the result

In Java, it looks like this:

java
int length = 10;
int width = 5;
int area = length * width;
System.out.println(area);

Output:

text
50

At this point, understanding the problem's input, calculation and expected output clearly is more important than understanding the Java syntax.

Program and Application

Program and application are related, but they are not the same. A program is a collection of instructions that performs a specific task, and it can be very small: Nur's mark calculator, a temperature converter or a tool that renames files.

An application is usually larger software in which many features and components work together, such as a banking application, a web browser, a learning platform, a food delivery app or an online shop. An application may contain a user interface, a backend service, a database, sign-in (authentication), connections to other systems, and payment or notification features. In short, a small program performs one task; an application solves many related tasks together.

Source Code

Java source code is stored in files with the .java extension. Nur's program would be saved in a file called Main.java; other projects might have files such as GradeCalculator.java, BankAccount.java or Student.java.

A Java source file may contain classes, methods, variables, conditions, loops and object-related code. Nur's program already shows two of them: a class called Main, and inside it a method called main where the program starts. You do not need to understand these parts yet; we will learn them step by step in the following lessons.

A Common Programming Workflow

Programming is an iterative process in practice, but as a beginner you can understand it as a series of steps.

1. Understand the Problem

Before writing code, clarify what the program should do, what input it needs, what output is expected, which rules it must follow, and which situations may be invalid. If you misunderstand the problem, technically correct code can still produce the wrong solution.

2. Determine the Input, Output, and Rules

Decide what data the program will receive and what result it should produce. For a grade calculator like Nur's, the input could be her mathematics, English and science marks, and the output her total, average and grade. A rule could be that every mark lies between 0 and 100.

3. Create an Algorithm

Determine the steps that turn the input into the expected output. For the grade calculator:

  1. Receive the marks
  2. Validate the marks
  3. Calculate the total
  4. Calculate the average
  5. Display the result

4. Write the Code

Implement the algorithm in a programming language. The program at the start of this lesson is this step for Nur's problem (without the validation, which needs if statements from lesson 1.15).

5. Run, Test, and Debug

After writing the code, run the program and check that it compiles, behaves as expected, produces the correct result, and copes with unexpected input. When testing a grade calculator, try not only ordinary marks but also boundary and invalid ones: 0, the maximum allowed mark, a negative mark, and a mark greater than the maximum. A program that works for one input is not necessarily correct in every situation.

6. Improve the Solution

Once the program works, it can be improved: better variable names, less duplicate code, stronger validation, more readable code, and large pieces of logic broken into smaller parts.

Debugging

The process of finding an error in a program, understanding its cause and fixing it is called debugging. During debugging, a developer generally:

  1. Observes the error or unexpected behavior
  2. Identifies the relevant code
  3. Finds possible causes
  4. Tests a hypothesis
  5. Applies a fix
  6. Runs and tests the program again

Errors are a normal part of programming, even for experienced developers. The important skill is finding their cause systematically instead of changing code at random.

Common Types of Programming Errors

Three important categories are syntax errors, runtime errors and logical errors. They differ in when you notice them: before the program runs, while it runs, or only when you check its results.

Syntax Error

A syntax error occurs when code does not follow the rules of the programming language. In this example, the semicolon at the end of the statement is missing:

java
System.out.println("Hello")

Correct:

java
System.out.println("Hello");

In int age = ; there is no value after =, which is also a syntax error. Syntax errors are generally detected by the compiler before the program runs, so the program does not run at all until they are fixed.

Runtime Error

A runtime error occurs while a program is executing. Dividing an integer value by zero, for example, makes the program fail at runtime:

java
int result = 10 / 0;

Runtime failures can also come from a missing file, invalid input, missing data, a network failure or an unexpected system state. We will learn about Java exceptions and error handling in module 8.

Logical Error

With a logical error, the program runs, but the result is incorrect. Suppose Nur drops science, and we remove its mark but forget to change the number of subjects:

java
int mathematics = 80;
int english = 90;
int total = mathematics + english;
int average = total / 3;
System.out.println(average);

The program prints 56 without complaint, but there are now two subjects, so the correct calculation divides by 2:

java
int average = total / 2;

Logical errors are comparatively hard to find: the program may not crash, there may be no error message, and the result may look reasonable. This is why testing with values whose correct answer you already know is important.

Programming Means Precise Problem Solving

A programming language is a tool. Good programming means understanding the problem clearly, breaking it into smaller parts, determining the input and expected output, creating a logical solution, implementing it in code, testing the result and fixing errors. Memorizing syntax lets you rewrite familiar examples, but to solve new problems you need to understand the logic underneath.

Practice Exercise 1: Algorithm and Input–Processing–Output

Solutions: lesson 1.22.

Write a solution for calculating the area of a rectangle, using the formula:

text
area = length × width

Write the following separately:

  • Input
  • Processing
  • Output
  • Algorithm

You do not need to write Java code.

Practice Exercise 2: Identify the Error Type

Determine whether each example contains a syntax error, runtime error, or logical error.

Example 1

java
System.out.println("Java")

Example 2

java
int result = 100 / 0;

Example 3

java
int price = 500;
int quantity = 2;
int total = price + quantity;

Example 4

java
int age = ;

Practice Exercise 3: Break Down a Problem

Choose any one of the following systems:

  • Library management system
  • Food delivery application
  • Student management system
  • Online shopping application
  • Learning platform

Write:

  1. What problem the system solves
  2. Three possible inputs
  3. Three processing operations
  4. Three possible outputs

You do not need to write Java code.

Knowledge Check

Question 1

What is programming?

Question 2

What is source code?

Question 3

What is an algorithm?

Question 4

What do Input, Processing, and Output mean?

Question 5

What is the difference between a program and an application?

Question 6

What is the difference between a syntax error, runtime error, and logical error?

Question 7

What is debugging?

Knowledge Check Answers

Answer 1

Programming is the process of designing and writing instructions for a computer.

Answer 2

Source code is human-readable code written by a programmer using a programming language.

Answer 3

An algorithm is an ordered sequence of steps used to solve a problem or complete a task.

Answer 4

Input is the information received by a program, Processing is the operation performed on that information, and Output is the result produced by the program.

Answer 5

A program generally performs a specific task. An application is generally larger software made up of multiple related features and components.

Answer 6

A syntax error breaks the syntax rules of the language. A runtime error occurs while the program is executing. With a logical error, the program runs but produces an incorrect result.

Answer 7

Debugging is the process of finding the cause of an error in a program and fixing it.

Lesson Summary

  • Programming is designing and writing instructions for a computer. Those instructions, written in a programming language, are called source code.
  • An algorithm is a clear, ordered sequence of steps that solves a problem, independent of any programming language.
  • Many problems can be thought of as Input, Processing, and Output: what comes in, what happens to it, what goes out.
  • A program performs a specific task; an application combines many related features and components.
  • A common workflow is: understand the problem, determine the input, output and rules, create an algorithm, write the code, run, test and debug, then improve the solution.
  • A syntax error breaks the language rules and stops the program from running, a runtime error happens while it runs, and a logical error quietly produces a wrong result.

Think first

Coming soon

Checkpoint questions that ask you to commit to an answer before the lesson shows it.