10. Introduction to Programming (Basic Concepts) Variables and Data Types, Operators, Conditional Statements, Loops, Basic Problem Solving,
Programming is the process of writing instructions that tell a computer how to perform specific tasks. These instructions are written using a programming language. Programming allows computers to solve problems, process data, automate tasks, and create applications such as websites, mobile apps, and software systems.
A program is a set of logical instructions that a computer follows step by step to achieve a desired result. To write effective programs, a programmer must understand several basic concepts such as variables, data types, operators, conditional statements, loops, and problem-solving techniques.
These concepts form the foundation of all programming languages.
A variable is a named storage location in a computer's memory used to store data that can change during program execution.
Variables allow programmers to store information such as numbers, text, or logical values that the program needs to process.
Each variable has:
Example:
age = 20 name = "Ali"
Here:
age is a variable storing the number 20.name is a variable storing text.Variables make programs flexible because their values can change during execution.
Most programming languages follow certain rules for naming variables:
Example of valid variable names:
totalMarks studentName price
Example of invalid variable names:
2marks student name class
A data type defines the kind of data that a variable can store and the operations that can be performed on it.
Common data types include:
An integer represents whole numbers without decimal points.
Examples:
10 -25 1000
Used for counting, indexing, and calculations.
Floating-point numbers represent numbers with decimal points.
Examples:
3.14 10.5 -0.75
Used in scientific calculations and measurements.
A character represents a single symbol such as a letter, digit, or special character.
Examples:
'A' 'B' '9'
Characters are often stored using encoding systems like ASCII or Unicode.
A string is a sequence of characters.
Examples:
"Hello" "Computer Science" "Pakistan"
Strings are commonly used to store names, messages, and textual data.
Boolean data type represents logical values.
Possible values:
True False
Used in decision-making and conditional statements.
Operators are symbols used to perform operations on variables and values.
They are essential for performing calculations and logical operations in programs.
Arithmetic operators perform mathematical calculations.
Examples include:
+ Addition - Subtraction * Multiplication / Division % Modulus (remainder)
Example:
sum = a + b
If a = 5 and b = 3, then sum = 8.
Relational operators compare two values and return a Boolean result.
Examples include:
> Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to
Example:
a > b
If a = 10 and b = 5, the result is True.
Logical operators combine multiple conditions.
Common logical operators:
AND OR NOT
Example:
(age > 18 AND citizen == True)
The result will be True only if both conditions are true.
Logical operators are widely used in decision-making structures.
Conditional statements allow programs to make decisions based on certain conditions.
They enable the program to execute different instructions depending on whether a condition is true or false.
The if statement executes a block of code only if a condition is true.
Example:
if marks >= 50 print "Pass"
If the marks are greater than or equal to 50, the message "Pass" will be displayed.
The if-else statement provides two alternative paths.
Example:
if marks >= 50 print "Pass" else print "Fail"
If the condition is true, "Pass" is printed; otherwise "Fail" is printed.
Used when there are multiple conditions to evaluate.
Example:
if marks >= 80 print "A Grade" else if marks >= 60 print "B Grade" else if marks >= 50 print "C Grade" else print "Fail"
This structure allows programs to make complex decisions.
Loops allow a block of code to execute repeatedly until a certain condition is met.
Loops are useful when performing repetitive tasks such as processing multiple items in a list.
A for loop is used when the number of iterations is known.
Example:
for i = 1 to 5 print i
Output:
1 2 3 4 5
The loop repeats five times.
A while loop executes as long as a specified condition remains true.
Example:
while number < 5 print number number = number + 1
The loop continues until the condition becomes false.
This loop executes the block of code at least once before checking the condition.
Structure:
do statements while condition
This type of loop is useful when the program must run at least once.
Programming is fundamentally about solving problems logically and efficiently.
The general steps for solving programming problems include:
The programmer must clearly understand what the problem requires.
Example:
Calculate the average marks of students.
Before coding, the programmer designs the solution using:
This helps visualize the logic.
The solution is then implemented using a programming language such as C, Python, Java, or PHP.
After writing the program, it must be tested using different inputs to check whether it produces correct results.
Errors found during testing are corrected. This process is called debugging.
The program may be improved to make it faster, more efficient, or easier to understand.
Introduction to programming provides the fundamental building blocks needed to create computer programs. Concepts such as variables, data types, operators, conditional statements, and loops allow programmers to write instructions that control the behavior of a computer. These concepts also help in developing logical thinking and problem-solving skills, which are essential for designing efficient and reliable software systems. Understanding these fundamentals is the first step toward mastering programming and computer science.