Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 17 Fundamental Concepts Expressed in JavaScript.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fundamental Concepts Expressed in JavaScript Get with the Program lawrence.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Chapter 17 Fundamental Concepts Expressed in JavaScript.
Introduction to C Programming
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Objectives You should be able to describe: Data Types
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Chapter 2: Using Data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Announcements Due dates Project 1B—Wednesday by 10pm rule Thursday by 10pm Only once during quarter! Lab 5—Friday by 10pm Next week Labs 6/7—Tuesday.
Data And Variables Chapter Names vs. Values Michael Jordan name (the letter sequence used to refer to something) value (the thing itself)
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 JavaScript.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Fluency with Information Technology
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Overview: Programming Concepts
Chapter 17 JavaScript.
Introduction to C++ Programming
Chapter 8 JavaScript: Control Statements, Part 2
Introduction to JavaScript
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Javascript Chapter 19 and 20 5/3/2019.
More JavaScript B. Ramamurthy 5/7/2019.
Presentation transcript:

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

1-2 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-2 Overview: Programming 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

1-3 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-3 Programming Concepts Names, values, variables Declarations Data types, numbers, string literals and Booleans Assignment Expressions Conditionals

1-4 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-4

1-5 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-5

1-6 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-6 Names, Values, And Variables Names Have Changing Values –Example: U.S. President has current value of George W. Bush, previous values of Bill Clinton, George Washington Names in a Program Are Called Variables –Values associated with a name change in programs using the assignment statement ( = )

1-7 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-7 Identifiers and Their Rules Identifier is the character sequence that makes up a variable's name –Must have a particular form Must begin with a letter or underscore ( _ ) followed by any sequence of letters, digits, or underscore characters Cannot contain spaces Case sensitive (Capitalization matters!)

1-8 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-8 Identifiers and Their Rules ValidInvalid firstOne1stOne first1first-1 first_1first$1 first_Onefirst One FirstOneFirst1!

1-9 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-9 A 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

1-10 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Statement Terminator A program is a list of statements The statements may be run together on a line Each statement is terminated by the statement terminator symbol –In JavaScript, it is the semicolon ( ; )

1-11 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Rules 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

1-12 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Initializing 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;

1-13 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Three Basic Date Types of Javascript Numbers Strings Booleans –These kind of values are called data types or just types

1-14 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Numbers Rules for Writing Numbers –There are no "units" or commas –Can have about 10 significant digits and can range from to

1-15 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Strings Strings are sequences of keyboard characters Strings are always surrounded by single ( ' ' ) or double quotes ( " " ) Strings can initialize a declaration –var hairColor = "black";

1-16 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Rules for Writing Strings in JavaScript Must be surrounded by single or double quotes Allow most characters except return (Enter), backspace, tab, \ 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

1-17 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Literals 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 ( \ )

1-18 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-18

1-19 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Boolean 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

1-20 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Assignment Statement Changes a variable's value ; Assignment Symbol: –In JavaScript, the equal sign ( = ) –Example: weeks = days / 7;

1-21 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18-21

1-22 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Interpreting an Assignment Statement Value flows from the right side to the left side Read the assignment symbol as "is assigned" or "becomes" or "gets" The expression (right side) is computed or evaluated first –If there are any variables in it, their current value is used Then this computed value becomes the value of the variable on the left side

1-23 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Three Key Points about Assignment All three of the components must be given –if anything is missing, the statement is meaningless Flow of value to name 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

1-24 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley An 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

1-25 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arithmetic Operators Multiplication must be given explicitly with the asterisk ( * ) multiply operator Multiply and divide are performed before add and subtract –Unless grouped by parentheses JavaScript does not have an operator for exponents Binary operators operate on two operands (like + and *) Unary operators operate on one operand (like - for negate) Modulus or mod ( % ) divides two integers and returns the remainder

1-26 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Relational Operators Make comparisons between numeric values Outcome is a Boolean value, true or false <less than <=less than or equal to ==equal to (Note difference between = and ==) !=not equal to >=greater than or equal to >greater than

1-27 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Logical Operators To test two or more relationships together –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

1-28 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Operators (cont'd) Operator Overload –Use of an operator with different data types –Case of interest in JavaScript is + Addition –When used with numbers, it adds produces 9 Concatenation –When + is used with strings, it concatenates or joins the strings together "four" + "five" produces "fourfive"

1-29 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley A Conditional Statement if ( ) ; Boolean expression is a relational expression; then-statement is any JavaScript statement

1-30 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley If Statements and Their Flow of Control 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

1-31 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Compound 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

1-32 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley if/else Statements To execute statements if a condition is false if ( ) { ; } else { ; } 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

1-33 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested 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

1-34 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested if/else Statements if ( ) { if ( ) { ; } else { ; } if ( ) { ; } else { ; }

1-35 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Espresso Program

1-36 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The 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