Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Similar documents
Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Chapter 17. Fundamental Concepts Expressed in JavaScript

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Lecture 3 Tao Wang 1

Everyday Algorithms. Algorithms and Introduction to Programming. bake a cake. start. mix sugar and butter. stir in 2 eggs. stir in flour.

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

C: How to Program. Week /Mar/05

2 nd Week Lecture Notes

COSC 122 Computer Fluency. Programming Basics. Dr. Ramon Lawrence University of British Columbia Okanagan

Lecture 5 Tao Wang 1

Visual C# Instructor s Manual Table of Contents

Chapter 2 - Introduction to C Programming

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Operators. Java operators are classified into three categories:

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Full file at C How to Program, 6/e Multiple Choice Test Bank

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

CSCE 120: Learning To Code

CSC Web Programming. Introduction to JavaScript

Expr Language Reference

JME Language Reference Manual

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Full file at

CS 106 Introduction to Computer Science I

Fundamental of Programming (C)

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Chapter 2. Lexical Elements & Operators

Language Reference Manual

Chapter 2: Using Data

4. Inputting data or messages to a function is called passing data to the function.

Introduction to C# Applications

ARG! Language Reference Manual

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CGS 3066: Spring 2015 JavaScript Reference

Basics of Java Programming

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Chapter 2, Part III Arithmetic Operators and Decision Making

Program Elements -- Introduction

Information Science 1

LECTURE 02 INTRODUCTION TO C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

The C++ Language. Arizona State University 1

Elementary Programming

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Client-Side Web Technologies. JavaScript Part I

Mirage. Language Reference Manual. Image drawn using Mirage 1.1. Columbia University COMS W4115 Programming Languages and Translators Fall 2006

Chapter 2 Working with Data Types and Operators

BITG 1233: Introduction to C++

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

The pixelman Language Reference Manual. Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

CS313D: ADVANCED PROGRAMMING LANGUAGE

Objectives. In this chapter, you will:

IT 374 C# and Applications/ IT695 C# Data Structures

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Using Data

(Not Quite) Minijava

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

Computer Programming : C++

Typescript on LLVM Language Reference Manual

UNIT- 3 Introduction to C++

Cellular Automata Language (CAL) Language Reference Manual

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

CA4003 Compiler Construction Assignment Language Definition

QUIZ: What value is stored in a after this

3 The Building Blocks: Data Types, Literals, and Variables

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

In addition to the primary macro syntax, the system also supports several special macro types:

Chapter 2: Overview of C++

corgi Language Reference Manual COMS W4115

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

DaMPL. Language Reference Manual. Henrique Grando

Lecture 3. Input, Output and Data Types

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

JAVA Programming Fundamentals

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Introduction to TURING

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

ECE 122 Engineering Problem Solving with Java

COMP 110 Introduction to Programming. What did we discuss?

Slang Reference Manual

JavaScript I Language Basics

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

4 Programming Fundamentals. Introduction to Programming 1 1

Transcription:

Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder 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-2 18-2 Programming Concepts Names, values, variables Declarations Data types, numbers, string literals and Booleans Assignment Expressions Conditionals 1-3 1-4 18-3 18-4

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-5 1-6 18-5 18-6 Identifiers and Their Rules 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!) Valid firstone first1 first_1 first_one FirstOne Invalid 1stOne first-1 first$1 first One First1! 1-7 1-8 18-7 18-8

A Variable Declaration Statement The Statement Terminator 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 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-9 1-10 18-9 18-10 Rules for Declaring Variables Initializing a Declaration 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 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-11 1-12 18-11 18-12

Three Basic Date Types of Javascript Numbers Numbers Strings Booleans These kind of values are called data types or just types Rules for Writing Numbers There are no "units" or commas Can have about 10 significant digits and can range from 10-324 to 10 308 1-13 1-14 18-13 18-14 Strings Rules for Writing Strings in JavaScript Strings are sequences of keyboard characters Strings are always surrounded by single ( ' ' ) or double quotes ( " " ) Strings can initialize a declaration var haircolor = "black"; 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-15 1-16 18-15 18-16

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-17 1-18 18-17 18-18 Boolean Values The Assignment Statement Two logical values: True and False Changes a variable's value They are values, not identifiers or strings <variable> <assignment symbol> <expression>; Used implicitly throughout programming process; only occasionally for initializing variables Mostly used to compare data or make decisions Assignment Symbol: In JavaScript, the equal sign ( = ) Example: weeks = days / 7; 1-19 1-20 18-19 18-20

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-21 1-22 18-21 18-22 Three Key Points about Assignment An Expression and its Syntax 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 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-23 1-24 18-23 18-24

Arithmetic Operators Relational 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 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-25 1-26 18-25 18-26 Logical Operators Operators (cont'd) 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 Operator Overload Use of an operator with different data types Case of interest in JavaScript is + Addition When used with numbers, it adds 4 + 5 produces 9 Concatenation When + is used with strings, it concatenates or joins the strings together "four" + "five" produces "fourfive" 1-27 1-28 18-27 18-28

A Conditional Statement If Statements and Their Flow of Control if ( <Boolean expression> ) <then-statement>; Boolean expression is a relational expression; then-statement is any JavaScript statement 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-29 1-30 18-29 18-30 Compound Statements if/else 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 To execute statements if a condition is false if ( <Boolean expression> ) <then-statements>; else <else-statements>; The Boolean expression is evaluated first If the outcome is true, the then-statements are executed and the elsestatements are skipped If the outcome is false, the then-statements are skipped and the elsestatements are executed 1-31 1-32 18-31 18-32

Nested if/else Statements 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 if (<Boolean exp1>) if (< Boolean exp2>) <then-stmts for exp2>; else <else-stmts for exp2>; if (<Boolean exp1>) if (< Boolean exp2>) <then-stmts for exp2>; else <else-stmts for exp1>; 1-33 1-34 18-33 18-34 The Espresso Program 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 1-35 1-36 18-35 18-36