Docsity

Introduction to Computer Programming: Ultimate Foundations & Logic Study Guide, Study Guides, Projects, Research of Programming Languages

Toggle sidebar
bg1
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
- Left side is always a variable
variable declaration declares a new variable:
- Integer variable can hold whole number values, like 1, 999, 0, or -25 (but not 3.5 or
0.001)
expression may be a number like 80, a variable name like numApples, or a simple
calculation like num Apples + 1
identifier is name created by a programmer for an item like a variable or function
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
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.
an identifier is a name, while a variable is a storage location
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Introduction to Computer Programming: Ultimate Foundations & Logic Study Guide and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

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

  • Left side is always a variable

variable declaration declares a new variable:

  • Integer variable can hold whole number values, like 1, 999, 0, or -25 (but not 3.5 or 0.001)

expression may be a number like 80, a variable name like numApples, or a simple calculation like num Apples + 1

identifier is name created by a programmer for an item like a variable or function 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 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.

an identifier is a name, while a variable is a storage location

Expressions: expression is a combination of items, like variables, literals, operators, and parentheses, that evaluates to a value. Ex: 2 * (x + 1) is an expression A 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

An expression 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.

Table 2.4.2: Precedence rules for arithmetic operators.

Conventi on

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

Operators: +,-,/,*,% modulo operator ( % ) evaluates to the remainder of the division of two integer operands. Ex: 23 % 10 is 3.

- Cannot do 100 % 0. It will give you “Error”

type conversion is a conversion of one data type to another, such as an integer to a float

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

type cast - programmer can type cast an integer to float by multiplying the integer by the float literal 1.

Function is a 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 ( ), and are separated by commas if more than one.

Corals 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: RaiseToPower(6.0, 2.0) evaluates to 36.0.

AbsoluteValue(x) Absolute value of x AbsoluteValue(-99.5) evaluates to 99.5.

The 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

A programmer can specify the seed using the function SeedRandomNumbers() , as in SeedRandomNumbers(10) or SeedRandomNumbers(99)

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.

A constant is a named value item that holds a value that cannot change

nested branches : branch's statements can include any valid statements, including another if-else branch:

multiple if branches , each decision is independent, and thus more than one branch can execute, in contrast to the if-elseif arrangement where only one branch can execute:

Equality operators

An equality operator checks whether two operands' values are the same (==) or different (!=).

An expression involving an equality operator evaluates to a Boolean value. A Boolean is a type that has just two values: true or false.

Equality operators Description Example (assume x is 3)

== a == b means a is equal to b x == 3 is true x == 4 is false

!= a != b means a is not equal to b x != 3 is false x != 4 is true

Relational operators

Description Example (assume x is 3)

< a < b means a is less-than b x < 4 is true x < 3 is false

> a > b means a is greater-than b x > 2 is true x > 3 is false

<= a <= b means a is less-than-or-equal to b

x <= 4 is true x <= 3 is true x <= 2 is false

>= a >= b means a is greater-than-or-equal to b

x >= 2 is true x >= 3 is true x >= 4 is false Note: However, floating-point types should not be compared using the equality operator == , due to the imprecise representation of floating-point numbers, as discussed in a later section. Ex: myFloat == 3.14 is NOT OK

Note: A common use of logical operators is to detect if a value is within a range.

Precedence rules are the order in which operators are evaluated in an expression

Precedence rules for arithmetic (+,-), logical(and, or), and relational(!=,

<) operators.

Operator/Con vention

Description Explanation

( ) Items within parentheses are evaluated first

In (a * (b + c)) - d, the + is evaluated first, then *, then -.

not Logical not is next

not(x == 1) or y is evaluated as (not(x == 1)) or y

  • / % + - Arithmetic operators (using their precedence rules; see earlier section)

z - 45 * y < 53 evaluates * first, then -, then <.

< <= > >= Relational operators

x < 2 or x >= 10 is evaluated as (x < 2) or (x >= 10) because < and >= have precedence over the or operator.

== != Equality and inequality operators

x == 0 and x >= 10 is evaluated as (x == 0) and (x >= 10) because == and >= have precedence over the and operator.

and Logical and x == 5 or y == 10 and z != 10 is evaluated as (x == 5) or ((y == 10) and (z != 10)) because the and operator has precedence over the or operator.

or Logical OR or has the lowest precedence of the listed arithmetic, logical, and relational

Rule: Floating-point numbers should not be compared using the == operator.

4.1 Loops A loop is a program construct that repeatedly executes the loop's statements (known as the loop body ) while the loop's expression is true; when false, execution proceeds past the loop. Each time through a loop's statements is called an iteration

- using a loop to examine a list of values one value at a time and updating variables along the way, is a common programming task

  • Counting specific values
  • Finding max values
  • Programmers often use loops to execute a computation until a done condition is reached. That done condition is reached when the loop expression is false.
  • sometimes a loop is executed as long as a value is greater-than another value, or less-than another value. Sometimes a loop is executed as long as a value is NOT equal to another value. An infinite loop is a loop that never stops iterating

Looping N times

A loop commonly must iterate a specific number of times, such as 10 times. A loop iterating a specific number of times commonly consists of three parts:

  1. loop variable initialization before the loop
  2. decision statement with the loop expression
  3. loop variable update at the end of the loop body.

6.1 User-defined function basics

function is a named list of statements.

● A function definition consists of the new function's name and a block of statements. The function's name can be any valid identifier. ● A function call is an invocation of a function's name, causing the function's statements to execute. ● A local variable is a variable declared in a function, which is then accessible only within that function.

parameter is a function input specified in a function definition. Ex: A pizza area function might have diameter as an input. argument is a value provided to a function's parameter during a function call. Ex: A pizza area function might be called as PrintPizzaArea(12.0) or as PrintPizzaArea(16.0)

A function may return one value by assigning a return variable with the return value

7.1 Algorithms

algorithm is a sequence of steps that solves a problem, generating correct output for any valid input values. Ensuring an algorithm's correctness is a key job for a programmer

Algorithms differ in various features. One feature is algorithm time efficiency : The number of calculations required to solve a problem. For the same problem, some algorithms may be much more time efficient than others. computational problem specifies an input, a question about the input that can be answered using a computer, and the desired output

8.1 Software design processes

systems development life cycle (SDLC)

● The analysis phase defines a program's goals. ● The design phase defines specifics of how to build a program. ● The implementation phase involves writing the program. ● The testing phase checks that the programs correctly meets the goals.

waterfall approach - program can be built by carrying out the SDLC phases in sequence agile approach - program built by doing small amounts of each SDLC phases in sequence, and then repeating

object is a grouping of data (variables) and operations that can be performed on that data (functions)

Abstraction means to have a user interact with an item at a high-level, with lower-level internal details hidden from the user (aka information hiding or encapsulation )

abstract data type ( ADT ) is a data type whose creation and update are constrained to specific well-defined operations.

Unified Modeling Language ( UML ) is a modeling language for software design that uses different types of diagrams to visualize the structure and behavior of programs

  • structural diagram visualizes static elements of software, such as the types of variables and functions used in the program - the “what”
  • behavioral diagram visualizes dynamic behavior of software, such as the flow of an algorithm - the “how”

Comparing the waterfall and agile approaches:

Common deliverables for the waterfall and agile approaches.

Phase Common deliverables for waterfall approach

Common deliverables for agile approach

Analysis Software requirements specification document

Design UML diagrams indicating how the program's components should be designed

Implementation Functional software product

One or more functioning software components

Testing Functional software product that satisfies the program's goals

Program component that satisfies a portion of the program's goals

The analysis phase of each approach commonly produces a software requirements specification ( SRS ), a document describing all requirements for the software product

9.1 Language survey

A program written in a compiled language is first converted by a tool ( compiler ) into machine code, which can run on a particular machine. Examples include C, C++, Java, and C#.

In contrast, an interpreted language (aka scripting language ) is a language that is run one statement at a time by another program called an interpreter. Examples include Python, Javascript, and MATLAB.

Compiled: Interpreted: -C -Python

-C# -Javascript

-C++ -MATLAB

-Java

-Transformed into machine code before ran -Executed line by line

-Faster -Can be run right away

without converting to executable first

Compiled Interpreted

C, C#, C++, Java Python, Javascript, MATLAB First converted to an executable, then ran Run right away, without converting to an executable first Faster Slower - Executed line-by-line

  • More portable to different machines Statically typed Dynamically Typed

Library basics

A library is a set of pre-written functions (and other items) that carry out common tasks, that a programmer can use to improve productivity