Chapter 5 Looping. At a Glance. Overview. Objectives. Quick Quizzes. Additional Resources. Key Terms



Similar documents
JavaScript: Control Statements I

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

Two-way selection. Branching and Looping

Visual Logic Instructions and Assignments

Writing Control Structures

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Section 1.4 Place Value Systems of Numeration in Other Bases

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Notes on Assembly Language

Python Loops and String Manipulation

While Loop. 6. Iteration

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

The While Loop. Objectives. Textbook. WHILE Loops

Total Quality Management (TQM) Quality, Success and Failure. Total Quality Management (TQM) vs. Process Reengineering (BPR)

Lecture 2 Notes: Flow of Control

UEE1302 (1102) F10 Introduction to Computers and Programming

14:440:127 Introduction to Computers for Engineers. Notes for Lecture 06

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

River Dell Regional School District. Computer Programming with Python Curriculum

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

In this Chapter you ll learn:

LOOPS CHAPTER CHAPTER GOALS

CHRIS User Guide: Dated Information and Date Tracking

Computer Programming I

Iteration CHAPTER 6. Topic Summary

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Database Programming with PL/SQL: Learning Objectives

Course Number: IAC-SOFT-WDAD Web Design and Application Development

Exercise 4 Learning Python language fundamentals

Flowchart Techniques

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Object Oriented Software Design

Decimal Number (base 10) Binary Number (base 2)

Pseudo code Tutorial and Exercises Teacher s Version

CGN Computer Methods

Computer Programming I

Chapter 7: Software Development Stages Test your knowledge - answers

Object Oriented Software Design

A simple algorithm with no simple verication

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix:

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Citrix EdgeSight for Load Testing User s Guide. Citrix EdgeSight for Load Testing 3.8

Curriculum Map. Discipline: Computer Science Course: C++

Chapter 24 - Quality Management. Lecture 1. Chapter 24 Quality management

Colored Hats and Logic Puzzles

WebSphere Business Monitor

Lecture 1 Introduction to Java

Paper Creating Variables: Traps and Pitfalls Olena Galligan, Clinops LLC, San Francisco, CA

Java Interview Questions and Answers

Programming and Software Development (PSD)

C++ INTERVIEW QUESTIONS

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Custom Javascript In Planning

IVR PARTICIPANT MANUAL

Programming Lists of Data

Citrix EdgeSight for Load Testing User s Guide. Citrx EdgeSight for Load Testing 2.7

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

A GUIDE TO PROCESS MAPPING AND IMPROVEMENT

71M6521 Energy Meter IC. Real Time Clock Compensation. The Challenge. The RTC in the 71M6521D/F. Theory of Operation APPLICATION NOTE

MACHINE ARCHITECTURE & LANGUAGE

Oxford Learning Institute University of Oxford

CSC 221: Computer Programming I. Fall 2011

Euler s Method and Functions

Demonstrating a DATA Step with and without a RETAIN Statement

- User input includes typing on the keyboard, clicking of a mouse, tapping or swiping a touch screen device, etc.

Module 10. Coding and Testing. Version 2 CSE IIT, Kharagpur

Unit 6. Loop statements

Java Program Coding Standards Programming for Information Technology

Chapter 12 Programming Concepts and Languages

FIREWALL CLEANUP WHITE PAPER

Python Programming: An Introduction To Computer Science

(Refer Slide Time: 00:01:16 min)

PHP Tutorial From beginner to master

20 CODE CHALLENGES. GCSE (9 1) Computer Science GCSE REFORM. February 2015

6.080/6.089 GITCS Feb 12, Lecture 3

What Is Recursion? Recursion. Binary search example postponed to end of lecture

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

GCE. Computing. Mark Scheme for January Advanced Subsidiary GCE Unit F452: Programming Techniques and Logical Methods

#820 Computer Programming 1A

PA2: Word Cloud (100 Points)

WAYNESBORO AREA SCHOOL DISTRICT CURRICULUM INTRODUCTION TO COMPUTER SCIENCE (June 2014)

5 Arrays and Pointers

Testing and Inspecting to Ensure High Quality

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

SAS Certified Base Programmer for SAS 9 A SAS Certification Questions and Answers with explanation

EXCEL VBA ( MACRO PROGRAMMING ) LEVEL SEPTEMBER AM-5.00PM MENARA PJ@AMCORP PETALING JAYA

Problem Solving Basics and Computer Programming

Transcription:

Chapter 5 Looping At a Glance Overview Objectives Quick Quizzes Additional Resources Key Terms

Lecture Notes Overview Chapter 5 covers loops in detail. Students learn the advantages of looping. They also learn to use a while loop and to control loops with sentinel values. Common loop mistakes are covered in detail. Students are also introduced to the for loop, the do while loop, and the do until loop. Finally, students learn how to use nested loops and how to accumulate totals using loops. Chapter Objectives In this chapter, your students will learn about: The advantages of looping Using a loop control variable Nested loops Avoiding common loop mistakes Using a for loop Common loop applications Understanding the Advantages of Looping 1. Understand the advantages of using loops in a program. 2. Describe examples in which looping might be required, such as processing payroll records. Using a Loop Control Variable 1. I the term loop control variable. 2. Explain the three actions required for a loop to end correctly. 3. Note that a loop s repetition is controlled with either a counter or a sentinel value. Using a Definite Loop with a Counter 1. Understand and explain the term definite loop. A definite loop is also called a counted or counter-controlled loop. 2. Walk through the code example in Figure 5-3, pointing out the loop control variable and explaining how it is used to control the number of repetitions of the loop.

Walk through the statement count = count + 1 to explain that the variable count on the right side holds the previous value of count, whereas the variable count on the left side holds the new value of count. 3. Understand how to control a loop by incrementing or decrementing control variables. 4. Understand how the example in Figure 5-3 could be controlled by decrementing the control variable. 5. Discuss the concept of a counter. 6. Although many loops start with the counter set to zero, it is not required to do so. Using an Indefinite Loop with a Sentinel Value 1. Understand what an indefinite loop is. 2. Walk through the example in Figure 5-4, pointing out how the loop is controlled by user input. The sentinel value used to control a loop can be a variable whose value may change prior to executing the loop itself. Understanding the Loop in a Program s Mainline Logic 1. Walk through the itemized list on page 190, which describes the three steps that must occur in every loop. Understand the differences between loop counters and other types of loop control variables. Understand situations in which one type of loop control would be preferable to another. Quick Quiz 1 1. What are the three steps found in every loop? Answer: initialize the loop control variable, compare the loop control variable, alter the loop control variable 2. When can a structured loop be exited? Answer: at the comparison test 3. Another term for counting down is. Answer: decrementing Nested Loops

1. Understand the concept of nested loops. Remind students that a block of statements can be used anywhere that it is legal to use a single statement. 2. Understand the terms inner and outer loop. Like nested if statements, the nested inner loop must be completely contained within the outer loop. 3. Understand the use of separate counters to control each loop. 4. Understand that the number of times a loop executes may depend on a constant or on a variable that changes, and walk through the example in Figure 5-8. Avoiding Common Loop Mistakes 1. State the four most common loop mistakes (listed on page 196). Mistake: Neglecting to Initialize the Loop Control Variable 1. An uninitialized loop control variable may contain garbage that would cause the loop to fail to execute or to execute an incorrect number of times. 2. Walk through the example in Figure 5-10. Mistake: Neglecting to Alter the Loop Control Variable 1. The loop control variable s value is the key factor in determining how many times the loop will execute. 2. Understand the possibility of an infinite loop, and walk through the example in Figure 5-11. Mistake: Using the Wrong Comparison With the Loop Control Variable 1. Walk through the sample of code in Figure 5-12, and ensure you understand the difference in the results produced in this version from the one in Figure 5-9. Mistake: Including Statements Inside the Loop that Belong Outside the Loop 1. While this error may not actually cause incorrect results, it potentially degrades the performance of the program. 2. Walk through the examples in Figures 5-13 and 5-14.

Although today s computers are extremely fast, if you are processing hundreds of thousands or millions of records, performing unnecessary statements for each record will have a measurable impact on job completion time. Using the for Loop 1. Undertand the for statement as a structure for definite loops. 2. Understand the three actions that are performed automatically within the for statement. The loop control variable is handled automatically by the for statement, there is less code in a for loop. 3. Understand the sample for loop code on page 206 in detail. 4. Walk through the bulleted list of tasks accomplished by the for loop on page 206. 5. Understand the conditions that suggest the use of a for loop. 6. Understand the use of variables for the increment, starting, and ending values of a for loop. 7. Know what a step value is. Quick Quiz 2 1. If the loop control variable is not altered within the loop, what will happen? Answer: An infinite loop will occur. 2. If the loop control variable is not initialized, what may happen? Answer: The loop may not execute at all or may execute an incorrect number of times. 3. In a for loop, is the condition tested before or after the loop body is executed? Answer: before Common Loop Applications 1. Loops are frequently used to accumulate totals and validate data. Using a Loop to Accumulate Totals 1. Understand the concept of a summary report or summary section on a report. 2. Understand the concept of an accumulator variable.

You could obtain a total for a variable by going back through the entire set of data a second time, but it would be very inefficient to do so. We can build the running total as we go, using an accumulator variable instead. 3. Understand the importance of initializing the accumulator variable. 4. Walk through the example in Figure 5-17. Using a Loop to Validate Data 1. Understand that validating data ensures that it falls within an acceptable range. 2. Walk through the bulleted list of possible actions on page 211. 3. Understand the error in logic in the example in Figure 5-18, and show how to correct the error in Figure 5-19. Limiting a Reprompting Loop 1. Understand the importance of limiting the number of iterations of a reprompting loop. 2. A programmer can choose to force a value rather than repeat a reprompting loop too many times. Validating a Data Type 1. Understand the concept of checking data types using built-in methods such as isnumeric(), ischar() and iswhitespace(). Note that many programs accept user data as a character string and then attempt to convert it to the appropriate data type. 2. Review the example in Figure 5-21. Validating Reasonableness and Consistency of Data 1. Understand the importance of performing validation whenever possible to avoid the use of invalid and inconsistent data. Quick Quiz 3 1. You have an outer for loop that will execute five times, with an inner for loop that will execute three times each time it is entered. How many times in total will the inner for loop execute? Answer: 15 (three times in each of the five iterations of the outer loop) 2. A report that contains only totals and other overall statistical information is called a(n) report. Answer: summary

3. A variable that is used to hold a running total is called a(n) variable. Answer: accumulator Additional Resources 1. Structured programming and looping with JavaScript: http://javascript.about.com/library/blstruc3.htm 2. For loops compared in several languages: http://en.wikipedia.org/wiki/for_loop 3. Loops and other structures: www.felgall.com/struc.htm 4. Counter-controlled loops and sentinel-controlled loops: http://cis.stvincent.edu/html/tutorials/swd/basic/patterns.html Key Terms Accumulator a variable that you use to gather or accumulate values. Counted loop or counter-controlled loop a loop whose repetitions are managed by a counter. Counter any numeric variable you use to count the number of times an event has occurred. Decrementing a variable is decreasing it by a constant value, frequently 1. Defensive programming a technique with which you try to prepare for all possible errors before they occur. Definite loop the number of repetitions is a predetermined value. for statement, or for loop can be used to code definite loops. The for statement contains a loop control variable that it automatically initializes, evaluates, and increments. Forcing override incorrect data by setting it to a specific value. GIGO ( garbage in, garbage out ) if your input is incorrect, your output is worthless. Incrementing a variable is adding a constant value to it, frequently 1. Indefinite loop one for which you cannot predetermine the number of executions. Inner loop contained within another when loops are nested. Loop control variable a variable that determines whether a loop will continue. Nested loops occur when a loop structure exists within another loop structure. Outer loop contains another when loops are nested. Step value a number you use to increase a loop control variable on each pass through a loop. Stub a method without statements that is used as a placeholder. Summary report lists only totals, without individual detail records. Validate data make sure data items are meaningful and useful. For example, you ensure that values are the correct data type or that they fall within an acceptable range.