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.

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic th Edition
Programming Logic and Design Eighth Edition
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
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.
Types of LOOP Structures Do While ……. Loop Do Until …… Loop For …… Next loop.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
An Introduction to Programming with C++ Fifth Edition
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 6 - Visual Basic Schneider
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
5.05 Apply Looping Structures
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Chapter 12: How Long Can This Go On?
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Tutorial 6 The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
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.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with Looping Structures
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
© 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,
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
UNIT 5 Lesson 15 Looping.
REPETITION CONTROL STRUCTURE
Programming Logic and Design Fourth Edition, Comprehensive
Lists, Loops, Validation, and More
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Outline Altering flow of control Boolean expressions
CIS 16 Application Development Programming with Visual Basic
Presentation transcript:

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  Selection / Decisions  Repetition / Looping

 Visual Basic repetition or looping statements  Do While ▪ Repeat/Loop while true ▪ Often called pretest loop (also has posttest form)  Do Until ▪ Repeat/Loop until true ▪ Often called posttest loop (also has pretest form)  For...Next ▪ Repeat/Loop a specified number of times

 Do While loop: repeat while test expression is true  The test expression is evaluated (pretest)  When true, the statement(s)are executed  The previous 2 steps are repeated as long as the expression evaluates to true  When false, the loop ends and the statement(s) are not executed expression statement(s) False True

Do While expression statement(s) Loop  Syntax explanation:  Do, While, and Loop are keywords  Do While statement marks the beginning of the loop and the Loop statement marks the end  The statement(s) to repeat are found between these and called the body of the loop  Expression – True/False value, variable, function call or expression that serves as test for repetition

intCount = 1 Do While (intCount <= 10) MsgBox (“The current number is: “ & intCount) intCount = intCount + 1 Loop intNumGrades = 0, intSumGrades = 0 strInput = InputBox(“Enter a grade(or -1 to stop)”) Do While (strInput <> “-1”) intSumGrades = intSumGrades + Val(strInput) intNumGrades = intNumGrades + 1 strInput = InputBox(“Enter a grade(or -1 to stop)”) Loop decAverage = intSumGrades / intNumGrades MsgBox(“Your average is: “ & decAverage)

 A loop must have some way to end itself  Something within the body of the loop must eventually force the test expression to false  In the previous example  The loop continues to repeat  intCount increases by one for each repetition  Finally (intCount <= 10) is false and the loop ends  If the test expression can never be false, the loop will continue to repeat forever  This is called an infinite loop

 Counter-controlled loops  repeat a specific number of times see intCount from the previous example  Counters are typically initialized before the loop begins intCount = 1  The counter is then modified in the body of the loop intCount = intCount + 1  Event-controlled loops  repeat until something happens in the loop body to change the value of loop control variable  Events can be: User Input, Expression results, Function results, etc.

 Sentinel Value  For either control type, the test expression will usually determine when to end the loop by making some comparison to a Sentinel value  The sentinel value serves as the test value or comparison criteria for ending the loop ▪ In counted loops this is usually the number of times to loop ▪ In event controlled loops this is the state or value that the event variable is compared to in the test expression

 Counter  A numeric variable that keeps track of or counts: number of loop repetitions, items that have been processed, or some other occurrence within a loop see intNumGrades from the previous example  Accumulator  A numeric variable that is used to hold a sub-total that is computed during multiple passes through a loop see intSumGrades from the previous example

 Do Until loop: repeat until test expression is true  The statement(s)are executed  The test expression is evaluated (posttest)  When false, the previous 2 steps are repeated until the expression evaluates to true  When true, the loop ends and the statement(s) are not executed again expression statement(s) False True

Do statement(s) Loop Until expression  Syntax explanation:  Do, Loop, and Until are keywords  Do statement marks the beginning of the loop and the Loop Until statement marks the end  The statement(s) to be repeated are found between these and called the body of the loop  Expression – True/False value, variable, function call or expression that serves as test for repetition

intCount = 1 Do MsgBox (“The current number is: “ & intCount) intCount = intCount + 1 Loop Until (intCount > 10) intNumGrades = 0, intSumGrades = 0 Do strInput = InputBox(“Enter a grade(or -1 to stop)”) If (strInput <> “-1”) Then intSumGrades = intSumGrades + Val(strInput) intNumGrades = intNumGrades + 1 End If Loop Until (strInput = “-1”) decAverage = intSumGrades / intNumGrades MsgBox(“Your average is: “ & decAverage)

 Previous Do While loops are in pretest form  The expression is evaluated first  Then the loop body is executed only if expression is true  The body of a pretest loop may not be executed at all (if initial value of expression is false)  Previous Do Until loops are in posttest form  The body of the loop is executed first  Then the expression is evaluated  The body of a posttest loop is executed at least once (even if initial value of expression is true)

 A Do While loop  Repeats as long as its test expression is true  Ends when its test expression becomes false  Usually a pretest loop, but also has a postest form:  A Do Until loop  Repeats as long as its test expression is false  Ends when its test expression becomes true  Usually a posttest loop, but also has a pretest form: Do Until expression statement(s) Loop Do statement(s) Loop While expression

Do strInputPW = InputBox(“Enter Password”) Loop Until (strInputPW = strSavedPW) strInputPW = InputBox(“Enter Password”) Do While (strInputPW <> strSavedPW) strInputPW = InputBox(“Enter Password”) Loop Do While (decBalance < 0) intDep = InputBox(“N egative balance, enter amount to deposit ”) decBalance += decDep Loop

 For Next loop: used for counted loops that repeat a specific number of times  The Counter variable is set initially to the StartValue  After each loop iteration, the Step Value is added to the value of the counter variable. (if there is no step value, 1 is added)  Iteration continues until the EndValue is exceeded Counter > EndValue? statement(s) False True Set Counter to StartValue Increment Counter

For Counter = StartValue To EndValue[Step Value] statement[s] Next [Counter]  Syntax explanation:  For, To, and Next are keywords  Counter – Variable to track/control number of iterations  StartValue is initial value of counter  EndValue is counter value at final iteration  Step (optional) – determines counter increment amount for each iteration of the loop (if not specified the default is +1; if specified can be positive – add or count up, or negative – subtract or count down

For intCount = 1 to 10 Step 1 msgBox (“The current number is: “ & intCount) Next For intCount = 100 to 0 Step -5 msgBox (“The current number is: “ & intCount) Next intSum = 0 intNum = Val(InputBox(“Enter number of grades to avg”)) For intCount = 1 to intNum Step 1 strInput = InputBox(“Enter a grade”) intSum = intSum + Val(strInput) Next decAverage = intSum / intNum MsgBox(“Your average is: “ & decAverage)

 In some cases it is convenient to end a loop before the test condition would end it  The following statements accomplish this  Exit Do (used in Do While or Until loops)  Exit For (used in For Next loops)  Use this capability with caution  It bypasses normal loop termination  Makes code more difficult to debug

Do While (true) strInput = InputBox(“Enter a grade(or -1 to stop)”) If (strInput = “-1”) Then Exit Do End If intSumGrades = intSumGrades + Val(strInput) intNumGrades = intNumGrades + 1 Loop decAverage = intSumGrades / intNumGrades MsgBox(“Your average is: “ & decAv

 The body of a loop can contain any type of VB statements including another loop  When a loop is found within the body of another loop, it’s called a nested loop

strOutput = “” For intTensPlace = 0 to 4 Step 1 For intOnesPlace = 0 to 9 Step 1 strOutput &= (intTensPlace & intOnesPlace & “ “) Next MsgBox(“I can count from 0 to 49: ” & strOutput) For hours = 0 To 24 lblHours.Text = hours For minutes = 0 To 59 lblMinutes.Text = minutes For seconds = 0 To 59 lblSeconds.Text = seconds Next seconds Next minutes Next hours

Have the user input a positive number via input box. Use a validation loop to prompt for input until valid. Display message indicating when valid input is received. Have the user input numbers via inputbox until the value “stop” is entered. Display the sum of all the input numbers. Have the user input an integer. Display the sum of all the integers from 1 through the input integer. Have the user input a yearly deposit amount and a yearly interest rate. Use a loop to determine the min number of years it will take to reach at least 1 million $ (for each year – add dep first, then compute and add int). Display the number of years and the final value. Modify to have the user also enter a number of years and display the final value after that number of years.

Write a VB application to have the user input via textbox an integer from 1 t0 10 (inclusive) to specify the height and base of a right triangle. Display the triangle in a list box using strings made up of the ‘#’ character. Create and display strings - one char at a time - using nested loops. Input:4 Output: # ## ### #### Write a VB application to have the user input a binary number via input box. Use a validation loop ensure the input is a valid binary number. When valid, convert the number to a decimal value (use a function) and report the results via message box.

 Lab 7 and Homework 7  Visual Basic – Looping  See handout for details and due date  Questions?