Introduction to Programming - Copy
Introduction to Programming - Copy
Example:
x = 10 “A variable holding an integer”
3. Expressions and Assignments
Expressions:
o A combination of variables, constants, and operators that evaluates to a value.
o Example:
result = (x + y) * z
Assignments:
o Statements that assign the value of an expression to a variable.
int age = 25; “Assigns the value 25 to the variable age”
4. Operators
Operators perform operations on variables and values.
Arithmetic Operators: +, -, *, /, %.
Relational Operators: ==, !=, >, <, >=, <=.
Logical Operators: && (AND), || (OR), ! (NOT).
Bitwise Operators: &, |, ^, ~, <<, >>.
Assignment Operators: =, +=, -=, *=, /=.
6. Input/Output Statements
Input Statements: Allow programs to take user input.
Example:
name = input("Enter your name: ")
Output Statements: Display results to the user.
Example:
print("Hello, World!")
7. Control Structures
Control structures dictate the flow of a program.
Conditional Statements:
Example:
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Loops:
o For Loop:
for i in range(5):
print(i)
o While Loop:
while x > 0:
x -= 1
8. Error Handling
Mechanisms for managing runtime errors.
Example (Python):
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
Advantages:
Easy to understand and follow, even for non-programmers.
Language-agnostic, so it can be translated into any programming language.
Simplifies complex logic by breaking it down into steps.
Disadvantages:
Not executable directly; it needs to be converted into code.
Can be less precise in some situations, especially for complex algorithms.
2. Flowcharts
Definition:
A flowchart is a graphical representation of an algorithm that uses symbols and arrows to
depict the flow of control through the steps of the algorithm. Each step is represented by a
specific shape, and arrows indicate the direction of the process.
Purpose:
To visually represent the flow of control and decision-making in an algorithm.
To make the algorithm easier to follow, especially for complex processes.
Features:
Uses specific shapes to represent different types of operations:
o Oval: Start and end of the process.
o Rectangle: Process or operation (e.g., calculation, assignment).
o Diamond: Decision point (e.g., if-else statements).
o Parallelogram: Input/output operations (e.g., reading input, printing output).
Arrows show the order of execution.
Example:
Flowchart to find the largest number in a list:
{
}
Advantages:
Ideal for representing algorithms with complex decision-making and control flow.
Helps visualize the entire process at a glance.
Useful for both technical and non-technical audiences.
Disadvantages:
Can become cluttered and difficult to understand for large algorithms.
Not easily modifiable or reusable once created.
3. Actual Code
Definition:
Actual code is a precise, executable representation of an algorithm written in a specific
programming language. It is the final implementation of the algorithm that can be run and
tested on a computer.
Purpose:
To implement the algorithm so that it can be executed and tested for correctness.
To provide a detailed, language-specific description of the algorithm.
Features:
Written in a specific programming language (e.g., Python, Java, C++).
Includes all the syntax and rules of the chosen language.
Can be executed directly on a computer to solve the problem.
Example (Finding the largest number in Python):
python
def find_largest(numbers):
max_num = numbers[0] # Initialize max to the first element
for num in numbers: # Loop through each number in the list
if num > max_num: # If the current number is larger than max_num
max_num = num # Update max_num to the current number
return max_num # Return the largest number
Advantages:
Precise and executable; can be tested to check for correctness.
Detailed, specifying exactly how the algorithm is to be implemented.
Allows for optimization and real-world usage.
Disadvantages:
Specific to a programming language; not easily portable to other languages.
Requires knowledge of programming to understand and write.
Can be more complex to read and understand compared to pseudocode or flowcharts,
especially for beginners.
QBASIC
2. Menu Bar
Below the title bar, it contains drop-down menus with important options:
Menu Function
File Open, Save, Print, Exit QBasic
Edit Copy, Paste, Delete, Find text
View Show/hide debugging tools
Search Find and replace words in code
Run Execute the program (F5 key)
Debug Step-by-step execution for error fixing
Options Customize settings like screen color
Help Displays documentation and guidance
Tip: Most commonly used options are under File, Run, and Debug menus.
In QBasic, DIM (short for Dimension) is a statement used to declare variables and specify
their data types. It is especially useful for arrays and structured programming.
Syntax:
DIM variableName AS DataType
or
DIM arrayName(size) AS DataType
age = 25
name = "John"
price = 99.99
scores(0) = 90
scores(1) = 85
scores(2) = 78
scores(3) = 88
scores(4) = 92
matrix(0,0) = 1
matrix(0,1) = 2
matrix(0,2) = 3
matrix(1,0) = 4
matrix(1,1) = 5
matrix(1,2) = 6
matrix(2,0) = 7
matrix(2,1) = 8
matrix(2,2) = 9
The PRINT USING command is used to format output in a structured way. It helps display
numbers, strings, and other data in a readable and organized format.
Uses of PRINT USING in QBasic:
1. Controlling Decimal Places – It allows displaying numbers with a fixed number of
decimal places.
2. Aligning Text and Numbers – Ensures proper spacing and alignment for output.
3. Adding Special Symbols – Can include currency symbols ($), percentage signs (%),
and other characters.
4. Leading Zeros and Spaces – Helps in formatting numbers with leading zeros or
fixed-width spaces.
5. Tabular Display – Useful for printing tables in a structured way.
Examples of PRINT USING
Example 1: Formatting a Number with Two Decimal Places
CLS
DIM price AS SINGLE
price = 123.456
PRINT USING "The price is $##.##"; price
END
Output:
The price is $123.46
The number is rounded to two decimal places.
ARRAYS
An array in QBasic is a collection of multiple values stored under a single variable name,
where each value is accessed using an index (subscript). Arrays are useful for storing lists,
tables, and structured data.
Why Use Arrays?
✔ Stores multiple values in one variable.
✔ Reduces repetition – avoids declaring multiple separate variables.
✔ Easier to process data using loops.
✔ Efficient memory usage for large sets of data.
Declaring an Array in QBasic
DIM arrayName(size) AS DataType
arrayName → Name of the array.
size → Number of elements (index starts from 0 by default).
DataType → Type of data stored (e.g., INTEGER, STRING, SINGLE).
Types of Arrays in QBasic
1. One-Dimensional (1D) Arrays
A simple list of values stored in a sequence.
Example: Storing 5 student scores
CLS
DIM scores(4) AS INTEGER ' Declares an array with 5 elements (0 to 4)
scores(0) = 90
scores(1) = 85
scores(2) = 78
scores(3) = 88
scores(4) = 92
PRINT "Student Scores:"
FOR i = 0 TO 4
PRINT "Score"; i + 1; ":", scores(i)
NEXT i
END
Output:
Student Scores:
Score 1: 90
Score 2: 85
Score 3: 78
Score 4: 88
Score 5: 92
Explanation:
scores(4) means the array has 5 elements (0 to 4).
A FOR loop prints each score efficiently.
matrix(0,0) = 1
matrix(0,1) = 2
matrix(0,2) = 3
matrix(1,0) = 4
matrix(1,1) = 5
matrix(1,2) = 6
matrix(2,0) = 7
matrix(2,1) = 8
matrix(2,2) = 9
FOR i = 1 TO n
INPUT "Enter number: ", numbers(i)
NEXT i
Advantages of Arrays
✔ Organized storage of multiple values.
✔ Easy access using loops.
✔ Efficient compared to multiple separate variables.
✔ Supports multi-dimensional storage (tables, grids).