Introduction to
Programming
Programming is the process of writing instructions for a computer to
execute. It's used to solve problems efficiently using logic and structured
steps. Programming languages, like Python, C++, and Java, allow us to
communicate with computers.
by
MystarUni
Output Statements
Basic Syntax Data Types Formatting
1 2 3
Use print() function to display text Can print strings, numbers, Control spacing, decimal places,
and variable values on screen. variables, and complex and alignment when displaying
expressions. output.
25 Example
4
print"Hello," # Hello
print "Age" # Àge
Variable Declaration
What is a Variable? Declaration Syntax Variable Naming Data Types
A variable is a storage
Rules Integer: Whole numbers
DECLARE
location in memory that Variable names must start (positive, negative, zero)
variableName AS
holds data. Variables must with a letter or underscore,
DataType Float/Double: Decimal
be declared before use. contain only letters,
numbers, and underscores, numbers with fractional
be case-sensitive, and have parts
String: Text data, enclosed
meaningful, descriptive
in quotes
names. Avoid using
Boolean: True or False
reserved keywords or
values
spaces.
Character: Single letter or
symbol
Input Statements
Input statements allow users to enter values during program execution. They provide a way for programs to interact with the user
and receive data.
INPUT variableName
Decision Structures
Simple IF Statement IF-ELSE Statement
1 2
IF condition THEN IF condition THEN
statement ENDIF statement1 ELSE
statement2 ENDIF
IF-ELSE IF Statement
3
IF condition1 THEN statement1 ELSE IF
condition2 THEN statement2 ELSE statement3 ENDIF
Control Structures (Loops)
Loops allow repeating a set of statements multiple times. They are essential
for automating repetitive tasks and processing large amounts of data.
FOR Loop
1
FOR counter FROM start TO end STEP increment
statement ENDFOR
WHILE Loop
2
WHILE condition statement ENDWHILE
DO-WHILE Loop
The DO-WHILE loop ensures that the statement inside the loop is executed
at least once, even if the condition is initially false. This is useful for
scenarios where you need to perform an action before checking a condition.
DO statement WHILE condition
Example Problems
Let's explore some common programming problems to understand how loops
and decision structures are used in practice.
Problem 1 Average of Problem 2 Factorial of
1 2
N Numbers a Number
This problem involves This problem involves
calculating the average of a calculating the factorial of a
set of numbers provided by given number, which is the
the user. product of all positive
integers less than or equal to
that number.
Problem 3 Largest Number in a Set
3
This problem involves finding the largest number in a set of numbers
provided by the user.
Problem 1 Average of N Numbers
This problem involves calculating the average of a set of numbers provided by the user.
DECLARE n, sum, num, i AS INTEGER
DECLARE average AS REAL
PRINT "Enter the number of values: "
INPUT n
SET sum = 0
FOR i FROM 1 TO n STEP 1
PRINT "Enter number ", i, ": "
INPUT num
sum = sum + num
ENDFOR
SET average = sum / n
PRINT "Average = ", average
Problem 2 Factorial of a
Number
This problem involves calculating the factorial of a given number, which is the
product of all positive integers less than or equal to that number.
DECLARE n, factorial, i AS INTEGER
PRINT "Enter a number: "
INPUT n
SET factorial = 1
FOR i FROM n TO 1 STEP -1
factorial = factorial * i
ENDFOR
PRINT "Factorial of ", n, " is ", factorial
Problem 3 Largest Number in a Set
This problem involves finding the largest number in a set of numbers provided by the user.
DECLARE n, num, largest, i AS INTEGER
PRINT "Enter the number of values: "
INPUT n
PRINT "Enter number 1: "
INPUT largest
FOR i FROM 2 TO n STEP 1
PRINT "Enter number ", i, ": "
INPUT num
IF num > largest THEN
largest = num
ENDIF
ENDFOR