Title: Concepts of Algorithmic Thinking Get With the Program: Basic Programming Concepts
1Concepts of Algorithmic Thinking Get With the
ProgramBasic Programming Concepts
2Just a thought
- JavaScript is
- The earliest known writing by Java Man.
- Programming language for Web pages.
- Instructions in the Starbucks bag on how to brew
good coffee.
3Programs vs. algorithms
4Five Essential Properties of Algorithms
- Input specified
- Output specified
- Definiteness
- Effectiveness
- Finiteness
5Program vs. Algorithm
- A program is an algorithm that has been
customized to - solve a specific task
- under a specific set of circumstances
- using a specific language
- Algorithm is general
- Program is specific
6Overview
- Basic programming concepts
7Programming Concepts
- Programming Act of formulating an algorithm or
program - Basic concepts have been developed over last 50
years to simplify common programming tasks - Concepts will be expressed in JavaScript in this
course - Easy syntax
- Immediate results
8Overview
- Names, values, variables
- Variable declarations
- Data types
- Assigning values to variables
- Expressionscalculating values
- Conditionals, branches, or tests
- Loops or iterations
9An Algorithm
10An Algorithm
11Names, values, and variables
12Names, Values, and Variables
- Names Have Changing Values
- U.S. President
- current value of George W. Bush
- previous values of Bill Clinton, George
Washington - Names in a Program Are Called Variables
- Variables store values and give you a handy way
to refer to the current value in the variable - Like we say The President to refer to our
current president
13Identifiers and Their Rules
- Identifier the name of a variable
- A variable name refers to the present value of
the variable, just like The President refers to
the current president. - Case sensitive
- HOME ? Home ? home
14Identifiers and Their Rules
15Variable Declarations
16A Variable Declaration Statement
- Declaration State what variables will be used
- Command is the word var
- For example, a program to calculate area of
circle given radius, needs variables area and
radius - var radius, area
- The declaration is a type of statement
- The declaration allocates space in memory to the
variable - Declare your variables at the top so you can find
them easily
17The Statement Terminator
- A program is a list of statements
- The statements may be run together on a line
- Use white space to help you
- read your code
- understand your program
- End each statement withthe statement terminator
symbol - In JavaScript, all statements terminate with the
semicolon ( )
18Names, Values, And Variables
- Declaring a variable
- Names a particular area in computer memory where
you can store values - Gives you a name, or handle, that is independent
of the current value
19Rules for Declaring Variables
- Every variable used in a program must be declared
(before it is used) - In JavaScript declaration can be anywhere in the
program - Programmers prefer to place them first
- Undefined values
- Variable has been declared but does not yet have
a value - var number1 // undefined value
- var number2 42 // initialized to the value 42
20Initializing a Declaration
- We can set an initial value as part of
declaration statement - var taxRate .088
- Related variables may be grouped in one
declaration/initialization unrelated variables
are usually placed in separate statements - var num1 42, num2, num3 var num1
42 var num2 var num3
21Data types
- Currency, string, number, boolean, date/time
22Three Basic Date Types of Javascript
- Numbers
- Strings
- Booleans
- These kind of values are called data types or
just types
23Numbers
- Rules for Writing Numbers
- No "units" or commas
- Up to 10 significant digits
- Range from 10-324 to 10308
24Strings
- sequences of keyboard characters
- always surrounded by single ( ' ' ) or double
quotes ( " " ) - No smart quotes!
- initialize a declaration
- var hairColor "black"
- quotes can nest
- firstLine Johnson called, Dude!
25Strings
- Rules for Writing Strings
- Must be surrounded by single or double quotes
- Allow most characters except return (Enter),
backspace, tab, backslash (\) - Double-quoted strings can contain single quoted
strings and vice versa - The apostrophe ( ' ) is the same as the single
quote - Any number of characters allowed in a string
- Minimum number of characters is zero ( "" ),
which is the empty string
26Literals
- String Literals stored in the computer
- Quotes are removed (they are only used to delimit
the string literal) - Any character can be stored in memory
- Even a character that cannot be typed can be
stored, using escape mechanism - in JavaScript, the backslash ( \ )
27Boolean Values
- Two logical values True and False
- They are values, not identifiers or strings
- Used implicitly throughout programming process
only occasionally for initializing variables - Mostly used to compare data or make decisions
28Comments
- //Single-line JavaScript comment
- /Multi-line JavaScript comment continues for
more than one line/ - Comments allow you to
- Annotate your code
- Remind yourself what you did and why
- Notes for yourselfor someone elsesix months
from now when youre making an update!
29Assigning values to variables
- All about assignment statements
30Assigning Values to Variables
- Assign values to variables with an assignment
operator. - We'll use for now.
- var yourAge, acctBal, custName
- yourAge 32 //store 32 in yourAge
- acctBal 100.75 //store 100.75 in acctBal
- custName 'Jeff' //store 'Jeff" in custName
- isCustomer true //store boolean true in
isCustomer (no quotes) - Var yourName 'Jeff' //alternate all-in-one line
assignment statement
31Assignment Statement
- ltVariablegt ltassignmentgtltexpressiongt
- Flow moves from right to left.
- Results of the ltexpressiongt replace the value
stored in the ltvariablegt.
32Assigning Values to Variables and Variables to
Variables
- We can also assign one variable to another
-
33Other Assignment Operators
34Assignment
- Three Key Points
- All three of the components must be given
- if anything is missing, the statement is
meaningless - Flow of value to identifier is always right to
left - Values of any variables used in the expression
are always their values before the start of the
execution of the assignment
35Expressions
- Calculating values in variables
36An Expression and its Syntax
- Algebra-like formula called an expression
- Describe the means of performing the actual
computation - Built out of values and operators
- Standard arithmetic operators are symbols of
basic arithmetic
37Arithmetic Operators
- Multiplication must be given explicitly with the
asterisk ( ) multiply operator - Multiply and divide are performed before add and
subtract - Unless grouped by parentheses
- Within parentheses multiply and divide are
performed first - JavaScript does not have an operator for exponents
38Relational Operators
- Make comparisons between numeric values
- Used in if statements and stop tests in loops
- Outcome is a Boolean value, true or false
- lt less than
- lt less than or equal to
- equal to
- ! not equal to
- gt greater than or equal to
- gt greater than
Note Difference between and compares
values assigns a value to a variable
39Logical Operators
- To test two or more relationships at once
- Teenagers are older than 12 and younger than 20
- Logical AND
- Operator is
- Outcome of a b is true if both a and b are
true otherwise it is false - Logical OR
- Operator is
- Outcome of a b is true if either a is true or
b is true - Logical NOT
- Operator is !
- Unary operator. Outcome is opposite of value of
operand
40Operators (cont'd)
- Addition
- Adds numbers
- 4 5 produces 9
- Concatenation
- Strings text together
- "four" "five" produces "fourfive
- "four" 5" produces "four5"
41Conditionals, branches, or tests
- Adding logic to an algorithm
42Conditional Statement Syntax
- if ( ltBoolean expressiongt )
- ltthen-statementgt
- Boolean expression is a relational expression
then-statement is any JavaScript statement
43If Statements Control Flow
- The Boolean statement, called a predicate, is
evaluated, producing a true or false outcome - If the outcome is true, the then-statement is
performed - If the outcome is false, the then-statement is
skipped - Then-statement can be written on the same line as
the Boolean or on the next line
44Compound Statements
- Sometimes we need to perform more than one
statement on a true outcome of the predicate test - You can have a sequence of statements in the then
clause - Group these statements using curly braces
- They are collected as a compound statement
45if/else Statements
- To execute statements if a condition is false
- if ( ltBoolean expressiongt )
- ltthen-statementsgt
- else
- ltelse-statementsgt
- The Boolean expression is evaluated first
- If the outcome is true, the then-statements are
executed and the else-statements are skipped - If the outcome is false, the then-statements are
skipped and the else-statements are executed
46Nested if else Statements
- The then-statement and the else-statement can
contain an if/else - The else is associated with the immediately
preceding if - Correct use of curly braces ensures that the else
matches with its if
47Nested if/else Statements
if (ltBoolean exp1gt) if (lt Boolean exp2gt)
ltthen-stmts for exp2gt
else ltelse-stmts for exp1gt
if (ltBoolean exp1gt) if (lt Boolean exp2gt)
ltthen-stmts for exp2gt else
ltelse-stmts for exp2gt
48The Espresso Program
49The Espresso Program
- Line 3 is a basic conditional statement
- Lines 4-4c use an if statement with conditionals
in the then statement - Line 5 uses basic if statement
- Lines 6, 7 compute using arithmetic operators
50In future lectures
51Coming soon to a computer near you
- Loops, or iterations, repeat code as many times
as necessary - The number of loops can be scripted
- Arrays are lists or collections
- Loops make it easy to iterate through all the
elements in an array - Functions allow you to package an algorithm for
use in multiple places
52Summary
- Programming is the exact specification of an
algorithm - JavaScript is typical with many rules
- Learning strategy
- Do the reading first
- Practicing is better than memorizing for learning
the rules - Use the program-save-reload-check plan
- Precision is your best friend
53End papers
- Why is programming fun?
- First is the sheer joy of making things. As the
child delights in his mud pie, so the adult
enjoys building things, especially things of his
own design. I think this delight must be an image
of God's delight in making things, a delight
shown in the distinctness and newness of each
leaf and each snowflake. - Source Frederick P. Brooks, Jr. The Mythical
Man-Month Essays on Software Engineering.