1/63
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Computer program basics
A computer program consists of instructions that execute one at a time.
Input
A program gets data, perhaps from a file, keyboard, touchscreen, network, etc.
Process
A program performs computations on that data, such as adding two values like x + y.
Output
A program puts that data somewhere, such as to a file, screen, network, etc.
Flowchart
Is a graphical language for creating or viewing computer programs.
Program
Is a list of statements, each statement carrying out some action and executing one at a time.
Interpreter
Runs a program's statements. Run and execute are words for carrying out a program's statements.
String literal
Consists of text (characters) within double quotes, as in "Go #57!".
Character
Includes any letter (a-z, A-Z), digit (0-9), or symbol (~, !, @, etc.).
Cursor
Indicates where the next output item will be placed in the output. The system automatically moves the cursor after the previously-output item.
Newline
Is a special two-character sequence \n whose appearance in an output string literal causes the cursor to move to the next output line. The newline exists invisibly in the output.
Comment
Is text a programmer adds to a program, to be read by humans to better understand the code, but ignored by the program when executing.
Whitespace
Refers to blank spaces (space and tab characters) between items within a statement, and to newlines. Whitespace helps improve readability for humans, but for execution purposes much whitespace is ignored.
Embedded computers
Is a computer inside another electrical device, like inside a TV, car, printer, thermostat, satellite, etc. Thus, beyond business and personal computing devices like PCs, tablets, and smartphones, computers exist invisibly in nearly anything electrical today too.
Pseudocode
Is text that resembles a program in a real programming language but is simplified to aid human understanding. "Code" is a term for a program written in text. "Pseudo" in this context means "similar to". Commonly, each pseudocode text line corresponds to a program statement.
Coral
A simple language for learning to program. Coral has two versions: flowchart and code. Coral code looks like pseudocode, but follows rules that allow execution.
Variable
Is a named item, such as x or numPeople, used to hold a value.
Assignment statement
Assigns a variable with a value, such as x = 5. That statement means x is assigned with 5, and x keeps that value during subsequent statements, until x is assigned again.
= is not equals
In programming, = is an assignment of a left-side variable with a right-side value. = is NOT equality as in mathematics. Thus, x = 5 is read as "x is assigned with 5", and not as "x equals 5". When one sees x = 5, one might think of a box labeled x having the value 5 put in.
Variable declaration
Declares a new variable, specifying the variable's name and type. When a statement that assigns a variable with a value executes, the program puts the value into the variable. The variable will hold that value until the variable is assigned with another value.
Expressions
May be a number like 80, a variable name like numApples, or a simple calculation like numApples + 1. Simple calculations can involve standard math operators like +, -, and , and parentheses as in 2 (numApples - 1).
Identifier
An identifier must:
be a sequence of letters (a-z, A-Z), underscores (_), and digits (0-9)
start with a letter
Identifiers are case sensitive, meaning upper and lower case letters differ. So numCats and NumCats are different.
Reserved word
Is a word that is part of the language, like integer, Get, or Put. A programmer cannot use a reserved word as an identifier. A list of reserved words appears at the end of this section.
Naming conventions
A set of style guidelines defined by a company, team, teacher, etc., for naming variables.
Two common conventions for distinguishing words in an identifier are:
Camel case: Lower camel case abuts multiple words, capitalizing each word except the first, as in numApples or peopleOnBus.
Underscore separated: Words are lowercase and separated by an underscore, as in num_apples or people_on_bus.
Literal
Is a specific value in code, like 2.
Operator
Is a symbol that performs a built-in calculation, like the operator + which performs addition. Common programming operators are shown below.
Evaluates
To a value, which replaces the expression. Ex: If x is 5, then x + 1 evaluates to 6, and y = x + 1 assigns y with 6.
Precedence rules
Convention | Description | Explanation |
---|---|---|
( ) | Items within parentheses are evaluated first. | In 2 * (x + 1), the x + 1 is evaluated first, with the result then multiplied by 2. |
unary - | - used for negation (unary minus) is next. | In 2 * -x, the -x is computed first, with the result then multiplied by 2. |
* / | Next to be evaluated are * and /, with each having equal precedence. | |
+ - | Finally, the + and - operators have equal precedence. | In y = 3 + 2 x, the 2 x is evaluated first, with the result then added to 3, because has higher precedence than +. Spacing doesn't matter: y = 3+2 x would still evaluate 2 * x first. |
left to right | If more than one operator of equal precedence is evaluated, evaluation occurs left to right. | In y = x 2 / 3, the x 2 is first evaluated, with the result divided by 3. |
Unary minus
An exception is minus used as negative, as in: xCoord = -yCoord. Minus (-) used as negative is known as.
Incremental development
Is the process of writing, compiling, and testing a small amount of code, then writing, compiling, and testing a small amount more (an incremental amount), and so on.
Floating-point number
Is a real number, like 98.6, 0.0001, or -666.667. The term "floating-point" refers to the decimal point being able to appear anywhere ("float") in the number. A variable declared as type float stores a floating-point number.
Floating-point literal
Is a number with a fractional part, even if that fraction is 0, as in 1.0, 0.0, or 99.573. Good practice is to always have a digit before the decimal point, as in 0.5, since .5 might mistakenly be viewed as 5.
Floating-point dividing by zero
Many programming languages produce an error when performing floating-point division by 0, but Coral does not. Coral handles this operation by producing infinity or -infinity, depending on the signs of the operands. Printing a floating-point variable that holds infinity or -infinity outputs Infinity
or -Infinity
.
RandomNumber()
Function is a built-in Coral function that takes two arguments, lowValue and highValue, and returns a random integer in the range lowValue to highValue. Ex: RandomNumber(1, 10) returns a random integer in the range 1 to 10.
Divide-by-zero error
For integer division, the second operand of / or % must never be 0, because division by 0 is mathematically undefined. A error occurs at runtime if a divisor is 0, causing a program to terminate.
Type conversion
Is a conversion of one data type to another, such as an integer to a float.
Implicit conversion
Coral automatically performs several common conversions between integer and float types, and such automatic conversion is known as.
Integer-to-float conversion
Is straightforward: 25 becomes 25.0.
float-to-integer conversion
Just drops the fraction: 4.9 becomes 4.
Type cast
Converts a value of one type to another type. A programmer can type cast an integer to float by multiplying the integer by the float literal 1.0. Ex: If myIntVar is 7, then myIntVar * 1.0
converts integer 7 to float 7.0.
Modulo operator (%)
Evaluates to the remainder of the division of two integer operands. Ex: 23 % 10 is 3. 70 % 7 is 0. Reason: 70 / 7 is 10 with remainder 0.
Common data types
Data type | Description |
---|---|
Integer | An integer is a whole number value, like 1, 999, 0, or -25. |
Float | A float is a floating-point number, which is a real number, like 98.6, 0.0001, or -666.667. |
Character | A character is a single letter, like 'A', 'p', or '%'. |
String | A string is a sequence of characters, like "Hello" or "The forecast for today is sunny with highs of 75F.". |
Boolean | A Boolean refers to a quantity that has only two possible values, true or false. |
Array | An array is an ordered list of items of a given data type, like an array of integers or an array of floats. |
Constant
Is a named value item that holds a value that cannot change. Constants are commonly used in programs to hold the value of mathematical or physical constants, such as pi, the speed of light, or kilograms per pound. Constants can also be used for any value that should not change during the program's execution.
Constant
Is a named value item that holds a value that cannot change. Constants are commonly used in programs to hold the value of mathematical or physical constants, such as pi, the speed of light, or kilograms per pound. Constants can also be used for any value that should not change during the program's execution.
Branches
In a program, a branch is a sequence of statements only executed under a certain condition. In Coral flowcharts, a decision creates two branches: If the decision's expression is true, the first branch executes, else the second branch executes. Afterwards, the branches rejoin. Coral flowcharts use a diamond symbol for a decision.
If-else branches
A decision and its two branches are often called if-else branches, because IF the decision's expression is true then the first branch executes, ELSE the second branch executes.
if-elseif branches
A branch may itself contain a decision and branches. Commonly, a series of decisions appear cascaded in each decision's false branch, known as if-elseif branches, to detect specific values of a variable. The example below detects values of 1, 25, or 50 for variable numYears. if-elseif branches are typically drawn with the decision boxes stacked vertically.
Equality operator
== evaluates to true if the left and right sides are equal. Ex: If numYears is 10, then numYears == 10
evaluates to true. Note the equality operator is ==, not =.
Nested if-else branches
If-else branches have two branches. A branch's statements can include any valid statements, including another if-else branch, known as nested branches. The nested branches can take on various forms, and the if-else branches may even use different variables.
Multiple if branches
If-else branches have two branches. A branch's statements can include any valid statements, including another if-else branch, known as nested branches. The nested branches can take on various forms, and the if-else branches may even use different variables.
Equality operator IE
Equality operators | Description | Example (assume x is 3) |
---|---|---|
== | a == b means a is equal to b | x 3 is true |
!= | a != b means a is not equal to b | x != 3 is false |
Relational operator
Relational operators | Description | Example (assume x is 3) |
---|---|---|
< | a < b means a is less-than b | x < 4 is true |
> | a > b means a is greater-than b | x > 2 is true |
<= | a <= b means a is less-than-or-equal to b | x <= 4 is true |
>= | a >= b means a is greater-than-or-equal to b | x >= 2 is true |
Logical operator
Treats operands as being true or false, and evaluates to true or false. Logical operators include and, or, not.
Logical operators IE
Logical operator | Description |
---|---|
(a) and (b) | Logical and: true when both operands are true |
(a) or (b) | Logical or: true when at least one of the two operands is true |
not(a) | Logical not: true when the one operand is false, and vice-versa |
Precedence rules
Operator/Convention | Description | Explanation |
---|---|---|
( ) | Items within parentheses are evaluated first | In |
not | Logical not is next |
|
* / % + - | Arithmetic operators (using their precedence rules; see earlier section) |
|
< <= > >= | Relational operators |
|
== != | Equality and inequality operators |
|
and | Logical and |
|
or | Logical OR | or has the lowest precedence of the listed arithmetic, logical, and relational operators. |
If statements
Is an if decision expression followed by sub-statements, with no else part.