CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Loops.

Slides:



Advertisements
Similar presentations
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Introducing JavaScript Loops.
Advertisements

Programming Logic and Design Eighth Edition
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Objectives In this chapter, you will learn about:
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Programming Logic and Design Fifth Edition, Comprehensive
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Selection Structures.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
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.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Pendalaman Materi Conditional dan Repetition (Pengulangan)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Design Document Sample Given two concentrated circles with different radii, calculate the area which falls inside the big circle but outside the small.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
Computer Science 101 While Statement.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Control Structure Senior Lecturer
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Alternate Version of STARTING OUT WITH C++ 4th Edition
A LESSON IN LOOPING What is a loop?
Programming Concepts and Database
Chapter 4: Repetition Structures: Looping
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Loops

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Goals By the end of this lecture, you should understand... When to use a pre-test loop.When to use a pre-test loop. When to use a post-test loop.When to use a post-test loop. When to use a counter-controlled (for) loop.When to use a counter-controlled (for) loop. How to use relational operators.How to use relational operators.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Introducing Loops A loop is a programming structure that executes repeatedly until a given condition is met.A loop is a programming structure that executes repeatedly until a given condition is met. We categorized loops into two basic groups:We categorized loops into two basic groups: –Loops that depend on some TRUE/FALSE condition (pre-test & post-test loops) –Loops that depend on reaching a maximum number of iterations or “counts” (counter-controlled loops)

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Basic Loop Structure Repeat Prompt for and input a number, Num Write num Until Num = 0 Write “Done” –The body of the loop executes repeatedly until the user enters a 0. At that point the loop exits; the statement that follows the loop then executes. –Note the indentation of code, which makes it easier to read.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Relational Operators Conditions that determine whether a loop is reentered or exited are usually constructed with relational operators.Conditions that determine whether a loop is reentered or exited are usually constructed with relational operators. = equal to <> not equal to < less than > greater than <= less than or equal to >= greater than or equal to

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Post-test Loops When encountering a post-test loop, a computer tests the condition after the loop body executes.When encountering a post-test loop, a computer tests the condition after the loop body executes. Programmers use post-test loops when they want the loop body to execute at least once.Programmers use post-test loops when they want the loop body to execute at least once.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Post-test Loop Example Repeat Prompt “Who will win this year’s Superbowl?” Input userAnswer Until userAnswer = “Chicago” Write “You gain wisdom.”

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Pre-Test Loops When encountering a pre-test loop, a computer tests the condition before the loop body executes.When encountering a pre-test loop, a computer tests the condition before the loop body executes. Programmers use pre-test loops when they are unsure if the loop might ever need to execute at all.Programmers use pre-test loops when they are unsure if the loop might ever need to execute at all.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Pre-test Loop Example Input Password While Password <> “parrothead” Write “What is the password?” Input Password End While Write “Safe unlocked!”

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Trace (Walk through) a Loop It is impossible to see what a loop is doing without tracing it to see how it works. Suppose the user enters 3, 1, -1.It is impossible to see what a loop is doing without tracing it to see how it works. Suppose the user enters 3, 1, -1. Input Number While Number > 0 Write Number ^ 2 Input Number End While NumberOutput 3 9 1

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science For Loops Because they are so common, most programming languages include a distinct structure for easily building them, called a for loop.Because they are so common, most programming languages include a distinct structure for easily building them, called a for loop. For Counter = InitialValue Step Increment To LimitValue Body of loop End For

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science For Loop Example 1 For Count = 1 Step 1 To 5 Write Count, Count ^ 2 End For CountOutput

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science For Loop Example 2 For N = 1 Step 2 To 20 Write N Write N End For NOutput135 …1719

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Data Validation Users may enter erroneous data by mistakeUsers may enter erroneous data by mistake Programs should include statements that check, or validate that the value is in a proper range, and request the user to re-enter invalid data. Write “Enter a positive number ->” Input Num While Num ” Input Num End While … the program continues with a valid number in the variable NumPrograms should include statements that check, or validate that the value is in a proper range, and request the user to re-enter invalid data. Write “Enter a positive number ->” Input Num While Num ” Input Num End While … the program continues with a valid number in the variable Num

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Data Validation Example Write “Enter a positive number ->” Input Num While Num ” Input Num End While … the program continues with a valid number in the variable Num

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Questions?

Resources Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.Venit, Stewart. Extended Prelude to Programming: Concepts and Design. Scott/Jones, Inc., 2002.