CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
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.
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
For Next Looping My first Looping Structure. For…Next FOR counter_variable = initial_value TO end_value STEP increment Statement-1 Statement-2 ……. Statement-n.
1 Pertemuan 06 Repetition Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
CP Week 7 Looping constructs Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code.
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:
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
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?
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
Repetition Chapter 7. Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
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.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
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.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Repetition Statements
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
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.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
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.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Higher Computing Software Development -So Far- 5/10/10.
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
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
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.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
BO65: PROGRAMMING ITERATION 1. Starter  Write down what you think the code below outputs on screen: Dim intCounter as integer intCounter = 0 Do while.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Chapter 5- Control Structures: Part 2
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.
Problem Solving and Control Statements: Part 2
Lists, Loops, Validation, and More
Control Structures: Part 2
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Introducing Do While & Do Until Loops & Repetition Statements
Control Structures: Part 1
Loop Control Structure.
حلقات التكرار.
For loops intCount = 1 inCount = intCount + 1 True intCount
Case & Repetitive Statements
Prepared By: Deborah Becker
ASP control structure BRANCHING STATEMENTS
Presentation transcript:

CSC 162 Visual Basic I Programming

Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit condition is tested after the body is executed at least once

Pretest Loops While/Wend or Do While/Loop Do Until/Loop For/Next

For Control_Variable = Initial_Value To Final_Value [Step Increment] Body_of_Loop Next Control_Variable Instead of having separate incrementing and condition checking statements, a For/Next loop combines both into the For statement. The Step is optional; it specifies the amount by which the Control_Variable is incremented. If left unspecified, it is assumed to be +1. While technically any data type is usable for the Control_Variable, it is best to use Integer data types. Strange things can happen otherwise. (See Example 3.)

Example 1 Dim intCounter As Integer Dim intSquare As Integer For intCounter = 0 To 10 intSquare = intCounter ^ 2 Print intCounter & " squared is " & intSquare Next intCounter Example 1 Execution

Example 2 Dim intCounter As Integer Print "Counting down from 21 by 3's" For intCounter = 21 To 14 Step -3 Print intCounter Next intCounter Example 2 Executionple 2 Execution

Example 3 Dim sngCounter As Single Print "Am I really counting by tenths?" For sngCounter = 0 To 1 Step 0.1 Print sngCounter Next sngCounter Example 3 Execution

Posttest Loops Do/Loop While Do/Loop Until

Do/Loop While Do Body_of_Loop Loop While Condition As long as the Condition is satisfied, the loop will continue to execute. Once the Condition is false, the code will resume execution after the Loop While statement.

Example Dim intCounter As Integer Dim intSquare As Integer intCounter = 0 Do intSquare = intCounter ^ 2 Print intCounter & " squared is " & intSquare intCounter = intCounter + 1 Loop While intCounter <= 10

Do/Loop Until Do Body_of_Loop Loop Until Condition As long as the Condition is not satisfied, the loop will continue to execute. Once the Condition is true, the code will resume execution after the Loop Until statement.

Example Dim intCounter As Integer Dim intSquare As Integer intCounter = 0 Do intSquare = intCounter ^ 2 Print intCounter & " squared is " & intSquare intCounter = intCounter + 1 Loop Until intCounter > 10

The Semicolon ; intNum = 2 Print intNum & " "; Print (intNum ^ 2) & " "; Print (intNum ^ 3) & " "; Print "That's it!" Output: That's it! The semicolon keeps output on the same line. By itself, the Print statement outputs a single line.

What does this code produce? Dim intOuterCounter As Integer Dim intInnerCounter As Integer For intOuterCounter = 1 To 10 Step 2 For intInnerCounter = intOuterCounter To 1 Step -1 Print intInnerCounter; Next intInnerCounter Print Next intOuterCounter