COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Looping Structures: Do Loops
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
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.
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 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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
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:
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?
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
Tutorial 6 The Repetition Structure
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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
259 Lecture 11 Spring 2013 Advanced Excel Topics – Loops.
Introduction to Problem Solving and Control Statements.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
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.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
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()
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.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Looping Structures. A B dialog box that pops up and prompts the user for input Text area that pops up and prompts the user for to wait while it gets.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
UNIT 5 Lesson 15 Looping.
REPETITION CONTROL STRUCTURE
Lists, Loops, Validation, and More
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.
Arrays, For loop While loop Do while loop
Chapter (3) - Looping Questions.
Chapter 6: Repetition Statements
Case & Repetitive Statements
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.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

COMPUTER PROGRAMMING I 5.05 Apply Looping Structures

Objective/Essential Standard Essential Standard 5.00 Apply Programming and Conditional Logic (23%) Indicator 5.05 Apply Looping Statements. (8%)

VISUAL BASIC Looping Structures

Repeated Actions Have you ever performed the same action over and over? For example: As long as it rains I am going to sing the same song. When the song finishes I am going to start over again. Programmers can code repeated actions into their programs. This is called a loop. Each time the loop runs is called an iteration.

Types of Loops These are the four types of loops. 1. Do While 2. Do…Loop While 3. For…Next 4. For Each…Next  Covered later

Pretest and Posttest When dealing with loops there are two different ways to test a condition. Pretest  Test the condition BEFORE the loop runs. If the condition is false the loop will not execute.  A pretest loop only executes if the condition is true for at least one time. Posttest  Run the loop one time then test the condition- if the condition is false the loop will then terminate.  A posttest loop will ALWAYS execute at least one time.

Do While Loop

Do While…Loop Syntax Do While condition Statements Loop This form executes only if condition is true, therefore if condition is false, the loop does not execute. This is a pre-test loop. Checks the condition first

Do While…Loop Example sum = 20 Do While sum < 10 sum += 2 Loop The statement does not iterate at all because sum is initially greater than 10. Is sum less than 10? No, so the loop does not “iterate”

Do While Example Add all even numbers 1 to 25 - skip all odd numbers. Dim i As Integer = 0 Dim intResult As Integer = 0 Do while i < 25 intResult = intResult + i‘ updates intResult by i i += 2‘ increases i by 2 Loop Accumulator Counter

Using Do While to Validate Input Dim intNum As Integer = -1 Dim strInputNum As String = “” strInputNum = InputBox("Enter a Number between 1 & 10", "Number") Do While strInputNum = Nothing Or (Convert.ToInt32(strInputNum) 10) MessageBox.Show("Enter a Number between 1 & 10") strInputNum = InputBox("Enter a Number between 1 & 10", "Number") Loop MessageBox.Show("You entered a number between 1 & 10. Your number was " & strInputNum)

VISUAL BASIC Do…Loop While

The looping structure that evaluates a condition after executing a loop once. Syntax Do Statements Loop While condition * Statements is the loop and is executed at least once. * Condition is a Boolean expression used to determine if the loop is to be repeated. condition is true  repeats Checks the condition after it has looped at least once.

Do…Loop While The looping structure that executes a set of statements as long as a condition is true. The condition is a Boolean expression. Evaluates to T or F Executes at least once. The loop below iterates (repeats) while sum is less than 10: sum = 0; Do sum =sum + 2 Loop While sum < 10 Is sum less than 10? No, stop. Yes, continue to loop

Do…Loop While Example Write a program that adds all numbers up from 1 to 50. Dim i As Integer = 0 Do intResult = intResult + i‘accumulator i += 1‘counter Loop While (i <= 50) lblResult.Text = intResult.ToString()

Using Do..Loop While to Validate Input Dim intNum As Integer = -1 Dim strInputNum As String = “” Do MessageBox.Show("Enter a Number between 1 & 10") strInputNum = InputBox("Enter a Number between 1 & 10", "Number") Loop While (strInputNum = Nothing Or (Convert.ToInt32(strInputNum) 10)) MessageBox.Show("You entered a number between 1 & 10. Your number was " & strInputNum)

Wrap Up This lesson introduced the different types of loops and why loops are so useful in programming. For more information us/library/ezk76t25.aspx us/library/ezk76t25.aspx