Logic and Control. 4-2 Decision (selection) structure: if (condition) { statement etc. } Example: if (age == 15) { document.write(“ you are a fifteen”);

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
Repeating Actions While and For Loops
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Introduction to JavaScript for Python Programmers
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Week 9 PHP Cookies and Session Introduction to JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
ALBERT WAVERING BOBBY SENG. Week 4: JavaScript  Quiz  Announcements/questions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
22/11/ Selection If selection construct.
JavaScript, Fourth Edition
CIS 133 Mashup Javascript, jQuery and XML Chapter 3 Building Arrays and Controlling Flow.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Control Structures, Blocks and Compound Statements A good programming language allows you to control the flow of your program in three ways: - execute.
Pertemuan 5 IT133 Pengembangan Web JavaScript. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
JavaScript, Sixth Edition
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Learning Javascript From Mr Saem
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
CHAPTER 4 DECISIONS & LOOPS
Loops BIS1523 – Lecture 10.
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
JavaScript Syntax and Semantics
Scripts & Functions Scripts and functions are contained in .m-files
Javascript Conditionals.
JavaScript: Control Statements.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
JavaScript: Control Statements I
Arrays, For loop While loop Do while loop
Chapter 8 JavaScript: Control Statements, Part 2
Introduction to JavaScript for Python Programmers
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 4: Decision Structures and Boolean Logic
Chapter 6: Repetition Statements
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 4: Decision Structures and Boolean Logic
Chapter 8 JavaScript: Control Statements, Part 2
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Logic and Control

4-2 Decision (selection) structure: if (condition) { statement etc. } Example: if (age == 15) { document.write(“ you are a fifteen”); }

4-3 A relational operator (aka comparison operator) determines whether a specific relationship exists between two values. Table 4-1 Relational operators

4-4 Examples: if (age > 90)… if (size < 10)… if (sum <= 50)… if (theNumber == 100)… if (x != 100)…

4-5 Examples: if (age > 90) { document.write (“you are a bit old”); } if (size< 10) { document.write (“you are just the right size”); } if (total >= 0) { total += 50; } if (name == “jasmine”) { document.write (“nice name!”); }

4-6 if (condition) { statement etc. } else { statement etc. }

4-7 if (age >= 20) { document.write(“you are an adult”); } else { document.write(“you are NOT an adult”); } if (salary <= 10) { document.write(“you need a raise”); } else { document.write(“you make a fine salary”); }

4-8 Concept: You can compare strings. This allows you to create decision structures that test the value of a string. If (password == “blahblah”) { document.write(“that is the correct password”); } else { document.write(“that is the correct password”); }

4-9 Other String Comparisons Figure 4-10 Character codes for the strings 'Mary' and 'Mark' Figure 4-11 Comparing each character in a string

4-10 The if-else if - else Statement if (condition_1) { statement etc. } else if (condition_2){ statement etc. } else{ statement etc. }

4-11 if (score < 60) { document.write(‘Your grade is F.’); } else if (score < 70) { document.write( ‘Your grade is D.’); } else if score < 80: { document.write( ‘Your grade is C.’); } else if score < 90 { document.write( ‘Your grade is B.’); } else { document.write( ‘Your grade is A.’); }

 isNaN( ) – determines whether a number is legal  Returns True (if not a number)  or False (if it is a number)  NaN is an abbreviation for: Not a Number  Examples of nonNumbers:  A string  A number divided by 0  alert(isNaN(4)) will return false  alert(isNaN(“four”)) will return true

 Logical operators are used to determine the logic between variables or values.  Given that x=6 and y=3, the table below explains the logical operators: Operator Description Example && and (x 1) is true || or (x==5 || y==5) is false ! not !(x==y) is true

 Many times you will need to check multiple conditions within the same statement  Logical Operators allow you to check multiple conditions:  Examples: if ((x > 100) && (x < 1000)) … if (age 65) …

 The innerHTML property sets or returns the inner HTML of an element.  We will use it to set error messages on fields Example: Styling error message #Nerror {color:red;} The input field with place for error message Setting the innerHTML of the span element to conatin error message document.getElementById(" Nerror ").innerHTML = "Must be numeric";

5-16 A condition-controlled loop uses a true/false condition to control the number of times that it repeats. while (number < 20) … while (password != passwordOnFile)… while (isNaN(ans))…

5-17 while (condition) { statement etc. }

5-18 The loop will continue to run as long as count is less than, or equal to 5. var count = 1;//set count to have a value of 1 while (count <= 5)//this is the condition being tested { document.write(count + " "); count++;// this will increment the count by 1 } If the condition (count <= 5) is true, the statements inside the brackets are executed and then the loop starts over If the condition is false, the statements inside the brackets are skipped and the program exits the loop

5-19 Programs that calculate the total of a series of numbers typically use two elements: A loop that reads each number in the series A variable that accumulates the total of the numbers as they are read.

5-20 for (var=startvalue; var<=endvalue; var=var+increment) { code to be executed… } var i = 1; for (i = 1; i <= 5; i++)…

5-21 The example below defines a loop that starts with count=0. The loop will continue to run as long as count is less than, or equal to 5. count will increase by 1 each time the loop runs. var i=0; for (i=0; i "); }

 Loops used extensively when working with arrays  Example: var fruits = [‘apple’, ‘peach’, ‘orange’]; var counter = 0; while (counter < fruits.length) { document.write(fruits[counter]) + ‘ ‘); counter++; }

function functionName ( ) { some code goes in here…. }  function – keyword, tells browser you are declaring a function  functionName – the name you choose to call the function  ( ) - place for parameters, extra info the function may need  { - show the beginning of function code  } – shows the end of the function code

 Functions only execute if they are called!!  Good Practice – place function definitions in at the top of the head section  You can call a function in the head section or the body section  To call a function, provide the function name and the parameters (if any)  Example: doSomething( );

 Used to supply 1 or more values to a function  Set in the function header inside the parenthesis  General format: function doSomething(var1, var2) //this is a function header  When you make a function “call”, you need to supply the parameters  Example: doSomething(myVar1, myVar2) //this is a function call

 Used to return a value from a function  Place the return statement as the last line of the function  The value is returned to the place in the main script where the function was called  You need to save the returned value in a variable

 When a variable is created by an assignment statement inside the function, the variable is local to that function  Can be assigned or modified in the function  Once the function is done (hit the closing bracket }), variable dies – value cannot be used outside of function  The variable does not exist outside the function!