Chapter 8.1 - Programming Concepts Flashcards
five basic constructs and what is included in each
» data use – variables, constants and arrays
» sequence – order of steps in a task
» selection – choosing a path through a program
» iteration – repetition of a sequence of steps in a program
» operator use – arithmetic for calculations, logical and Boolean for decisions
Define a variable
a named data store than contains a value that may change during the execution of a program
Define a constant
a named data store than contains a value that does not change during the execution of a program.
What is it mean to ‘declare’
define the value and data type of a variable or constant
How to declare an a constant integer in psuedocode
CONSTANT FirstConst <— 500
How to declare a variable thats a string in pseudocode
DECLARE SecondVar : STRING
SecondVar <— “Neel”
What does the use of data types enable
» data to be stored in an appropriate way
» data to be manipulated effectively
» automatic validation in some cases.
List the basic data types and define each one
» integer – a positive or negative whole number that can be used with mathematical operators
real – a positive or negative number with a fractional part.
char – a variable or constant that is a single character
string – a variable or constant that is several characters in length. Can also be an empty string
Boolean - variable or constant that can have only two values TRUE or FALSE.
What does the ‘sequence’ of a program refer to
the order in which the steps in a program are executed
What does ‘selection’ of a program refer to
allowing the selection of different paths through the steps of a program
2 types of selection statements
IF/ELSE statements
Case Statements
Syntax for IF statements for pseudocode
if variable number is greater than 17 print 50 otherwise print 51
IF number > 17
THEN
OUTPUT ‘50’
ELSE
OUTPUT ‘51’
ENDIF
When are case statements used
used when there are multiple choices to be made.
syntax in psuedocode for caseof statements for addition and subtraction
CASE OF OpValue
“+” : Answer <— n1+ n2
“-“ : Answer <— n1 - n2
OTHERWISE OUTPUT “Please enter a valid choice”
ENDCASE
What does ‘iteration’ enable a program to do
allows a section of programming code to be repeated under certain conditions
3 types of loops
how many iterations does each have
» Count-controlled loops (for a set number of iterations)
» Pre-condition loops – may have no iterations
» Post-condition loops – always has at least one iteration.
What is the count-controlled loop
pseudo code syntax
For loop
pseudocode - FOR …. TO …. NEXT
What function is used with for loops in python
what does each parameter stand for
is the last value included or excluded
range (1, 10, 1)
first 1 = starting value
second 10 = end value (EXCLUDED)
last 1 = step value
What is the pre-condition loop
pseudo syntax
While loops
WHILE ____ DO
ENDWHILE
What is the post condition loop
pseudocode
repeat until loop
REPEAT
____
UNTIL ___
When is a condition controlled loop used vs when is a count controlled loop used
Count controlled - number of iterations is known
condition controlled - number of iterations is unknown
What is ‘totalling’
keeping a total that values are added to
What is ‘counting’
keeping track of the number of times an action is performed
eg number of times a loop is executed
4 string functions and purpose
Length - finding the number of characters in the string
Substring - extracting part of a string
upper - converting all the letters in a string to uppercase.
lower - converting all the letters in a string to lowercase.
How to use a length string function in
python
pseudocode
python -
len(“Computer Science”)
psueodo-
LENGTH(“Computer Science”)
How to use a substring string function in
python
psuedo
python -
“Computer Science”[9:16]
or
MyString[9:16]
Pseudocode-
SUBSTRING(“Computer Science”, 10, 7)
or
SUBSTRING(MyString, 10, 7)
First index of strings in
python
Pseudocode
python - 0
Pseudocode- 1
How to use an upper string function in
python
psuedo
python -
“Computer Science”.upper()
Pseudocode -
UCASE(“Computer Science”)
How to use a lower string function in
python
psuedo
python -
“Computer Science”.lower()
Pseudocode -
LCASE(“Computer Science”)
3 diff types of operators
Arithmetic
Logical
Boolean
What is an arithmetic operator used for
an operator that is used to perform calculations
how to use exponent in python
3 ** 2
3 squared
What is a logical operator used for
an operator that is used to decide the path to take through a program if the expression formed is true or false
Not equal statement in
python
psuedo
python - !=
pseudocode - <>
what is a boolean operator used for
an operator that is used with logical operators to form more complex expressions
3 types of boolean operators in python
pseudo
and, or, not
AND, OR, NOT
what does it mean to ‘nest’ code
the inclusion of one type of code construct inside another
Define a procedure
set of programming statements grouped together under single name that can be called to perform a task at any point in a program.
Define a function
set of programming statements grouped together under a single name that can be called to perform a task at any point in a program and will return a value back to the main program.
Definition of a parameter
variables that store the values of the arguments passed to a procedure or function
How to define a procedure in pseudocode
how to execute
procedure to output a number of stars based on input from user
PROCEDURE Stars (N : INTEGER)
DECLARE C : INTEGER
FOR C <— 1 TO N
OUTPUT”*”
NEXT C
ENDPROCEDURE
CALL Stars (7)
What is a procedure known as in python
how to make one
Void function
define a function but with no return statement
How to print without line break in python
print(“*”, end = “”)
pseudocode syntax for functions
FUNCTION Celsius (Temperature : REAL) RETURNS REAL
RETURN (Temperature – 32) / 1.8
ENDFUNCTION
How to call a function in pseudocode
Must be assigned to a variable since it always returns a value
eg
MyTemp ← Celsius(MyTemp)
How to write a non-void function (a fruitful function) in python
Just use a return statement
3 components of defining a function in pseudo
» the name of the procedure or function
» any parameters passed to the procedure or function, and their data type
» the data type of the return value for a function.
Define a global variable
define a local variable
global variable - can be used by any part of a program. its scope covers the whole program.
local variable - can only be used by the part of the program it has been
declared in. Its scope is restricted to that part of the program.
4 library routines and their purpose
» MOD – returns remainder of a division
» DIV – returns the quotient (i.e. the whole number part) of a division
» ROUND – returns a value rounded to a given number of decimal places
» RANDOM – returns a random number
How to use each library routine in pseudocode
Value1 ← MOD(10,3)
Value2 ← DIV(10,3)
Value3 ← ROUND(6.97354, 2) returns the value rounded to 2 decimal places
Value4 ← RANDOM()
returns a random number between 0 and 1 inclusive
How to use library routines in python
% - mod operator
// - floor division
round(6.97354, 2) {rounds to 2 dp}
import random
random.randint(3, 9)
What is divmod in python
divmod(x,y) provides both answers where the first answer is DIV and the second answer is MOD
How to comment in
python
pseudo
#
psuedo - //