Tutorials Home   >   Programming Fundamentals   >   Basics of Programming

Basics of Programming

1. Introduction

Programming is the process of giving a computer a set of instructions to perform a task. These instructions are written in a programming language, which the computer can understand.

Programming is essential because it allows us to:

  • Automate tasks

  • Solve problems efficiently

  • Build software, apps, and websites

  • Control devices and machines


2. What is a Program?

A program is a sequence of instructions that tells a computer how to perform a specific task.

Example:

  • A program that calculates the sum of two numbers.

  • A program that prints β€œHello, World!” on the screen.

Characteristics of a Program:

  1. Logical – Follows step-by-step instructions.

  2. Efficient – Solves the problem quickly.

  3. Accurate – Gives correct results.

  4. Readable – Easy for humans to understand.


3. Types of Programming Languages

Programming languages can be classified into:

  1. Low-Level Languages

    • Close to machine code.

    • Examples: Assembly language, Machine code.

    • Fast but hard to read and write.

  2. High-Level Languages

    • Closer to human language.

    • Examples: C, C++, Java, Python.

    • Easier to learn and maintain.

High-level languages are translated into machine language using:

  • Compiler – Translates the entire program at once (e.g., C, C++).

  • Interpreter – Translates program line by line (e.g., Python).


4. Basic Structure of a Program (C Example)

Every C program follows a basic structure:

#include <stdio.h> // Preprocessor directive

int main() { // Main function
printf(“Hello, World!\n”); // Output statement
return 0; // End of program
}

Components Explained:

  1. #include <stdio.h> – Includes standard input/output library.

  2. int main() – Entry point of the program.

  3. printf() – Prints output to the screen.

  4. return 0; – Ends the program and returns success status to the OS.


5. Steps in Programming

  1. Problem Analysis – Understand what the problem is.

  2. Algorithm Design – Step-by-step solution in simple language.

  3. Flowchart Creation – Visual representation of the algorithm.

  4. Coding – Writing the program in a programming language.

  5. Compilation/Interpretation – Translate code into machine language.

  6. Execution – Run the program to get the output.

  7. Testing and Debugging – Find and fix errors.


6. Types of Errors in Programming

  1. Syntax Errors – Mistakes in the rules of the programming language.

    • Example: Missing semicolon ; in C.

  2. Runtime Errors – Errors that occur when the program is running.

    • Example: Dividing by zero.

  3. Logical Errors – Code runs but produces incorrect results.

    • Example: Using wrong formula in calculations.


7. Input and Output in Programs

Input

  • Data received by the program from the user.

  • In C: scanf() is used to take input.

int age;
scanf("%d", &age);

Output

  • Data displayed by the program.

  • In C: printf() is used to show output.

printf("Your age is %d", age);

8. Variables and Data Types

Variables

  • Named storage locations in memory to hold data.

  • Example: int age; stores an integer value.

Data Types

Type Size (bytes) Description
int 2/4 Stores integers (whole numbers)
float 4 Stores decimal numbers
double 8 Stores double-precision decimal numbers
char 1 Stores a single character
void 0 No data (used in functions)

9. Constants

  • Values that do not change during program execution.

  • Example:

#define PI 3.1416
const int MAX = 100;

10. Operators

Operators are symbols used to perform operations on data.

Type Example Description
Arithmetic +, -, *, /, % Perform mathematical operations
Relational ==, !=, >, <, >=, <= Compare values
Logical &&,
Assignment =, +=, -= Assign values to variables

11. Control Structures

Control structures are used to control the flow of execution in a program.

a) Conditional Statements

  • Execute code based on conditions.

if(age >= 18){
printf("Adult");
}else{
printf("Minor");
}

b) Loops

  • Repeat a block of code multiple times.

For Loop

for(int i=0; i<5; i++){
printf("%d\n", i);
}

While Loop

int i = 0;
while(i<5){
printf("%d\n", i);
i++;
}

Do-While Loop

int i = 0;
do{
printf("%d\n", i);
i++;
}while(i<5);

12. Functions

  • A function is a block of code that performs a specific task.

  • Functions make code reusable and organized.

Syntax Example:

int add(int a, int b){
return a + b;
}
int main(){
int sum = add(5, 10);
printf(“Sum = %d”, sum);
return 0;
}

13. Arrays

  • An array is a collection of elements of the same type stored in contiguous memory.

int marks[5] = {80, 90, 85, 70, 95};
printf("%d", marks[0]); // Output: 80

14. Summary of Programming Basics

Concept Description
Program Set of instructions to solve a task
Variable Memory location to store data
Data Type Type of data a variable holds
Operator Symbol for operations on data
Control Structures Direct flow of program execution
Function Reusable block of code
Array Collection of similar data elements

15. Conclusion

Programming is a step-by-step process of solving problems using a computer.
Understanding basic concepts like variables, data types, input/output, control structures, and functions is essential before moving to advanced topics.