Advanced Decisions and Loops Chapter 5. 5.1 Some Simple Schoolroom Statistics.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Going Round and Round: the Repetition Structure Chapter 4.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Copyright © 2013 by John Wiley & Sons. All rights reserved. LOOPS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30,
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
JavaScript, Fourth Edition
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Chapter 7 Problem Solving with Loops
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
CHAPTER 4 DECISIONS & LOOPS
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Python Loops and Iteration
Chapter 5: Control Structure
Control Structures II (Repetition)
Chapter 4 – Control Structures Part 1
Chapter 6: Conditional Statements and Loops
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
LOOPS.
Lecture 4B More Repetition Richard Gesick
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Control Structure Senior Lecturer
Control Structure Senior Lecturer
Arithmetic Expressions
Chapter 2.1 Repetition.
Chapter 5: Control Structures II (Repetition)
Topics Introduction to Repetition Structures
Loops.
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Advanced Decisions and Loops Chapter 5

5.1 Some Simple Schoolroom Statistics

It All Adds Up: Summing Program to sum test scores using a sentinel value to end the input: Summing 3. 4.function getSum() { 5.var score = 0; var sum = 0; 6.while (score != -999) { 7.sum = sum + score; 8.score = parseInt(prompt("Enter a score or enter -999 when you're finished:"," ")); 9.} 10.document.write("the sum of these scores is: “ + sum + "."); 11.} Click to enter students' scores 15. <input type="button" id="scores" value="Enter the scores“ onclick="getSum();" /> 17.

Computing Averages To find the average, we must sum and count the numbers entered. The example finds the average of values entered using a counter to keep track of how many values are entered. 1. function getAverage(){ 2.var score = 0; var sum = 0; 3.var count = 0; var average = 0; 4.while (score != -999) { 5.sum = sum + score; 6.count++; 7.score = parseInt(prompt("Enter a score or enter -999 when you're finished:"," ")); 8.} 9.average = sum/(count - 1); 10.document.write(" The sum of these scores is: " + sum + ". "); 11. document.write(" The average of these scores is: " + average + ". "); 12. }

Putting it all Together: Sum, Average, Highest, and Lowest Values 1. function getStats() 2. { 3.var score = 0; var sum = 0; var count = 1; var average = 0; 4.var high = 0; var low = 0; 5.score = parseInt(prompt("Enter a score or enter -999 when finished:"," ")); 6.low = score; high = score; 7.while (score != -999) { 8. sum = sum + score; 9. count++; 10. score = parseInt(prompt("Enter a score or -999 when finished:"," ")); 11. if (score > high) 12.high = score; 13. if ((score < low) && (score != -999)) 14.low = score; 15.} 16.average = parseInt(sum/(count - 1)); 17.document.write(" Number of scores entered: " + (count - 1) + ". "); 18.document.write(" Sum of these scores: " + sum + ". "); 19.document.write(" Average of these scores: “ + average + ". "); 20.document.write(" Lowest score is: " + low + ". "); 21.document.write(" Hghest score is: " + high + ". "); 22. }

Is it Odd or Even? It is often important to identify if a number is even or odd. If a number is odd, then it will have a remainder when divided by 2. That remainder will always be 1. If it is even, the remainder, when dividing by 2 will be 0. We can use that to check if a number is odd or even. Given a numeric variable, myNum : If myNum % 2 == 1  myNum is odd If myNum % 2 == 0  myNum is even

Some Math methods The Math.round() method rounds off floating point numbers mathematically to an integer value – Math.round(89.001) results in 89 – Math.round(89.678) results in 90 The Math.floor() method truncates the decimal part of any floating point number – Math.floor(89.001) results in 89 – Math.floor(89.678) results in 89 The Math.ceil() method rounds any floating point number up to the next integer – Math.ceil(89.001) results in 90 – Math.ceil(89.678) results in 90

5.2 To Continue or Not to Continue?

The break statement The break statement is essential to a switch structure. It can also be used in a loop if necessary but often creating a compound condition can do the same thing. The general pseudocode for a possible use of the break statement is given. In this situation a user can continue shopping until a maximum amount is spent. Then the user should be informed that further purchases will increase shipping charges. Declare and initialize variables Start a while loop Prompt the user for the item desired and how many will be bought Use a switch to identify the cost of that item, calculate cost of the number of that item ordered, keep a running sum of the total cost. Check if total cost puts the user over the max allowed – If this is true, break out of the loop and inform the customer that another item will increase the shipping cost Continue with the loop until the customer is done shopping

The continue Statement The continue statement allows you to skip an iteration in a loop once (or more, depending on the conditions) but return and complete more iterations.

Counting by Threes The following program uses the continue statement to count by threes: 1. function getThrees() 2. { 3.for (var i = 0; i <= 100; i++) 4.{ 5.if ((i/3) != parseInt(i/3)) 6.{ continue; } 7.document.write(i + " "); 8.} 9. } Output will be:

5.3 Nested for Loops

Desk Checking Most important when programs become complex Use pencil and paper to go through the program, line by line Write down the value of each variable at each line and any output

Example Pass value of x value of y value of z output Outer pass 1100 Inner pass = 2 Inner pass = 5 Inner pass = 8 loop ends,test fails1108 Outer pass Inner pass = 3 Inner pass = 6 Inner pass = 9 loop ends, test fails2109 Outer pass Inner pass = 4 Inner pass = 7 Inner pass = 10 loop ends, test fails310

Which Way Should Loops Be Nested? No clear answer to this question. You can nest a pre-test loop inside a post-test loop Or nest a for loop inside a while loop or nest several while loops inside a for loop or any other combination. The programming problem that you are faced with may determine how you write the code. If there is no clear reason to select one option over another, the choice is yours.

5.4 Drawing Shapes and Patterns

Drawing A Square

Drawing A Rectangle

Drawing A Right Triangle

Mouse Events: Creating a Rollover In this example the JavaScript is within the HTML page, not in the section. All the code is on lines Line 10 identifies the element we are changing with id = "photo".

If you enter and run this code, you will first see: If you run your mouse over the wizard you will see: And, finally, if you move your mouse off the image, you will see:

More Mouse Events AttributeValueDescription: what it does onclickJavaScriptwhat happens when the mouse is clicked ondblclickJavaScriptwhat happens when the mouse is double-clicked onmousedownJavaScriptwhat happens when a mouse button is pressed onmousemoveJavaScriptwhat happens when the mouse pointer moves onmouseoutJavaScriptwhat happens when the mouse pointer moves off an element onmouseoverJavaScriptwhat happens when the mouse pointer moves over an element onmouseupJavaScriptwhat happens when the mouse button is released