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.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Introduction to Computing Science and Programming I
CS0004: Introduction to Programming Repetition – Do Loops.
Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the.
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.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
5.05 Apply Looping Structures
CHAPTER 6 Loop 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:
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?
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
CS 101 Test 2 Study Guide Acronyms RAD - Rapid Application Development IDE - Integrated Development Environment GUI - Graphical User Interface VB - Visual.
Introduction to Problem Solving and Control Statements.
JavaScript, Fourth Edition
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Review while loops Control variables Example Infinite loop
Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Controlling Program Flow with Looping Structures
Controlling Program Flow with Decision Structures.
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()
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Chapter 6 Controlling Program Flow with Looping Structures.
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.
UNIT 5 Lesson 15 Looping.
Visual Basic 6 (VB6) Data Types, And Operators
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
The switch Statement, and Introduction to Looping
3rd prep. – 2nd Term MOE Book Questions.
Chapter 5: Loops and Files.
3rd prep. – 2nd Term MOE Book Questions.
Logical Operators and While Loops
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Repetition Control Structure
Do … Loop Until (condition is true)
Introduction to Problem Solving and Control Statements
Chapter 6: Repetition Statements
Logical Operators and While Loops
Prepared By: Deborah Becker
Repetition Statements (Loops) - 2
statement. Another decision statement, , creates branches for multi-
REPETITION Why Repetition?
Presentation transcript:

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

Loop Blocks Loops have 4 parts 1.a beginning 2.an end 3.body 4.test (Boolean)

Two Types of Loops 1.When you know how many times to execute (definite) 2.When you keep executing until the user says to stop (indefinite)

Do While..Loop Do while ( ) do something do something Want to do it again? Loop Notice that the test is at the beginning of the loop. This means that the commands in the loop may never be executed!

Do.. Loop While Do do something do something Want to do it again? Loop While (Boolean expression) Notice that the test is at the end of the loop. This means that the commands in the loop must be executed at least once.

Some Problems with Loops " off by one” error no exit condition (infinite loop - use Control-Break to stop!)

Example 1 intCount = 1 Do while (intCount < 10) intCount = intCount + 1 Loop MsgBox Str(intCount)

Example 2 intCount = 1 Do intCount = intCount + 1 Loop While (intCount < 10) MsgBox Str(intCount)

Example 3 intCount = 1 Do while (intCount <= 10) intCount = intCount + 1 Loop MsgBox Str(intCount)

Example 4 intCount = 1 Do intCount = intCount + 1 Loop While (intCount <= 10) MsgBox Str(intCount)

Example 5 intCount = 1 Do while (intCount > 10) intCount = intCount + 1 Loop MsgBox Str(intCount)

Example 6 intCount = 1 Do intCount = intCount + 1 Loop While (intCount > 10) MsgBox Str(intCount)

Class Exercise Write a program that allows the user to input an integer into a text box and test the integer to see if it is a prime number. Prime numbers are numbers that are only divisible by 1 and themselves. After clicking a Check button, the program uses a message box to tell the user if the number is Prime or Not Prime

The Interface

The Objects frmPrime- the form txtInput- text box for input cmdCheck cmdEnd lblEnterIntegerLabel

Algorithm Start "the number at 1“ Keep Looping increment "the number" While integer not divisible by "the number"

Code Dim intTestNum, intDivisor As Integer intTestNum = Val(txtInput.Text) intDivisor = 1 Do intDivisor = intDivisor + 1 Loop While (intTestNum Mod intDivisor <> 0)

Is it Prime? If (loop ended when intDivisor = intTestNum) the number is prime else its not

VB Code if (intDivisor = intTestNum) then MsgBox "It is Prime" Else MsgBox "It is NOT Prime" End If

Error Checking The main Do loop does not have to be executed if intTestNum <= 1 If < 1 prime NOT defined If = 1it is Prime by definition

Nesting Blocks We will place the main DO loop inside an If..Then so that it is ONLY executed if intTestNum > 1. Otherwise we will give an error message If ("the test number" > 1) execute the loop Else give an error message

VB Code If (intTestNumber > 1) then Do intDivisor = intDivisor + 1 Loop While (intTestNum Mod intDivisor <> 0) Else MsgBox "Please Enter a positive integer" End If

Using Input Boxes InputBox Command InputBox, InputBox Function strName = InputBox (, ) Note the use of parentheses in function!

Homework Modify Review 2 (page 6-2) to have the user to input the integer using a horizontal scroll bar rather that a text box Do Review 4, a & b (page 6-8) Exercises 1, 2, and 3 (a & b)