Chap 8 - pseudocode Flashcards
(48 cards)
features of pseudocode
-all command words are in UPPERCASE
-DECLARE all variables at the start of the code
rules for variables
-has to start with a character
-assignee using ←
-case insensitive
Poop & poop are the same variables
declaring variables
DECLARE variable: data type
outputting
OUTPUT variable
OUTPUT “hello”, “world”
taking input
OUTPUT “question/ statement”
INPUT variable
eg.
OUTPUT “name?”
INPUT name
how to comment on code
// this is a comment
// slashes must be added for each line
data types
-integer
-boolean
-real
-string - delimited by “ “
-char- delimited by ‘ ‘
arithmetic operation for
-addition
-subtraction
-multiply
-divide
-power
-squared
-remainder - returns remainder
-integer division - returns quotient
-rounding
-give random int
- +
- -
- *
- /
- ^(1/2)
-MOD (num, divide by)
-DIV (num, divide by)
-ROUND (num, decimal place)
-RANDOM( )
logical operators for:
-greater than
-greater than or equal
-smaller than
-smaller than or equal
-equal (comparison)
-not equal (comparison)
- >
- > =
- <
- <=
- =
- <>
Boolean operators
AND
OR
NOT
assigning variables
-use ←
eg.
age ← 24
pop ←”poop”
if, else conditional statement
(else is not necessary)
IF ( )
THEN
action
ELSE
action
ENDIF
nested ifs
IF ( )
THEN
action
ELSE
IF ( )
THEN
action
ELSE
action
ENDIF
ENDIF
case statement
CASE OF variable
“__” : action
“__” : action
OTHERWISE action
ENDCASE
note: for loops in pseudocode, both values are included
eg. 0 TO 4 = 5 runs
so always start with 1
for loop
FOR i ← 1 TO x
action
NEXT i
while loop
i ← x
WHILE i <10
action
i ← i + 1
ENDWHILE
-use for validation
repeat until loop
i ← x
REPEAT
action
i ← i + 1
UNTIL i <10
-use for validation
How does a post condition loop work
-initialize loop counter
-execute action
-update counter
-check condition
-if condition met, stop
else repeat
nested loops
-2 counters for inner and outer loops
-outer loop only loops after inner loop is finished
eg.
FOR i ← 1 TO 10
action
FOR j ← 1 To 5
action
NEXT j
NEXT i
loop/if with a list as its range
n= [“A”, “B”]
-for i in n:
-while i in n:
-if variable[i] in n:
note: i changes to elements in list, list can be any data type
Alter
index values in list
-all char has an index value
-first element = 1 - positive
-space also has an index value
finding length of characters
LENGTH(“hello”)
LENGTH(poop)
finding substring of characters
SUBSTRING (“hello”, 1, 3)
SUBSTRING(poop, 1, 3)
-first parameter = string
-second parameter = position of starting character
-third parameter = length of wanted substring