Docsity

Basic instruction types are Programs use variables to refer to data, basic computer progr, Exams of Computer Science

Toggle sidebar
bg1
1.
Basic instruction types are:
Input: A program gets
data, perhaps from a
file, keyboard, touch-
screen, network, etc.
Process: A program per-
forms 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.
2.
Programs use variables to refer to data,
like
x,
y,
and
z
below.
The
name is due to a variable's
value "varying" as a pro-
gram assigns a variable
like x with new values.
3.
A
basic computer program.
1) A basic computer pro-
gram's instructions get in-
put, process, and put out-
put. This program first as-
signs x with what is typed
on the keyboard input, in
this case 2.
2)
The
program's
next
in-
struction
gets
the
next
in-
put, in this case 5.
3)
The program then does
some processing, in this
case assigning z with x + y
Instant Pdf Download
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Basic instruction types are Programs use variables to refer to data, basic computer progr and more Exams Computer Science in PDF only on Docsity!

  1. Basic instruction types are: Input: A program gets data, perhaps from a file, keyboard, touch- screen, network, etc. Process: A program per- forms 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.
  2. Programs use variables to refer to data, like x, y, and z below. The name is due to a variable's value "varying" as a pro- gram assigns a variable like x with new values.
  3. A basic computer program. 1) A basic computer pro- gram's instructions get in- put, process, and put out- put. This program first as- signs x with what is typed on the keyboard input, in this case 2.
    1. The program's next in- struction gets the next in- put, in this case 5.
    2. The program then does some processing, in this case assigning z with x + y

(so 2 + 5 yields 7).

  1. Finally, the program puts z (7) to output, in this case to a screen
  1. algorithm. A sequence of instructions that solves a problem
  2. A flowchart is a graphical language for creating or viewing com- puter programs. Numer- ous flowchart languages exist.
  3. A program is a list of statements, each statement carrying out some action and exe- cuting one at a time. OR A sequence of statements
  4. For Coral, an interpreter runs a program's state- ments.
  5. Run and execute are words for carrying out a program's statements
  6. Statement A specific action in a pro- gram.
  1. Interpreter A tool that runs a pro- gram's statements.
  2. Programming terms.
  3. Getting input 1) A parallelogram repre- sents an input (or output) statement.
    1. The statement assigns the variable on the left (wage) with the next val- ue in the program's input (20).
    2. This program uses that gotten value in a subse- quent calculation.
  4. Basic input
  5. Basic input example: Dog years to human years.
  6. In a Coral flowchart, a parallelogram repre- sents an output statement,

written as: Put item to out- put.

  1. string literal consists of text (char- acters) within double quotes, as in "Go #57!".
  2. character includes any letter (a-z, A-Z), digit (0-9), or symbol (~, !, @, etc.).
  3. cursor indicates where the next output item will be placed in the output. The sys- tem automatically moves the cursor after the previ- ously-output item.
  4. newline is a special two-charac- ter 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 out- put.
  5. Outputting string literals. 1) This program puts "Keep calm" at the cur- rent cursor location in out- put, which initially is the beginning of the output device. The cursor auto-
  1. Valid and invalid comments

tent to another human reading the program.

  1. During execution, the comments are entirely ig- nored.
  1. Whitespace refers to blank spaces (space and tab charac- ters) between items within a statement, and to new- lines. Whitespace helps improve readability for humans, but for execution purposes much white- space is ignored.
  2. Computing history
  3. An embedded computer is a computer inside another device. Typically, these devices have a ded-
  1. The number of embedded computers are greater than the number of desktops and laptop computers.

icated function such as a cottee maker or washing machine

In 2012, there were 350 million desktops and lap- tops, compared to 17 bil- lion embedded comput- ers shipped.

  1. Bit and Byte A single 0 or 1 is called a bit. 1011 is four bits. Eight bits, like 11000101, are called a byte.
  2. ASCII bit codes for common characters (ignore Dec for now).
  1. Characters and numbers are encoded and stored in the computer's memory
  1. Each character has a unique bit code.
  2. Words are stored as a sequence of bit codes in the computer's memory.
  1. Computational thinking.
  2. Programming is all about precision Programming is all about precision. Programs must be created precisely to run correctly. Ex:

"If-else" statements. When used in English, the result is logical, unam- biguous info. Some call this "computational think- ing".

  1. = and == have ditterent meanings.
  2. Using i where j was meant can yield a hard-to-find bug.
  3. Declaring a variable as int when char was needed can cause confusing er- rors.
  4. Not considering that n could be 0 in sum/n can cause a program to fail entirely in rare but not in- significant cases.
  5. The ditterence between typing x/2 vs. x/2.0 can have huge impacts.
  6. Counting from i be- ing 0 to i < 10 vs. i <=

10 can mean the ditter- ence between correct out- put and a program out- putting garbage.

  1. Pseudocode is text that resembles a program in a real pro- gramming language but is simplified to aid human understanding. "Code" is a term for a program writ- ten in text. "Pseudo" in this context means "sim- ilar to". Commonly, each pseudocode text line cor- responds to a program statement.
  2. Pseudocode compactly represents a program's state- ments.
  1. Pseudocode describes a program in text form, with one statement per line of text. The text may be easier to create than graphics, and more com- pact.
  2. The pseudocode is con- sidered to execute one statement at a time.

means "the item on the left always equals the item on the right". So for x + y = 5 and x * y = 6, one can determine x = 2 and y = 3 or vice versa.

  1. Assignment statements look similar to algebra equations but have VERY ditterent meaning. The left side MUST be one vari- able.
  2. The = isn't "equals", but is an action that PUTS a value into the variable. As- signment statements only make sense when execut- ed in sequence.
  1. = 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 as- signed 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.
  1. Select the correct value for x, y, and z after the follow- Always the latest value the ing statements execute
  2. A variable may appear on the left and right of an assignment statement
  3. Variable on both sides. Indicate the value of x after the statements execute

letter falls in

  1. A variable may appear on both sides of an as- signment statement. After x = 1, then x = x * 20 assigns x with 1 * 20 or 20, overwriting x's previ- ous value 1.
  2. Another x = x * 20 as- signs x with 20 * 20 or 400, which overwrites x's previ- ous value 20.
  3. Only the latest value is held in x. The previous val- ues are shown greyed out above but in actuality are completely gone.
  1. variable declaration declares a new variable, specifying the variable's

numApples = 8 assigns numApples with the value of the right-side expres- sion (in this case 8).

  1. 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).
  2. Assigning a variable. In the code below, lit- terSize is assigned with 3, and yearlyLitters is as- signed with 5. Later, an- nualMice is assigned with the value of litterSize * yearlyLitters (3 * 5, or 15), which is then printed. Next, litterSize is assigned with 14, yearlyLitters is as- signed with 10, and an- nualMice is assigned with their product (14 * 10, or 140), which is printed.
  1. Assignment statement with same variable on both sides
  2. Variable assignments overwrite a variable's previous values: People-known example

Commonly, a variable ap- pears on both the right and left side of the = op- erator. Ex: If numItems is 5, after numItems = nu- mItems + 1 executes, nu- mItems will be 6. The statement reads the val- ue of numItems (5), adds 1, and assigns numItems with the result of 6, which replaces the value previ- ously held in numItems

  1. The program de- clares two integer vari- ables yourFriends and to- talFriends.
  2. The program gets input and assigns yourFriends with 200.
  3. The assign- ment statement reads yourFriends (200), mul- tiplies yourFriends by yourFriends, and as- signs totalFriends with the

So numCats and NumCats are ditterent.

  1. A reserved word (or keyword) is a word that is part of the language, like integer, Get, or Put. A program- mer cannot use a reserved word as an identifier.
  2. naming conventions: A set of style guidelines defined by a company, team, teacher, etc., for naming variables.
  3. Two common conventions for distinguishing words in *Camel case: Lower camel an identifier are: case abuts multiple words, capitalizing each word ex- cept the first, as in numAp- ples or peopleOnBus. *Underscore separated: Words are lowercase and separated by an under- score, as in num_apples or people_on_bus.
  4. Good practice is to create meaningful identifier names example that self-describe an item's purpose. Good practice minimizes use of abbreviations in identifiers except for well-known ones like num in numPassengers. Pro- grammers must strive to find a balance. Abbrevia- tions make programs harder to read and can lead to confusion. Long variable names, such as averageAge- OfUclaGraduateStudent may be meaningful, but can

make subsequent statements too long and thus hard to read.

  1. Reserved words and function names in Coral.
  2. literal is a specific value in code, like 2
  3. operator is a symbol that performs a built-in calculation, like the operator + which per- forms addition
  4. Common programming operators are shown below.
  5. Indicate which are valid expressions. x and y are vari- ables.
  6. Does the expression correctly capture the intended behavior?
  7. Precedence rules for arithmetic operators.