© 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
CS0007: Introduction to Computer Programming
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Programming with Microsoft Visual Basic th 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.
Repeating Actions While and For Loops
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
An Introduction to Programming with C++ Fifth Edition
1 Loops Loops repeat (iterate) a block of statements for a number of times. A terminating condition tells a loop when to stop iterating (e.g. terminate.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
5.05 Apply Looping Structures
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
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.
Chapter 12: How Long Can This Go On?
Engineering 1020 Introduction to Programming Peter King Winter 2010.
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.
Loops ISYS 350. A Box of Chocolate Repeat this process until box is empty: – Take one chocolate from the box – Eat the chocolate – Box has more chocolate?
© 1999, by Que Education and Training, Appendix A, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Loops ISYS 350. Compute the sum of a list of numbers: Example: 5, 2, 10, 8, 4 Process: Sum= 0 Get a number from the list Sum = Sum + the number Repeat.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with Looping Structures
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
While ( number
Chapter Looping 5. The Increment and Decrement Operators 5.1.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 1 Unit 9 Do Until and For… Next Loops Chapter 5 Lists,
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Programming Fundamentals
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Iteration with While You can say that again.
Chapter (3) - Looping Questions.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Introduction to Computer Science
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:

© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Pre-test Loops condition True False Steps to repeat if True condition False True Steps to repeat if False Do While condition Steps to repeat if condition is true Loop Do Until condition Steps to repeat if condition is false Loop

© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Post-test Loops condition False True Steps to repeat if False condition True False Steps to repeat if True Do Steps to repeat if condition is true Loop While condition Do Steps to repeat if condition is false Loop Until condition

© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach When are conditional loops used? n Execute processing steps repeatedly i One or more times: post-conditional loopsDo Loop body Loop While conditionLoop Until condition i Zero or more times: pre-conditional loops Do While conditionDo Until condition Loop body Loop n Data Validation i Prevent user from moving on until valid value has been entered

© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Pre-test Loop to Find Factorial FactNbr = CInt(txtFactNbr.Text) Factorial = 1 Multiplier = FactNbr Do While Multiplier > 0 Factorial = Factorial * Multiplier Multiplier = Multiplier - 1 Loop FactNbr = CInt(txtFactNbr.Text) Factorial = 1 Multiplier = FactNbr Do Until Multiplier <= 0 Factorial = Factorial * Multiplier Multiplier = Multiplier - 1 Loop What is the minimum number of times the loop will repeat? What happens if txtFactNbr contains zero?

© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Post-test Loop to Play a Game Dealer = 0 User = 0 Do Call PlayGame(Dealer, User) Again = InputBox(“Play again?”) Loop Until Ucase(Again) = “NO” Or Again = “” Dealer = 0 User = 0 Do Call PlayGame(Dealer, User) Again = InputBox(“Play again?”) Loop While Ucase(Again) <> “NO” And Again <> “” What is the minimum number of times the game will be played? What happens if the user says No the first time? What happens if the user says NO? What happens if the user clicks the Cancel button ?

© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach False True Steps to process after loop ends Steps to process repeatedly Count > EndVal? Set Count = StartVal Count = Count + IncrVal Check if done counting Update counter Initialize counter Counting: Pre-Test Conditional Loop

© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach For/Next Loops Three Characteristics of a Counting Loop i Initialize counter i Increment counter in each loop iteration i Test counter before next iteration against final value For Count = StartVal To EndVal Step IncrVal steps to process repeatedly Next Count steps to process after loop ends False True Steps to process after loop ends Steps to process repeatedly Count > EndVal? Set Count = StartVal Count = Count + IncrVal

© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach For Next Loop Example FactNbr = CInt(txtFactNbr.Text) Factorial = 1 For Multiplier = FactNbr To 1 Step -1 Factorial = Factorial * Multiplier Next Multiplier