Docsity

Introduction to Programming Concepts: Variables, Expressions, Operators, and Control Flow, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
C173 – Scripting and Programming
Foundations – Notes
Chapter 1 – Introduction
- Programs consist of instructions executions one at a time. Basic instructions are
oInput – A program gets data, perhaps from a file, keyboard, touchscreen,
and network.
oProcess – A program performs computations on that data, such as adding
two values like x + y
oOutput – A program puts that data somewhere, such as to a file,
screen, network, etc.
- Variables – Refer to data like x, y, and z. Programs use variables. Name is due to
a variables value “varying” as a program assigns a variable like x with new
values.
- Algorithm - A sequence of instructions that solves a problem
- Flowchart – A graphical language for creating or viewing computer programs.
- Flowcharts use Coral language, a language intended for learning to program.
- Program is a list of statements, each statement carrying out some action and
executing one at a time. In a coral flowchart, each statement is in a graphical node,
with different shaped for different types of statements.
- Coral uses an interpreter to run a programs statement.
- Run and execute are words for carrying out a program’s statements.
- Program is a sequence of statements.
- Parallelogram represents an input statement, written as variable = get next input.
- Parallelogram also represent an output statement.
- String literal consists of text (characters) within double quotes example being “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.
System automatically moves the cursor after the previously-output items.
- Newline – A special two-character sequence (\n) whose appearance in an output
string literal causes the cursor to move to the next output line. 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. Coral
uses // for comments. Comment must be on its own line.
- Whitespace refers to blank spaces (space and tab characters) between items within
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Introduction to Programming Concepts: Variables, Expressions, Operators, and Control Flow and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

C173 – Scripting and Programming

Foundations – Notes

Chapter 1 – Introduction

  • Programs consist of instructions executions one at a time. Basic instructions are o Input – A program gets data, perhaps from a file, keyboard, touchscreen, and network. o Process – A program performs computations on that data, such as adding two values like x + y o Output – A program puts that data somewhere, such as to a file, screen, network, etc.
  • Variables – Refer to data like x, y, and z. Programs use variables. Name is due to a variables value “varying” as a program assigns a variable like x with new values.
  • Algorithm - A sequence of instructions that solves a problem
  • Flowchart – A graphical language for creating or viewing computer programs.
  • Flowcharts use Coral language, a language intended for learning to program.
  • Program is a list of statements, each statement carrying out some action and executing one at a time. In a coral flowchart, each statement is in a graphical node, with different shaped for different types of statements.
  • Coral uses an interpreter to run a programs statement.
  • Run and execute are words for carrying out a program’s statements.
  • Program is a sequence of statements.
  • Parallelogram represents an input statement, written as variable = get next input.
  • Parallelogram also represent an output statement.
  • String literal consists of text (characters) within double quotes example being “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. System automatically moves the cursor after the previously-output items.
  • Newline – A special two-character sequence (\n) whose appearance in an output string literal causes the cursor to move to the next output line. 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. Coral uses // for comments. Comment must be on its own line.
  • Whitespace refers to blank spaces (space and tab characters) between items within

a statement, and to newlines. Ignored during execution.

  • 1 equals on for a switch 0 is off.
  • Moore’s Law – Engineers have reduced switch sizes by half about every 2 years.
  • Bit – On is called 1, and off is 0
  • Byte – Eight bits
  • ASCII and Unicode are character encoding standards.
  • ASCII uses 7 bits per code for 128 characters
  • Unicode can represent over 100,000 items, such as symbols and non-English characters.
  • Pseudocode – A text that resembles a program in a real programming language but is simplified to aide human understanding.
  • “Code” – Is a term for a program written in text.
  • “Pseudo” – Means similar to.
  • Coral has two versions, flowchart, and code. Coral code looks like pseudocode but follows rules that allow execution.
  • Pseudocode represents a program textually and informally.

Chapter 2 - Variables / Assignments

  • Variable is a named item, such as x or Num People, used to hold a value.
  • Assignment Statement – Assigns a variable with a value such as x = 5.
  • 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, 999, 0, or - 25.
  • Expressions – May be a number like 80, a variable name like nonapple’s, or a simple calculation like nonapple’s + 1. Simple calculations can involve standard math operators like +, -, and *.
  • Identifiers = An identifier must be a sequence of letter, underscores, and digits. Starts with a letter.
  • _ is called an underscore.
  • Identifiers are case sensitive.
  • Reverse word (or called a keyword) is a word that is part of the language, like integer, get, or put. You cannot see reversed words as an identifier.
  • Naming Conventions – A set of style guidelines defined by a company, team, teacher, etc. for naming variables. Two common conventions are o Camel Case – Lower camel case multiple words, capitalizing each word except the first like nonapple’s. o Underscore Separated – Words are lowercase and separated by an underscore, like num_apples.
  • Expression is a combination of items, like variables, literals, operators, and parentheses, that evaluate to a value. Ex 2* (x+1) is an expression. Can be just a variable like x or literal like 2.
  • Literal is a specific value in code like 2.
  • Operator is a symbol that performs a built-in calculation. +, -, *, /
  • Expression evaluates to a value, which replaces the expression. Ex if X is 5, then x +

then myIntVar * 1.0 converters integer 7.0 to 7

  • Modulo operator (%) evaluates to the remainder of the division of two integer operands. Ex: 23 % 10 is 3. Essentially the remainder from integer division is the modulo operator.
  • Data Types – o Integer – An integer is a whole number value, like 1, 999, 0, or -25. Typically used for things that are counted. o Float – A float is a floating-point number, which is a real number 98.6, 0.001, or -667.645. Typically used for things that are measured. o Character – A character is a single letter like ‘A’, ‘a’, or ‘%’ o String – A string is a sequence of characters, like “hello” or “The forecast is sunny today” o Boolean – A Boolean refers to a quantity that has two possible values, true or false. o 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 – A named value item that holds a value that cannot change.

Chapter 3 – Branches

  • Branch – A sequence of statements only executed under a certain decision.
  • Decision – Creates two branches. If the decision’s expression is true, the first branch executes, else the second branch executes.
  • If-Else – A decisions and its two branches. IF the decision’s expression is true then the first branch executes, ELSE the second branch executes.
  • If-elseif branches – A series of decisions appear cascaded in each decision’s false branch.
  • Nested branches – A branch’s statement can include any valid statements, including another if-else branch. Can take on various forms, and the if-else branches may even use different variables.
  • Equality operator checks whether two operand’s values are the same (==) or different (! =). Equality is ==. An expression involving an equality operator evaluates to a Boolean value.
  • Boolean is a type that has just two values – true or false.
  • Relational operator checks how one operand’s value to another, like being greater than. o < - a < b means a is less than b o > - a > b means a is greater than b o <= - a <= b means a is less than or equal to b o >= - a >= b means a is greater than or equal to b
  • Logical operator treats operands as being true or false and evaluates to true or false. Logical operators include and, or, not.
  • Logical Operators – o (a) and (b) Logical and: True when both operands are true

o (a) or (b) Logical or: True when at least one of the two operands is true o Not(a) Logical not: True when the one operand is false, and vice-versa

  • If any operand of the or operator is true, then the or operator evaluates to true.
  • Precedence rules – The order in which operators are evaluated in an expression. o () – Items within parentheses are evaluated first o Not – Logical not is next o * / % + - - Arithmetic operators o < <= > >= - Relational operators o == != - Equality and inequality operators o And – Logical and o Or – Logical or
  • Floating-point numbers should not be compared using the == operator.
  • If statement – Is an if decision expression followed by sub-statements, with no else part.
  • If-elseif statement start with an if decision expression, followed by elseif decision expressions, and ending with else; Only one branch can execute.
  • Nested if-else statements – A branch’s statements can include any valid statements, including another if-else statement. Can take on various forms and the if-else statements may even use different variables.

Chapter 4 – Loops

  • Loop is a program construct that repeatedly executes the loop’s statements (known as a loop body) while the loop’s expression is true.
  • Iteration – Each time through a loop’s statement
  • Sentinel Value – Is a special value indicating the end of a list, such as a list of positive integers ending with 0. 0 is not included in the average.
  • Loops commonly must iterate a specific number of times, such as 10 times. Loops with specific numbers of iterations include three parts – a loop variable initialization before the loop, a decision statement with the loop expression, and a loop variable update at the end of the loop body.
  • Infinite Loop – Is a loop that never stops iterating. A common error is to accidentally create an infinite loop, often by forgetting to update a variable in the body, or by creating a loop expression whose evaluation to false isn’t always reachable.
  • GCD – Greatest common divisor
  • While Loop – A loop that repeatedly executes the loop body while the loop’s expression evaluates to true. Number of iterations is not (easily) computable before the loop, like iterating until the input is ‘q’
  • For loop – Is a loop consisting of a loop variable initialization, a loop expression, and a loop variable update that typically describes iterating for a specific number of times. Number of iterations is computable before the loop, like iterating N times.
  • Incremental development can be faster.
  • Functions might cause slower execution but can have improved readability.
  • Functions can decrease redundant statements.
  • In coral code a function definition begins with the word Function, the function’s name, and a list of comma-separated paraments in parentheses, and a return indication.
  • If a function has a return value, then it defines the return variable’s type and name, it not then it states return nothing.
  • A function can only return one item
  • A function’s parameter may be defined as an array, by including the word array just as in a variable declaration.
  • A programmers execution begins with the main function.
  • A programmer can influence a functions behavior via an input.
  • Values passed to a function are the function’s arguments.
  • Function statement may include branches, loops, calls to other functions, and other statements.

Chapter 7 – 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.
  • Algorithm time efficiency – The number of calculations required to solve a problem
  • Computational Problem specifies an input, a question about the input that can be answered using a computer, and the desired output.
  • Binary search is more efficient than linear search.
  • Shortest path algorithm determines the shortest path from a start vertex to each vertex in a graph
  • Longest common substring – Determines the longest common substring that exists in two input strings.
  • NP-Complete – Problems are a set of problems for which no know efficient algorithm exists. NP-complete problems have the following characteristics o No efficient algorithm has been found to solve an NP-complete problem o No one has proven that an efficient algorithm to solve an NP-complete problem is impossible o If an efficient algorithm exists for one NP-complete problem, then all NP- complete problems can be solved efficiently.
  • An algorithm with a polynomial runtime is considered efficient, generally one whose runtime increases no more than polynomially with respective to the input size
  • In contrast, an algorithm with an exponential runtime is not efficient.
  • Algorithm efficiency is typically measured by the algorithm’s computational complexity.
  • Computational complexity is the amount of resources used by the algorithm. The most common resources considered are the runtime and memory usage.
  • Runtime complexity is a function, T(N), that represents the number of constant time operations performed by the algorithm on an input size N.
  • Best case is the scenario where the algorithm does the minim possible number of operations
  • Worst case is the scenario where the algorithm does the maximum possible number of operations.
  • Space complexity is a function, S(N), that represents the number of fixed-size memory units used by the algorithm for an input size N. Ex. The space complexity of an algorithm that duplicates a list of numbers is S(N) = 2N + k, where k is a constant representing memory used for things like the loop counter and list pointers.
  • Auxiliary space complexity is the space complexity not including the input data. Ex. An algorithm to find the maximum number in a list will have a space complexity of S(N) = N
    • k, but an auxiliary space complexity of S(N) = k, where k is constant.
  • Linear search is a searching algorithm that starts from the beginning of a list and checks each element until the search key is found or the end of the list is reached.
  • Runtime – The time the algorithm takes to execute.
  • Binary Search – Checks the middle contact first. If the desired contact comes alphabetically before the middle contact, binary search will then search the first half and otherwise the list half. Each step reduces the contacts that need to be searched by half.
  • Binary search is a faster algorithm for searching a list if the list’s elements are sorted and directly accessible (such as an array). Binary search first checks the middle element of the list. If the search key is found, the algorithm returns the matching location. If the search key is not found, the algorithm repeats the search on the remaining left sublist or the remaining right sublist starting in the middle of those sublists.
  • Sorting is the process of converting a list of elements into ascending (or descending) order. Can be alphabetical or numerical.
  • Heuristic – A technique that willingly accepts a non-optimal or less accurate solution in order to improve execution speed.
  • Heuristic Algorithm is an algorithm that quickly determines a near optimal or approximate solution. Such an algorithm can be designed to solve the 0- knapsack problem. The knapsack problem with the quantity of each item limited to 1.

Chapter 8 – The Design process

  • SDLC – Systems development life cycle. Has four phases o Analysis Phase defines a program’s goals o Design Phase defines specifics of how to build a program. o Implementation Phase involves writing the program. o Testing Phase checks the programs correctly meets the goals.
  • Compiled language – First converted by a tool into machine code, which can run on a particular machine. Ex. include C, C++, Java, and C#.
  • Interpreted Language (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 runs faster but interpreted is easier for newbies.
  • Statically typed – Meaning each variable’s type must be declared and cannot change while the program runs. Most compiled languages are like this
  • Dynamically typed – Meaning a variable’s type may change while a program executes usually based on what is assigned to the variable. Many interpreted languages are like this like python.
  • Statically typed languages are often considered safer due to reporting errors during compilation, while dynamically typed languages are considered easier to use when types need to change, requiring less converting or fewer variables.
  • Object consists of some internal data items plus operations that can be performed on that data.
  • Object-orientated language supports decomposing a program into objects. C++, java, python, and C# provide extensive object-oriented support.
  • C does not support objects
  • Markup language allows a developer to describe a document’s content, desired formatting, or other features. HTML is a popular markup language.
  • Library is a set of pre-written functions that carry out common tasks, that a programmer can use to improve productivity. Must include a library to use its functions. No limits exist on the number of libraries you can have.

Chapter 10 – Troubleshooting Process

  • Troubleshooting is a systematic process for finding and fixing a problem’s cause in a system. Troubleshooting process contains two steps o Create a hypothesis. A hypothesis states a possible cause of a problem. o Run a test. A test is a procedure whose result validates or invalidates a hypothesis.
  • Symmetric problems are easier and clear to solve while asymmetric tend to be more complex
  • Hierarchy means an object can be decomposed into sub-objects.
  • Binary search divides an item into two halves, runs a test to decide in which half something lies, and repeats the binary search on that half.

Chapter 11 – Program Debugging

  • A program is a series of instructions (aka statements) a computer executes to perform a calculation, like a recipe is a series of instructions a chef executes to make a meal.
  • A program problem’s cause is called a bug, and troubleshooting is called debugging.
  • Visual inspection is a basic debugging process.
  • Debug output statements is another debugging process whose output helps determine whether the preceding statement has the bug.
  • Hierarchical debugging breaks down big code into sections and uses debug output statements on those sections.

Additional Notes –

  • Purpose of parentheses in a programming expression is to group expressions
  • Return Value of a function is the output of a function
  • Study more on Waterfall approach and agile
  • Study more on UML diagrams
  • Class Diagram characteristics – System’s classes, attributes, and methods and their features, constraints, and relationships
  • Library functions common relationship is the functions all relate to the same purpose

Software Design Process –

  • Software development life cycle process has four phases o Analysis Phase – Defines a programs goals o Design Phase – Defines specifics of how to build a program o Implementation Phase – Involves writing the program o Testing Phase – Checks that the programs correctly meet the goals
  • Object is a grouping of data (variables) and operations that can be performed on that data (functions)
  • UML – Universal Modeling Language is a modeling language for software design that uses different types of diagrams to visualize the structure and behavior of programs. UML consists of several structural and behavioral diagrams
  • Structural Diagram – Visualizes static elements of software, such as the types of variables and functions used in the program. Ex. List of components necessary to build an online shopping system.
  • Behavioral Diagram – Visualizes dynamic behavior of software, such as the flow of an algorithm.
  • Use Case Diagram – Is a behavioral diagram used to visually model how a user interacts with a software program. Actions from users and the accompanying actions in software, as well as connections between different components of the software. Ex. The use case diagram below shows components of an online shopping system and how the customer, as well as how external services, connect to system components. Does not contain

o Testing – ▪ Waterfall delivers functional software product that satisfies the program’s goals ▪ Agile delivers program component that satisfies a portion of the program’s goals.