Loops Counting down from N to 1 in assembly we used JUMP statements to repeat previous instructions thus looping READ LOOP: WRITE SUB decrement JPOSLOOP.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Chapter 04 (Part III) Control Statements: Part I.
CS0004: Introduction to Programming Repetition – Do Loops.
VB PROJECT “PROJECT SAMPLES”. For Next Loops Design a VB program that displays in a picture box the first N multiples of an input integer Input 3 exam.
Repeating Actions While and For Loops
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
CS150 Introduction to Computer Science 1
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
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.
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.
ENGR 112 Decision Structures.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
Nested LOOPS.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Count and add list of numbers From user input and from file.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
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.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CS 100 Introduction to Computing Seminar October 7, 2015.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
VB CSCI130 Instructor: Dr. Imad Rahal LAYEROrder Application SW: Excel & Access2 High-order P.L.: Visual Basic1 Low-order P.L.: Assembly3 System Software:
Chapter 7 Problem Solving with Loops
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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()
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions to be made 3.Loops- actions to be repeated.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
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.
while Repetition Structure
for Repetition Structures
CHAPTER 5A Loop Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
While Loops Chapter 3.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Repetition and Loop Statements
Chapter (3) - Looping Questions.
Loop Strategies Repetition Playbook.
CHAPTER 6: Control Flow Tools (for and while loops)
Repetition Statements (Loops) - 2
The while Looping Structure
EECE.2160 ECE Application Programming
Presentation transcript:

Loops Counting down from N to 1 in assembly we used JUMP statements to repeat previous instructions thus looping READ LOOP: WRITE SUB decrement JPOSLOOP HALT decrement :1

Do Loops Stopping/looping condition but not sure how many iterations good with flags (or sentinels)‏ Come in two forms Do While --- LOOPING CONDITION Dim InputNum As Single InputNum=inputbox(“Enter a number to print, 0 to stop”)‏ Do While InputNum<>0 picResults.Print InputNum InputNum=inputbox(“Enter a number to print, 0 to stop”)‏ Loop What would happen if I don’t read input again from user inside loop? Do Until --- EXIT CONDIITON Dim InputNum As Single InputNum=inputbox(“Enter a number to print, 0 to stop”)‏ Do Until InputNum=0 picResults.Print InputNum InputNum=inputbox(“Enter a number to print, 0 to stop”)‏ Loop Print even numbers between 2 and 100 in a picturebox

Do Loops Dim counter As Integer counter=2 Do While counter <=100 picResults.Print counter counter = counter +2 Loop Dim counter As Integer counter=2 Do Until counter >100 picResults.Print counter counter = counter +2 Loop Same initialization phase for loop variable (Outside loop)‏ Syntactically different exit conditions (Semantically the same)‏ Opposite to one another Same action (Loop body: Do … Loop)‏

Do Loops Program to compute the average for any class exam at CSBSJU Enter any number of grades When the flag (-1) is provided end of input and print average Algorithm + program Show program

Do Loops Program to get the average of any number of grades (enter -1 to exit)‏ Sum = 0, count=0, average=0 grade=input by user While grade <> -1 Add grade to sum Add 1 to count Enter another grade Divide sum by count to obtain the average Print average

Do Loops Private Sub cmdAverageButton_Click()‏ ‘program to compute the average exam score Dim count As Integer Dim grade As Single, sum As Single, avg As Single grade = InputBox (“Enter the first exam score:”, ”Exam Scores”)‏ Do While grade <> -1 sum = sum + grade count = count + 1 grade = InputBox(“Enter another score Type -1 to end.”, “Exam Scores”)‏ Loop avg = sum/count MsgBox “The average is: ” & FormatNumber(avg)‏ End Sub

For Next Loops When we know how many times we need to repeat the loop With a consistent increase or decrease  For Next Loops are better Display values between 1 to 5 (vs. until user inputs -1)‏ Dim CTR As Integer For CTR = 1 to 5 picResults.Print CTR Next CTR After every loop, the loop counter is incremented by 1 (default)‏ Initialization: CTR=1 Exit Condition: CTR>5 Action: Results.Print CTR Combines the initialization and exit conditions into one line Previously initialization was done before loop

For Next Loops After every loop, the loop counter is incremented by 1 (default)‏ Can be changed by specifying steps (display even numbers from 2 to 100)‏ For CTR = 2 to 100 Step 2 picResults.Print CTR Next CTR Can be positive or negative (display even numbers from 100 to 2)‏ For CTR = 100 to 2 Step -2 picResults.Print CTR Next CTR

For Next Loops The steps don’t have to be integers For CTR = 1 to 3 Step.5 picResults.Print CTR Next CTR Suppose we want to display all even numbers between 2 and another number specified by the user Dim CTR as Integer, N As Integer N = txtEndBox.Text For CTR = 2 to N Step 2 picResults.Print CTR Next CTR Design a VB program that displays in a picture box the first N multiples of an input integer