0% found this document useful (0 votes)
6 views7 pages

Essential Programming Commands Guide

The document provides an overview of basic programming commands, including variable assignment, input/output operations, control structures (if-else, switch, loops), string handling, file operations, and arrays. It also discusses flowcharts, pseudocode examples, and trace tables to help understand algorithms. The content is aimed at teaching fundamental programming concepts and techniques.

Uploaded by

iheanyi197
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Essential Programming Commands Guide

The document provides an overview of basic programming commands, including variable assignment, input/output operations, control structures (if-else, switch, loops), string handling, file operations, and arrays. It also discusses flowcharts, pseudocode examples, and trace tables to help understand algorithms. The content is aimed at teaching fundamental programming concepts and techniques.

Uploaded by

iheanyi197
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Basic Commands

Annotation
//Comments are written using two slashes

Assignment
name = "Harold"
age = 49

Constants and Global Variables


constant tax = 15
global name = "Admin"

Input / Output
name = input("Enter your name")
print("Transaction Complete")

Casting
str(29)
int("102")
float(30)
bool("False")

Random Number
number = random(1,100)
Selection
Selection (if - then - else)
if firstname == "Steven" then
print("Hello" + firstname)
elif firstname == "Steve" then
print("Please use full name")
else
print("Who are you?")
end if

Selection (case select)


switch day:
case “Sat”:
print(“It is Saturday”)
case “Sun”:
print(“It is Sunday”)
default:
print(“It is a Weekday”)
endswitch
Iteration
Iteration (for loop)
for i = 1 to 10 step 1
input item
next i

Iteration (while loop)


while firstname != "Steven"
firstname = input("Try again:")
endwhile

Iteration (do while loop)


do
firstname = input("Guess name:")
until firstname == "Steven"
String Handling
Length of a String
word = "dictionary"
print([Link]) outputs 10

Substrings
word = "dinosaurs"
print([Link](2,3)) outputs nos
print([Link](3)) outputs din
print([Link](4)) outputs aurs

Concatenation
name = "Penelope"
surname = "Sunflower"
print(name + surname)

String Cases
phrase = "The Cat Sat On The Mat"
print([Link])
print([Link])

ASCII Conversion
ASC("C") returns 67
CHR(100) returns "d"
File Handling
File Handling - Reading Lines
file1 = open("[Link]")
while NOT [Link]()
print([Link]())
endwhile
[Link]()

File Handling - Writing to a (New) File


newFile("[Link]")
file2 = open("[Link]")
paint = input("Enter a paint colour:")
[Link](paint)
[Link]()
Arrays
Declare Array
array names[3]

array names = "Ella", "Sam", "Ali"

Declare 2D Array
array grid[4,5]

Assign Values
names[2] = "Samantha"
grid[1,3] = "X"
Flowcharts
A flowchart can be used to visually represent an algorithm.

It is more likely you will need to be able to interpret a flowchart rather than draw one.

The flowchart symbols are:

Algorithm Examples
Below are two different methods for representing the same algorithm - a program to
encourage people to buy items cheaply at a supermarket.

The program allows the price of items in a supermarket to be entered until the total reaches 100.
The total price and the number of items entered are tracked as the program loops. Once the total
reaches 100 or more, an if statement checks how many items have been entered and a different
message is printed if there are 20 or more items, 30 or more items or less than 20 items.

Pseudocode
//This is a program to see how many items you can buy in a supermarket
before you spend over £100}

total = 0
itemsentered = 0

while total < 100


itemprice = input("enter the price of the next item")
total = total + itemprice
itemsentered = itemsentered + 1
endwhile

if itemsentered >= 20 then


print("You are on your way to saving money.")
elif itemsentered => 30 then
print("You're a real money saver.")
else
print("Look for better deals next time.")
endif
Flowchart
Reading Algorithms
In an exam you may be asked to read an algorithm and prove your understanding, most
commonly by listing the outputs.

Start from the first line and follow the program line by line, recording the value of
variables as you go.

When you encounter a for loop, repeat the indented code as many times as stated in the
range.
Example Algorithm:
procedure NewProgram()

maxvalue = input()

for i = 1 to maxvalue
output (i * i)
???????

print("program finished")

endprocedure
Example Questions:
1. List the outputs produced by the algorithm if the 'maxvalue' input is 5.

2. State the code that has been replaced by '???????' and what the code's purpose is.
Example Answers:
1.
Outputs:
1
4
9
16
25
program finished
2.
Missing Code: next i
Purpose: Moves the loop to the next iteration.

Trace Tables
Trace tables are used to track the value of variables as a program is run.

They can be used to manually track the values in order to investigate why the program isn't
working as intended.

Each row in the trace table represents another iteration. Each column stores the value of a
variable as it changes.

See below how the trace table is updated for the simple algorithm on the left.
num1 = 2
num2 = 5

for i = 1 to 3
output (num1 + num2)
num2 = num2 - 1
next i

print("complete")
For most algorithms, not every variable will be updated in each iteration.

Values may not be entered in the order of the trace table either. For example, each iteration
outputs num1 + num2 and then decreases the value of num2 by 1.

You might also like