Programming Fundamentals
This section explores programming fundamentals for computer science, topics covered include: variables and constants, the three basic programming constructs, count-controlled iteration, condition-controlled iteration, nesting, operators and applying computer aided mathematics.
Variables and Constants
Variables and constants are used to store data values in a program. They are essential for creating flexible and reusable code.
Variables
A variable is a named storage location in memory where data can be held and manipulated.
Declaration and Assignment:
- Declaration: Specifying the name and type of the variable.
- Assignment: Giving the variable a value.
Example:
python
age = 16 # Declaration and assignment in Python
Properties of Variables:
- They can change value during program execution.
- Their names should be descriptive for clarity.
Constants
A constant is similar to a variable, but its value cannot change once assigned.
Advantages of Constants:
- They improve code readability by clearly indicating values that remain unchanged.
- They reduce the chance of errors caused by accidental changes.
Example:
Python
PI = 3.14159 # PI is a constant in this example
Global and Local Variables
Global Variables:
- Declared outside of functions and accessible throughout the program.
- Disadvantage: May lead to unexpected behaviour if modified in different parts of the program.
Local Variables:
- Declared within a function or block and accessible only there.
- Advantage: Reduces interference with other parts of the program.
The Three Basic Programming Constructs
All programs are built using three fundamental constructs: sequence, selection, and iteration.
Sequence
- The simplest construct.
- Instructions are executed in order, one after the other.
Example:
python
print("Hello")
print("World")
Selection
- Allows decisions to be made using conditions (if statements).
- The program follows different paths depending on whether the condition is true or false.
Example:
Python
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Iteration
- Involves repeating a block of code.
- Two main types of iteration: count-controlled and condition-controlled.
Count-Controlled Iteration
Count-controlled loops repeat a set number of times, typically using a for loop.
Example:
python
for i in range(5): # Repeats 5 times
print(i)
Condition-Controlled Iteration
Condition-controlled loops repeat until a condition is met. These can be implemented using while or repeat loops.
While Condition-Controlled Loops
Repeats as long as the condition is true.
Example:
python
counter = 0
while counter < 5:
print(counter)
counter += 1
Repeat Condition-Controlled Loops
Repeats at least once and then checks the condition (common in some languages but not in Python).
Pseudocode Example:
java
REPEAT
counter = counter + 1
UNTIL counter >= 5
Infinite Loops
- A loop that never ends because the exit condition is never met.
- Usually an error but sometimes intentional, such as in servers.
Nesting
Nesting involves placing one construct inside another.
Nested Selection
Using an if statement inside another if statement.
Example:
python
if age > 12:
if age < 20:
print("You are a teenager.")
Nested Iteration
- A loop inside another loop.
- Common in tasks like matrix processing.
Example:
python
for i in range(3):
for j in range(3):
print(i, j)
Operators
Operators are symbols or keywords used to perform operations on data.
Mathematical Operators
- + (Addition)
- - (Subtraction)
- * (Multiplication)
- / (Division)
- // (Integer division)
- % (Modulus - remainder of division)
- ** (Exponentiation)
Comparison Operators
- == (Equal to)
- != (Not equal to)
- < (Less than)
- > (Greater than)
- <= (Less than or equal to)
- >= (Greater than or equal to)
Logical Operators
- AND (True if both conditions are true)
- OR (True if at least one condition is true)
- NOT (Reverses the truth value)
Applying Computer-Related Mathematics
Modulus
Returns the remainder of a division operation.
Example:
python
10 % 3 # Result: 1
Integer Division
Returns the whole number part of a division.
Example:
python
10 // 3 # Result: 3
Exponentiation
Raises a number to a power.
Example:
python
2 ** 3 # Result: 8
Use of Brackets
- Brackets determine the order of operations.
- Operations within brackets are evaluated first.
Example:
python
result = (3 + 2) * 4 # Result: 20
Summary of Key Concepts
Variables and Constants: Store data values with variables being changeable and constants fixed.
Programming Constructs: Sequence, selection, and iteration are the building blocks of programming.
Operators: Include mathematical, comparison, and logical operators for calculations and decision-making.
Mathematics in Programming: Concepts like modulus, integer division, and exponentiation are critical for many algorithms.
These concepts provide the foundation for creating effective and efficient programs.