Chapter 22 Programming Constructs Flashcards
What is a variable and provide an example?
A variable is a named location in computer memory used to hold data when a program is running: the value of a variable can change while the program is running.
When declaring a variable it must be given a name and a data type.
Example:
Python: number = 0
C#: int number
What is a constant and give an example?
A constant is the same as a variable except for 1 difference: the value of a constant remains the same when a program is running.
Example:
Python: there is no constant keyword
C#: Const float rateOfPay = 11.55
What is a Boolean Operator and give an example?
A Boolean Operator is an operator that allows conditions to be combined and then evaluated: the outcome is a Boolean variable.
Example:
Python: AND OR
C#: the AND operator is denoted by &&. the OR operator is denoted by ||
What is an Arithmetic Operator and give an example?
An arithmetic operator enables arithmetic operations to be carried out on variables: arithmetic operators include +, -,*,/.
Example:
b*c
What is an Input statement and give an example?
An input statement is a statement used to capture data which is to be used in the program.
Example:
Python: player1name=Input(“Please enter player 1’s name.”)
C#: player1name=Console.Readline()
What is an output statement and give an example?
An output statement is a statement used to output data and information from the program.
Example:
Python: print
C#: Console.Write
What is an assignment statement and give an example?
An assignment statement is a statement that assigns a value to a variable, constant, or other data structure: the value on the right-hand side of an assignment statement can contain a calculation.
Example:
a=b*c
What is an assignment statement and give an example?
An assignment statement is a statement that assigns a value to a variable, constant, or other data structure: the value on the right-hand side of an assignment statement can contain a calculation.
Example:
a=b*c
What is an array?
An array is a data structure that holds a set of data items of the same data type. The array is assigned a name, a size (representing the number of data items to be stored) and a data type by the programmer.
Key facts about arrays:
The computer reserves a set of memory locations, one for each element of the array. The memory locations are next to each other or ‘contiguous’.
In many programming languages (like python), arrays are zero-indexed. This means that the first element of an array is 0.
Examples:
** cars[0] cars[1] cars[2] cars[3] cars[4]
Pos in
array 0 1 2 3 4
(Index)
Value 35 250 69 22 204
Elements can be assigned values, for example cars[0]= 35
Creating an array in python:
cars= [35, 250, 69, 22, 204]
How do you code bubble sort?
Python (not indented correctly because of the limited space per line):
cars = [35, 250, 69, 22, 204]
for index in range (1,len(cars)):
currentvalue = cars[index]
position = index
while position>0 and cars[position-1]>current value:
cars[position] = cars[position-1]
position = position-1
cars[position]=current value
print (“After pass “ + str(index)+ “ “),
print (cars)
print(“The sorted list is: “),
print(cars)