variables and assignments

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

21 Terms

1
New cards

assignment statement

assigns a variable with a value. Its left side must be a variable. The right side is an expression. Ex. x=5

2
New cards

variable declaration

declares a new variable, specifying the variable’s name and type. A variable of type integer can hold whole number values like 1, etc. In coral, an integer variable’s initial value is 0.

3
New cards

assignment statement

assigns the variable on the left side of the = with the current value of the right side expression. An expression 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).

4
New cards

identifier

a name created by a programmer for an item like a variable or function. It must

  • be a sequence of letters, underscores, and digits

  • start with a letter

    they are case sensitive. A reserved word/key 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.

5
New cards
6
New cards

reserved words

Reserved words

Function names

Flowchart version

Code version

Get
Put
to
next
input
output
integer
float
array
and
or
not

if
elseif
else
for
while
Function
returns
with
decimal
places
nothing

RaiseToPower
SquareRoot
AbsoluteValue
RandomNumber
SeedRandomNumbers

7
New cards

expression

combination of items, like variables, literals, operators, and parentheses, that evaluates to a value. May be a single variable or a literal. Commonly used on the right side of an assignment statement, as in y = 2*(x+1)

a literal is a specific value in code, like 2. An operator is a symbol that performs a built in calculation, like the operator + which performs addition

8
New cards

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.

9
New cards

floating point (float) variables

a real number. The term ‘floating point; refers tot eh decimal point being able to appear anywhere in teh number.

A floating point literal is a number with a fractional part, even if that fraction is 0. Good practice is to always have a digit before the decimal point even if it is 0 for readability.

10
New cards

choosing a varuable type: float v integer

A programmer should choose a variable's type based on the type of value held.

  • Integer variables are typically used for values that are counted, like 42 cars, 10 pizzas, or -95 days.

  • Floating-point variables are typically used for values that are measured, like 98.6 degrees, 0.00001 meters, or -666.667 grams.

  • Floating-point variables are also used when dealing with fractions of countable items, such as the average number of cars per household.

Note: Some programmers warn against using floating-point for money, as in 14.53 representing 14 dollars and 53 cents, because money is a countable item (reasons are discussed further in another section). Integers may be used to represent cents or to represent dollars when cents are not included, such as for an annual salary (e.g., 40000 dollars, which are countable).


11
New cards

floating point divide by zero

dividing a nonzero floating point number by zero is undefined in regular arithmetic. Many programing languages produce an error when performing floating point division by 0, but coral does not. Coral handles this operation by producing infinity or -infinity. Printing a floating-point variable that holds infinity or -infinity outputs Infinity or -Infinity.

If the dividend and divisor in floating-point division are both 0, the division results in a "not a number". Not a number indicates an unrepresentable or undefined value. Printing a floating-point variable that is not a number outputs NotANumber.

12
New cards

function

list of statements executed by invoking the function’s name, with such invoking known as a function call. Any function input values, or arguments, appear within (0), and are separated by commas if more than one.

13
New cards

coral built in math functions

Function

Behavior

Example

SquareRoot(x)

Square root of x

SquareRoot(9.0) evaluates to 3.0.

RaiseToPower(x, y)

Raise x to power y: xy

RaiseToPower(6.0, 2.0) evaluates to 36.0.

AbsoluteValue(x)

Absolute value of x

AbsoluteValue(-99.5) evaluates to 99.5.

14
New cards

RandomNumber(0)

function that 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.

  • these numbers are known as pseudo-random because they are not actually, but have the appearance of being random.

  • . Internally, the RandomNumber() function has an equation to compute the next "random" integer from the previous one, (invisibly) keeping track of the previous one. For the first call to RandomNumber(), no previous random integer exists, so the function uses a built-in integer known as the seed. Coral automatically seeds the pseudo-random number generator with a number based on the current time. Since, the time is different for each program run, the program will get a unique sequence.

    Reproducibility is important for testing some programs. A programmer can specify the seed using the function SeedRandomNumbers(), as in SeedRandomNumbers(10) or SeedRandomNumbers(99). Note that the seeding should only be done once in a program, before the first call to RandomNumber().

15
New cards

divide by 0

For integer division, the second operand of / or % must never be 0, because division by 0 is mathematically undefined. A divide-by-zero error occurs at runtime if a divisor is 0, causing a program to terminate.

16
New cards

type conversion

a conversion of one data type to another, such as an integer to a float. Coral automatically performs several common conversions between integer and float types, and such automatic conversion is known as implicit conversion

  • For an arithmetic operator like + or *, if either operand is a float, the other is automatically converted to float, and then a floating-point operation is performed.

  • For assignments, the right side type is converted to the left side type.

integer-to-float conversion is straightforward: 25 becomes 25.0.

float-to-integer conversion just drops the fraction: 4.9 becomes 4.

17
New cards

type casting

A programmer sometimes needs to explicitly convert an item's type. Ex: If a program needs a floating-point result from dividing two integers, then at least one of the integers needs to be converted to a float so floating-point division is performed. Otherwise, integer division is performed, evaluating to only the quotient and ignoring the remainder.

A 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.

18
New cards

modulo operator

evaluates to the remainder of the division of two integer operands. Ex: 23 % 10 is 3.

Examples:

  • 9 % 5 is 4. Reason: Since 9 = 5 * 1 + 4, the integer division 9 / 5 results in 1, and the remainder is 4.

  • 70 % 7 is 0. Reason: 70 / 7 is 10 with remainder 0.

  • 1 % 2 is 1. Reason: 1 / 2 is 0 with remainder 1.

  • 10 % 4.0 is not valid. "Remainder" only makes sense for integer operands.

19
New cards

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.

20
New cards

constant

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

21
New cards

a programmer can use code to describe a flowchart’s variables and statements by:

  1. Declaring each variable's type and name, one variable declaration per line. Ex: integer userAge declares the variable named userAge of type integer.

  2. Listing each program statement in order, one statement per line.

To provide separation between different parts of a program, variable declarations and program statements are often separated by a blank line, which is considered whitespace and not part of the program structure.