Fundamentals Topic 1 Programming basics
1. Objectives
Define the following key concepts:
Algorithm
Pseudocode
Understand data types and their uses.
Familiarize with basic arithmetic operations in programming.
Learn about string handling operations.
Distinguish between variables and constants.
Write pseudocode for simple problems.
2. Algorithms
Definition: An algorithm is a sequence of instructions to solve a problem.
Example Problem: Calculate the amount of paint needed to paint a room.
Questions to Consider:
What inputs are needed?
What decisions does the painter have to make?
Steps to take:
Input the dimensions of the room.
Use programming statements like Input, Calculate, Output, and arithmetic symbols ( +, -, * ).
3. Pseudocode
Definition: A method to write instructions using natural language and programming elements.
Purpose: Helps in planning code before implementation.
Provides a structure to think through the steps needed.
Streamlines the coding process.
Structure: Typically, pseudocode uses a blend of English-like syntax combined with programming syntax.
3.1. Pseudocode Statements
Assignment Example:
currentMileage = previousMileage + 1
Input/Output Examples:
OUTPUT "Enter current mileage"currentMileage = USERINPUT
3.2. Comparison with Visual Basic
Pseudocode Example:
OUTPUT "Enter a number"number = USERINPUTIF number > 4 THEN OUTPUT "Greater than 4" ELSE OUTPUT "4 or less" ENDIF
Visual Basic Example:
Dim number As Integer = 0Console.Write("Enter a number")number = Console.ReadLine()If number > 4 Then Output "Greater than 4" Else Output "4 or less" End If
4. Variables and Assignment
Identifier: A name that points to a memory location (e.g., Score).
Assignment: Assigning values to variables.
Example:
Dim Score As Integer = 0Score = Score + 1
5. Data Types
Variable Declarations in Visual Basic:
Dim wholeNumber As Integer = 5Dim height As Decimal = 1.5Dim name As String = "Bob"Dim choice As Char = "a"Dim validData As Boolean = False
Python Behavior: Variables don’t need explicit type declaration when data is assigned.
5.1. Data Type Operations
Character Assignment to Integer: Assigning a character like "9" to an integer variable may cause type mismatch issues.
Boolean Values: Acceptable values are either
TrueorFalse.
6. Variables and Constants
Variable Characteristics:
Values can be modified during execution.
Constant Characteristics:
Value cannot be changed once defined.
Example Code:**
Const VAT As Single = 17.5
6.1. Benefits of Constants
Reduces the risk of errors by limiting access to memory locations.
Example: Would you declare 3.14159265359 as a variable or a constant? (It should be a constant.)
7. Operators
Mod and Div Operators:
Mod Operator: Returns the remainder of an integer division.
Div Operator: Returns the integer part of the division.
Example Calculations:
17 mod 3yields217 div 3yields5
8. String Handling Functions
Programming languages offer built-in string functions.
Examples:
name = "Robert"x = len(name)(returns length of string)Checks presence of substring using
find()method:x = name.find("be")(returns index position or 0 if not found)
8.1. Practical Application
Evaluating return values for string functions for example strings:
Consider
name = "John"Outputs of
x = len(name)andx = name.find("be")are evaluated.
9. Worksheet Activities
Complete the Worksheet tasks as assigned in units. The tasks provide practical application of the programming concepts discussed.