Big Idea 3: Algorithms and Programming Flashcards
How can a variable be used to represent a value, and what are the implications of using variables in programming? In the answer, explain the definition of variable.
A variable is an abstraction inside a program that can hold a value. Each variable has associated data storage that represents one value at a time, but that value can be a list or other collection that in turn contains multiple values.
Using meaningful variable names helps with the readability of program code and understanding of what values are represented by the variables.
Some programming languages provide types to represent data, which are referenced using variables. These types include numbers, Booleans, lists, and strings.
Some values are better suited to representation using one type of datum rather than another.
How is the value of a variable determined as a result of an assignment in programming? In the answer, explain the definition of assignment operator.
The assignment operator allows a program to change the value represented by a variable. The exam reference sheet provides the “<-“ operator for assignment. For example, using “a <- expression” in text or as a block evaluates the expression and then assigns a copy of the result to the variable a.
The value stored in a variable will be the most recent value assigned. For example:
a <- 1
b <- a
a <- 2
display(b) still displays 1.
How can a variable be used to represent a list or string, and what are the specific characteristics associated with these data types? In the answer, explain the definition of list and string.
A list is an ordered sequence of elements. For example, “[value1, value2, value3, …]” describes a list where value1 is the first element, value2 is the second element, and value3 is the third element, and so on.
An element is an individual value in a list that is assigned a unique index.
An index is a common method for referencing the elements in a list or string using natural numbers.
A string is an ordered sequence of characters.
How can data abstraction be developed using lists to store multiple elements? In the answer, explain the definition of data abstraction.
Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation. Data abstractions can be created using lists, which allow multiple related items to be treated as a single value. Lists enable developers to develop a data abstraction that is easier to develop and maintain. Data abstractions often contain different types of elements.
The exam reference sheet provides the notation [value1, value2, value3, …] to create a list with those values as the first, second, third, and so on items. Lists can be assigned using aList <- [value1, value2, value3, …], creating a new list containing the values at indices 1, 2, 3, and so on, and assigns it to aList.
How can data abstraction be developed using lists to store multiple elements, and what are the advantages of using lists in data abstractions? In the answer, explain the definition of data abstraction.
Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation.
Data abstractions can be created using lists.
Developing a data abstraction to implement in a program can result in a program that is easier to develop and maintain.
Data abstractions often contain different types of elements.
The use of lists allows multiple related items to be treated as a single value. Lists are referred to by different names, such as array, depending on the programming language.
The exam reference sheet provides the notation [value1, value2, value3, …] to create a list with those values as the first, second, third, and so on items.
For example,
- aList <- [value1, value2, value3, …] creates a new list that contains the values value1, value2, value3, and … at indices 1, 2, 3, and … respectively and assigns it to aList.
- aList <- [ ] creates an empty list and assigns it to aList
- aList <- bList assigns a copy of the list bList to the list aList. For example, if bList contains [20, 40, 60], then aList will also contain [20, 40, 60] after the assignment.
Explain how the use of data abstraction manages complexity in program code and discuss how list operations adhere to indexing rules.
Data abstractions manage complexity in programs by giving a collection of data a name without referencing the specific details of the representation.
The use of lists allows multiple items to be treated as a single value. This abstraction simplifies interactions with data collections and enhances the maintainability and readability of the code.
The exam reference sheet describes a list structure whose index values are 1 through the number of elements in the list, inclusive. For all list operations, if a list index is less than 1 or greater than the length of the list, an error message is produced, and the program will terminate.
For example,
- aList <- [value1, value2, value3, …] creates a new list that contains the values value1, value2, value3, and … at indices 1, 2, 3, and … respectively and assigns it to aList.
- aList <- [ ] creates an empty list and assigns it to aList
- aList <- bList assigns a copy of the list bList to the list aList. For example, if bList contains [20, 40, 60], then aList will also contain [20, 40, 60] after the assignment.
How can an algorithm be expressed without using a programming language? In the answer, explain the definition of algorithm.
An algorithm is a finite set of instructions that accomplish a specific task.
Beyond visual and textual programming languages, algorithms can be expressed in a variety of ways, such as natural language, diagrams, and pseudocode.
Algorithms executed by programs are implemented using programming languages.
Every algorithm can be constructed using combinations of sequencing, selection, and iteration.
How can you represent a step-by-step algorithmic process using sequential code statements? In the answer, explain the definition of sequencing and expression.
Sequencing is the application of each step of an algorithm in the order in which the code statements are given.
A code statement is a part of program code that expresses an action to be carried out.
An expression can consist of a value, a variable, an operator, or a procedure call that returns a value.
Expressions are evaluated to produce a single value.
The evaluation of expressions follows a set order of operations defined by the programming language.
Sequential statements execute in the order they appear in the code segment.
Clarity and readability are important considerations when expressing an algorithm in a programming language.
How do you evaluate expressions that use arithmetic operators? In the answer, explain the definition of arithmetic operators and MOD operation.
Arithmetic operators are part of most programming languages and include addition, subtraction, multiplication, division, and modulus operators.
The exam reference sheet provides the arithmetic operators +, -, *, /, and MOD. These are used to perform arithmetic on a and b. For example, “a + b” adds a and b, “a - b” subtracts b from a, “a * b” multiplies a and b, “a / b” divides a by b, and “a MOD b” evaluates to the remainder when a is divided by b. Assume that a is an integer greater than or equal to 0 and b is an integer greater than 0. For example, 17 MOD 5 evaluates to 2.
The order of operations used in mathematics applies when evaluating expressions. The MOD operator has the same precedence as the * and / operators.
How can you evaluate expressions that manipulate strings, and what specific methods are involved? In the answer, explain the definition of string concatenation and substring.
String concatenation joins together two or more strings end-to-end to make a new string.
A substring is part of an existing string.
How do you write and evaluate expressions that use relational operators? In the answer, explain the definition of relational operators.
A relational operator tests the relationship between two variables, expressions, or values.
The exam reference sheet provides the following relational operators: =, ≠, >, <, ≥, and ≤.
These are used to evaluate whether a relationship holds true or false, for example, a = b evaluates to true if a and b are equal; otherwise, it evaluates to false.
How do you write and evaluate expressions that use logic operators? In the answer, explain the definition of logic operators.
Logic operators include NOT, AND, and OR, which evaluate to a Boolean value. The exam reference sheet describes their usage:
- NOT condition evaluates to true if the condition is false; otherwise, it evaluates to false.
- condition1 AND condition2 evaluates to true if both conditions are true; otherwise, it evaluates to false.
- condition1 OR condition2 evaluates to true if either condition1 is true or if both are true; otherwise, it evaluates to false.
The operand for a logical operator is either a Boolean expression or a single Boolean value.
How can you express an algorithm that uses selection without using a programming language? In the answer, explain the definition of selection.
Selection determines which parts of an algorithm are executed based on a condition being true or false.
Conditional statements, or “if-statements,” affect the sequential flow of control by executing different statements based on the value of a Boolean expression.
How do you write conditional statements? How do you determine the result of conditional statements? In the answer, explain the concept of conditional statements and their structure.
Conditional statements or “if-statements” are used to control the flow of execution based on the truth value of expressions. The basic structure is provided as:
- IF(condition) { block of statements } which executes the block of statements if the condition is true.
- IF(condition) { first block of statements } ELSE { second block of statements } which executes the first block if the condition is true, otherwise, it executes the second block.
What are nested selection conditional statements?
Nested conditional statements consist of conditional statements within conditional statements.