1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.

Slides:



Advertisements
Similar presentations
Repeating Actions While and For Loops
Advertisements

Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Chapter 4 Loops Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.
Chapter 6 - Visual Basic Schneider
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
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.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4: Control Structures: Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
Lecture 14: Control Flow. The break, continue, goto statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 6 - Visual Basic Schneider 1 Chapter 6 Repetition.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
Chapter 6: The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Introduction to Programming Lecture 2 Msury Mahunnah, Department of Informatics, Tallinn University of Technology.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Decision Making and Branching (cont.)
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Introduction to Computers and Programming Lecture 7:
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
Chapter 6 - VB 2008 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
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.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Introduction to Programming Lecture 2
REPETITION CONTROL STRUCTURE
Chapter 5- Control Structures: Part 2
Control Structures: Part 2
Chapter 5: Repetition Structures
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Chapter 6 Control Statements: Part 2
Presentation transcript:

1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice Hall, Pearson Education Inc., 7th Ed. 2008, 6th Ed Liberty J., Learning Visual Basic.NET, O’Reilly, 2002 Any Visual Basic book available in AUBG library Course lecturer: Assoc. Prof. Svetla Boytcheva, PhD

2 INF110 Visual Basic Programming AUBG Spring semester 2011 Lecture 08 Title: Visual Basic (Repetition/Iteration statements 2)

3 Lecture Contents: §Reminder on Decisions/Selections §Reminder on Repetitions/Iterations §Endless Loops §Loops with GOTO §Demo programs §Practical session

4 Iterations Demo programs by J.Liberty, Chapter 06, Plus Practical Session INF110 Visual Basic Programming

5 ReminderOnDecisions/Selections

6 VBasic selection statements include If condition Then statement sequence End If If condition Then statement sequence1 Else statement sequence2 End If

7 VBasic selection statements include If condition1 Then statement sequence1 ElseIf condition2 statement sequence2 ElseIf condition3 statement sequence3 Else statement sequence4 End If

8 VBasic selection statements include Select Case expression Case value1 Block of one or more VB statements Case value2 Block of one or more VB Statements Case value3 Block of one or more VB statements Case value4... Case Else Block of one or more VB Statements End Select

9 ReminderOnIterations/Repetitions

10 Visual Basic supports the following loop structures: Do … Loop While … End While For … Next

11 For i = 1 To 5... Next And its corresponding Do While equivalent i = 1 Do While i <= 5... i = i + 1 Loop

12 (a) For counter=1 to 10 Console.Writeline(counter) Next (b) For counter=1 to 100 Step 10 Console.Writeline(counter) Next (c) For counter=100 to 5 Step -5 Console.Writeline(counter) Next Examples

13 Variations of the Do…Loop control structure: Do … Loop 1/ Pre test loops with While and Until 2/ Post test loops with While and Until

14 Pre test loops with While and Until

15 Do Loops All the variations use the same basic model. A pretest loop is executed either –While condition is True. Uses While reserved word Do While condition statement-block Loop –Until condition becomes True. Uses Until reserved word Do Until condition statement-block Loop

16 Examples I = 1 Do While (I <= 10) Console.WriteLine(I) I = I + 1 Loop How many iterations are to be run? (10) Try it

17 Examples I = 1 Do Until I <= 10 Console.WriteLine(I) I = I + 1 Loop How many iterations are to be run? (0) Try it

18 Examples to achieve the same effect I = 1 Do While (I <= 10) Console.WriteLine(I) I = I + 1 Loop ======================= same effect I = 1 Do Until Not(I <= 10) Console.WriteLine(I) I = I + 1 Loop How many iterations are to be run? (10) Try it

19 Examples to achieve the same effect I = 1 Do While Not(I <= 10) Console.WriteLine(I) I = I + 1 Loop ======================= same effect I = 1 Do Until (I <= 10) Console.WriteLine(I) I = I + 1 Loop How many iterations are to be run? (0) Try it

20 Post test loops with While and Until

21 Do Loops All the variations use the same basic model. A posttest loop is executed either –While condition is True. Uses While reserved word Do statement-block Loop While condition –Until condition becomes True. Uses Until reserved word Do statement-block Loop Until condition

22 Examples I = 1 Do Console.WriteLine(I) I = I + 1 Loop While (I <= 10) How many iterations are to be run? (10) Try it

23 Examples I = 1 Do Console.WriteLine(I) I = I + 1 Loop Until (I <= 10) How many iterations are to be run? (1) Try it

24 Examples to achieve the same effect I = 1 Do Console.WriteLine(I) I = I + 1 Loop While (I <= 10) =================== same effect I = 1 Do Console.WriteLine(I) I = I + 1 Loop Until Not(I <= 10) How many iterations are to be run? (10) Try it

25 Examples to achieve the same effect I = 1 Do Console.WriteLine(I) I = I + 1 Loop While Not(I <= 10) =================== same effect I = 1 Do Console.WriteLine(I) I = I + 1 Loop Until (I <= 10) How many iterations are to be run? (1) Try it

26 Endless loops with While … End While and Do … Loop

27 Endless loops - examples While True statement-block End While

28 Endless loops - comments The endless loops should be avoided. They must be terminated from within the own loop body with an Exit statement While True statement-block if condition Then Exit While statement-block End While

29 Endless loops - examples Do statement-block Loop

30 Endless loops - comments The endless loops should be avoided. They must be terminated from within the own loop body with an Exit statement Do statement-block If condition Then Exit Do statement-block Loop

31 Example 6-9 Creating Loops with Goto –Using Goto

32 Example 6-9 Option Strict On Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 0 repeat: ' the label Console.WriteLine("counterVariable: {0}", counterVariable) ' increment the counter counterVariable += 1 If counterVariable < 10 Then GoTo repeat ' loop implemented: control transferred back to label repeat End If End Sub 'Main End Module

33 Example 6-10 The Do Loop –Using Do While

34 Example 6-10 Option Strict On Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 0 Do While counterVariable < 10 Console.WriteLine("counterVariable: {0}", counterVariable) counterVariable = counterVariable + 1 Loop ' While counterVariable < 10 End Sub 'Main End Module

35 Example 6-11 The Do Loop –Using Do Until

36 Example 6-11 Option Strict On Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 0 Do Until counterVariable = 10 Console.WriteLine("counterVariable: {0}", counterVariable) counterVariable = counterVariable + 1 Loop ' Until counterVariable = 10 End Sub 'Main End Module

37 Example 6-12 The Do Loop –Do … Loop While

38 Example 6-12 Option Strict On Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 100 Do Console.WriteLine("counterVariable: {0}", counterVariable) counterVariable = counterVariable + 1 Loop While counterVariable < 10 End Sub 'Main End Module

39 Example 6-13 The Do Loop –Breaking out of a Do Loop Using Exit Do

40 Example 6-13 Option Strict On Imports System Module Module1 Sub Main( ) Dim counterVariable As Integer = 0 Do Console.WriteLine("counterVariable: {0}", counterVariable) counterVariable = counterVariable + 1 ' test whether we've counted to 9, if so, exit the loop If counterVariable > 9 Then Exit Do End If Loop End Sub 'Main End Module

41 Example 6-14 The For Loop –Using a For loop

42 Example 6-14 Option Strict On Imports System Module Module1 Sub Main( ) Dim loopCounter As Integer For loopCounter = 0 To 9 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub 'Main End Module

43 Example 6-15 The For Loop –Loop with a Single counter

44 Example 6-15 Option Strict On Imports System Module Module1 Sub Main( ) Dim loopCounter As Single For loopCounter = 0.5 To 9 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub 'Main End Module

45 Example 6-16 The For loop –Adjusting the step counter

46 Example 6-16 Option Strict On Imports System Module Module1 Sub Main( ) Dim loopCounter As Single For loopCounter = 0.5 To 9 Step 0.5 Console.WriteLine("loopCounter: {0}", loopCounter) Next End Sub 'Main End Module

47 Example 6-17 The For Loop –Controlling a For Loop using Next Multiple updates with one Next statement

48 Example 6-17 Option Strict On Imports System Module Module1 Sub Main( ) Dim outer As Integer Dim inner As Integer For outer = 3 To 6 For inner = 10 To 12 Console.WriteLine("{0} * {1} = {2}", outer, inner, outer * inner) Next inner, outer End Sub 'Main End Module

49 Example Run the program. Test the program. Modify the program and rerun it again.

50 Practical session 3 Loops, Iterations, Repetitions

51 Practical task 1 Write a program that requests a number from 1 to 20, and then displays a row of that many asterisks.

52 Practical task 2 Write a program that displays a 10 by 10 table (i.e. 10 rows by 10 columns) of asterisks.

53 Practical task 3 You are offered two salary options for ten days of work. Option 1: $100 per day. Option 2: $2 the first day, $4 the second day, $8 the third day, $16 the fourth day and so on, with the amount doubling each day. Write a program to determine which option pays better.

54 Practical task 4 Write a program that reads in 10 student test marks (percentages) and then displays the average mark, the lowest mark and the highest mark. Use these marks: 87, 56, 73, 94, 89, 82, 85, 78, 43, 89. The list is terminated by –1.

55 Practical task 4 Suppose you deposit money into a savings account and let it accumulate at 6 percent interest rate compounded annually. Write a program that reads your amount deposited and then displays when (in how many years) you will be a millionaire. Test the program with initial amount values like $10 000, $20 000, $

56 Practical task 5 Write a program that reads in an odd number (between 11 and 19) and displays an inverted triangle of asterisks on the screen such that the input number is the number of asterisks in the top row, and with one asterisk in the bottom row.

57 Practical task 6 Write a program to display all the numbers between 1 and 100 that are part of the Fibonacci sequence. The Fibonacci sequence begins 1, 1, 2, 3, 5, 8, 13, …, where each new number in the sequence is found by adding the previous two numbers in the sequence.

58 Practical task 7 Write a program that requests an exam grade and displays the equivalent percentage mark range. Use the scale: A = 90%-100%; B = 80%-89%; … F = 0%-59%.

59 Practical task 8 Write a program that reads a number between 11 and 20 and displays a triangle of asterisks on the screen. If the input number is odd, then this is the number of asterisks in the top row – the triangle is inverted. If the input number is even, then this is the number of asterisks in the bottom row.

60 Questions?

61 Thank You For Your Attention!