25 October Conditionals and Loops. Presentations Brendan: Cyberwarfare Casey: Online shopping (No current event today)

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
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,
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
29 November JavaScript: Arrays and Iterations Functions.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
JavaScript, Fourth Edition
JavaScript, Third Edition
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine.
Programming Logic and Design Sixth Edition
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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.
LESSON 6 – Arithmetic Operators
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
CPS120: Introduction to Computer Science Operations Lecture 9.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
CSC 107 – Programming For Science. The Week’s Goal.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
JavaScript 101 Lesson 3: Variables and Arithmetic.
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.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 JavaScript.
1 Variables and Arithmetic Operators in JavaScript.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
What are Operators? Some useful operators to get you started.
Tutorial 11 1 JavaScript Operators and Expressions.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 Type Called bool  Bool has only two possible values: True and False.
Overview: Programming Concepts
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Primitive Data, Variables, Loops (Maybe)
Building Java Programs Chapter 2
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Chapter 8 JavaScript: Control Statements, Part 2
Building Java Programs
T. Jumana Abu Shmais – AOU - Riyadh
Building Java Programs Chapter 2
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Boolean Expressions to Make Comparisons
Building Java Programs
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Building Java Programs Chapter 2
Matlab Basics.
COMPUTING.
Presentation transcript:

25 October Conditionals and Loops

Presentations Brendan: Cyberwarfare Casey: Online shopping (No current event today)

Announcements Papers: Good batch of papers Feel free to talk to me about any comments – content or style “Hygiene” – staple, title, page numbers, proof

Documenting Your Code Documenting your code is not necessary for the compiler or interpreter, but is typically needed by people – including yourself! ALWAYS document what you write Information for people only are comments

Comments Two Forms /* … */ // /* … */ Everything between the delimiters is a comment and not processed Can cross multiple lines Can start anywhere // Everything from there until the end of the line is not processed

Comment Examples var start = 0; // beginning of the string var start = 0; /* identifies the place start processing */ var start = 0; /* beginning of the string var end = 0;

What to do with Variables Simplest action is to assign a value: assignment statement Name = “Tom”; Price = 13.25; Can use any type of expression on the right – both constants and variables

Expressions: Arithmetic There are rules of precedence 1+b*c Do I multiply or add first? If you use parentheses, you don’t have to remember! Binary operations +, -, /, *, % (modulus or remainder) Unary operations Negation

Expressions: String Primary one is concatenation Uses “+” If with either entry is a string, means concatenate Else means add

Expressions: Boolean Comparisons Numeric or string >, <, == Not and combinations Logical operations And, or, not

Logic Tables 0 = false, 1 = true And 0 && 0 = 0 0 && 1 = 0 1 && 0 = 0 1 && 1 = 1 Or 0 || 0 = 0 0 || 1 = 1 1 || 0 = 1 1 || 1 = 1 Not !0 = 1 !1 = 0 There are actually 8 different logical operations on two variables? How would you convince yourself of that?

Practice Write a statement that computes total price including tax and shipping computes the area of a circle calculates the average speed of a car trip

How to Change the Operations Conditional statements Iterations (doing it more than once) Functions (an algorithm that can be executed more than once) All need compound statements – multiple statements that can be processed as one { stmt; stmt; stmt; }

Conditional Statements Executes the statement only if a condition is true Format: if (boolean) statement; /* avoid divide by zero */ if (n != 0) m = m/n; /* if purchase made, increase total purchase price and the number of items bought */ if (purchased = true) { total = total + price; items = items + quantity; };

Conditional with else Executes the first statement if a condition is true or the second if it’s false Format: if (boolean) statement; else statement; /* take the absolute value */ if (n < 0) abs_val = !n; else abs_val = n;

Conditional with else (cont.) /* execute either add or subtract depending on button and keep track of how many of each operation is performed */ if (operation = add) { result = value1 + value2; add_ops = add_ops + 1; }; else { // must be subtract result = value1 - value2; sub_ops = sub_ops + 1; };

Practice Write a statement that computes total price including tax if the purchaser is in state and shipping in all cases computes the area of a circle or a square depending on whether the radius or side was given calculates the average speed of a car trip if the length of the trip was more than one hour

Arrays Arrays are variables with dimensions Why are they interesting? Dealing with a table of values Translations Series of different values Processing a stretch of elements Strings Series of numbers

Arrays in JavaScript Declaration var name = new Array (no-elems); Examples var ZodiacSigns = new Array (12); Or can initialize Var HalfZodiacSigns = new Array (“Aries”, “Taurus”, “Gemini”, “Cancer”, “Leo”, “Virgo”);

Referencing Arrays Array element can be referenced any place that a constant or variable can be used Form: array [ index ] Array is the name Index is which element of the array you want Can be a constant, variable, or expression Must evaluate to an integer between 0 and one less than the array size Example MySign = HalfZodiacSigns[0]; What value does MySign have?

Iterations: Doing it more than once If I want to process all of the elements in an array, I need to tell the computer to do something for each element: Form that we will use for ( itervar = 0; itervar < endval ; itervar++) { Statement-list } var++ is a shorthand that is equivalent to var = var + 1;

What it does for ( i = 0 ; i < n ; i++) { Statement-list } 1. i = 0; 2. execute statement-list 3. i = i + 1; 4. if i < n go to #2

Iteration Example var numbers = new Array (10); var j; /* initialize an array to 1 to 10 */ for ( j=0; j<10; j++) { numbers[j] = j+1; }

Practice Write a loop that computes total price including tax and shipping for 3 orders computes the area of a “n” circles calculate the average speed of 2 legs of a car trip

Loops with conditionals var values = new Array (10); var results = new Array (10); var j; for ( j=0; j<10; j++) { if (values[j] < 0) results[j] = -j; else results[j] = j; }