Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.

Slides:



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

CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
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.
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.
Pseudocode and Algorithms
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
5.05 Apply Looping Structures
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Chapter 5: Control Structures II (Repetition)
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 12: How Long Can This Go On?
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
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.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
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.
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.
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.
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.
Tutorial 6 The Repetition Structure
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
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.
6.2 For…Next Loops General Form of a For…Next Loop
Introduction to Problem Solving and Control Statements.
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:
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
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.
CSET 3250 Client-Side Scripting VBScript : Operators, Control Structures, Procedures and Functions.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
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.
1 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: Analyze a Loan.
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.
Chapter 6: Loops.
Chapter 5- Control Structures: Part 2
JavaScript: Control Statements I
Outline Altering flow of control Boolean expressions
3 Control Statements:.
Introduction to Problem Solving and Control Statements
Chapter 6: Repetition Statements
2.6 The if/else Selection Structure
Prepared By: Deborah Becker
ASP control structure BRANCHING STATEMENTS
REPETITION Why Repetition?
Presentation transcript:

Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz

Compunet Corporation2 Loop Structures Loop structures allow you to execute one or more lines of code repetitively. You can repeat the statements until a condition is true, until a condition is false, a specified number of times, or once for each object in a collection. The loop structures supported by Visual Basic include: –While –Do...Loop –For...Next –For Each...Next

Compunet Corporation3 The While Loop You can use the While statement to execute a block of statements an indefinite number of times depending on the Boolean value of a condition. The statements are repeated as long as the condition is True.

Compunet Corporation4 Shampoo Algorithm 1.Get shampoo 2.Lather 3.Rinse What’s the problem?

Compunet Corporation5 While Loop Example Imports System.Console Public Module WhileDemo Sub Main() CheckWhile() End Sub Sub CheckWhile() Dim Counter As Integer = 0 Dim Number As Integer = 10 While Number > 6 Number = Number - 1 Counter = Counter + 1 End While WriteLine ("The loop ran " & Counter & " times.") End Sub End Module 4

Compunet Corporation6 While Loop Example Imports System.Console Public Class WhileDemo Shared Sub Main() CheckWhile() End Sub Shared Sub CheckWhile() Dim Counter As Integer = 0 Dim Number As Integer = 10 While Number > 6 Number = Number - 1 Counter = Counter + 1 End While WriteLine ("The loop ran " & Counter & " times.") End Sub End Class 4

Compunet Corporation7 Example (THE while LOOP) In the following example, Number is assigned a value that could cause the loop to execute more than 2 ^ 31 times. Sub ExitWhileExample() Dim Counter As Integer = 0 Dim Number As Integer = 8 While Number <> 10 If Number < 0 Then Exit While Number = Number - 1 Counter = Counter + 1 End While MsgBox("The loop ran " & Counter & " times.") End Sub Note 1: To stop an endless loop, press ESC or CTRL+BREAK. Note 2: Need Microsoft.VisualBasic namespace for MsgBox 9

Compunet Corporation8 Do...Loop Statements There are two ways to use the While keyword to check a condition in a Do loop. –You can check the condition before you enter the loop –or you can check it after the loop has run at least once.

Compunet Corporation9 Pre-test and Post-test Loops Loop body Condition TRUE FALSE Loop body Condition TRUE FALSE

Compunet Corporation10 Do...Loop Statements In the following example, the CheckWhileFirst procedure tests the condition before it enters the loop. If Number had been initialized to 6 instead of 10, the statements inside the loop would never run. Sub CheckWhileFirst() Dim Counter As Integer = 0 Dim Number As Integer = 10 Do While Number > 6 Number = Number - 1 Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") End Sub 4

Compunet Corporation11 Do...Loop Statements In the following example, the CheckWhileLast procedure, the statements inside the loop run once before testing the condition, which is False on the first test. Sub CheckWhileLast() Dim Counter As Integer = 0 Dim Number As Integer = 5 Do Number = Number - 1 Counter = Counter + 1 Loop While Number > 6 MsgBox("The loop ran " & Counter & " times.") End Sub 1

Compunet Corporation12 Do...Loop Statements There are two ways to use the Until keyword to check a condition in a Do loop. –You can check the condition before you enter the loop, –or you can check it after the loop has run at least once. –Looping continues while the condition remains False.

Compunet Corporation13 Do...Loop Statements Example In the following example, the CheckUntilFirst procedure tests the condition before it enters the loop. If Number had been initialized to 15 instead of 20, the statements inside the loop would never run. Sub CheckUntilFirst() Dim Counter As Integer = 0 Dim Number As Integer = 20 Do Until Number = 15 Number = Number - 1 Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") End Sub 5

Compunet Corporation14 Do...Loop Statements Example In the following example, the CheckUntilLast procedure, the statements inside the loop run once before testing the condition, which is False on the first test.. Sub CheckUntilLast() Dim Counter As Integer = 0 Dim Number As Integer = 20 Do Number = Number - 1 Counter = Counter + 1 Loop Until Number = 15 MsgBox("The loop ran " & Counter & " times.") End Sub 5

Compunet Corporation15 Exercises Write a program that displays the numbers 1 through 100. Write a program that displays the numbers 100 to 1, in reverse order.

Compunet Corporation16 Do...Loop – Exit Do Statements You can transfer out of a Do loop by using the Exit Do statement. One use of this is to test for a condition that could cause an endless loop, which is a loop that could run an extremely large or even infinite number of times. –If the condition is True, use Exit Do to escape. –If the condition is False, the loop continues running.

Compunet Corporation17 Do...Loop – Exit Do Example In the following example, Number is assigned a value that could cause the loop to execute more than 2 ^ 31 times. The If statement checks for this and exits if it exists, preventing endless looping. Sub ExitDoExample() Dim Counter As Integer = 0 Dim Number As Integer = 8 Do Until Number = 10 If Number <= 0 Then Exit Do Number = Number - 1 Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") End Sub 8

Compunet Corporation18 For...Next Statements Do loops work well when you do not know in advance how many times you need to execute the statements in the loop However, when you expect to execute the loop a specific number of times, a For...Next loop is a better choice. For loop uses a variable called a counter that increases or decreases in value during each repetition of the loop.

Compunet Corporation19 For...Next Statements The For loop is used to repeat a statement or block of statements. The syntax is as follows: For counter [ As datatype ] = start To end [ Step step ] ' Statement block to be executed for each value of counter. Next [ counter ]

Compunet Corporation20 For...Next Example Public Module ForLoop Sub Main() Dim I As Integer For I = 0 To 10 System.Console.Write (I & " ") Next I System.Console.WriteLine (" Terminating") End Sub End Module Output: Terminating

Compunet Corporation21 For...Next Example Public Class ForLoop Shared Sub Main() Dim I As Integer For I = 0 To 10 System.Console.Write (I & " ") Next I System.Console.WriteLine (" Terminating") End Sub End Class Output: Terminating

Compunet Corporation22 Example (for Statement) Public Module ForLoop Sub Main() Dim sum AS Integer =0 Dim prod AS Integer =1 For I AS Integer = 1 To 6 sum = sum + I prod = prod * I Next I System.Console.Write ("Product & Sum:") System.Console.Write (prod) System.Console.Write (" & ") System.Console.Write (sum) End Sub End Module Output: Product & Sum:720 & 21

Compunet Corporation23 Example (for Statement) Public Class ForLoop Shared Sub Main() Dim sum AS Integer =0 Dim prod AS Integer =1 For I AS Integer = 1 To 6 sum = sum + I prod = prod * I Next I System.Console.Write ("Product & Sum:") System.Console.Write (prod) System.Console.Write (" & ") System.Console.Write (sum) End Sub End Class Output: Product & Sum:720 & 21

Compunet Corporation24 More Practice Write programs (using loops) to do each of the following: –Sigma(j, k) that solves Si, where i goes from j to k –Product(j, k) that solves Pi, where i goes from j to k –Write a program to find the Factorial of n i.e. n!

Compunet Corporation25 Incrementing and Decrementing the Counter Variable One way of Incrementing and Decrementing: Increases the value n = n + 1 or n += 1 Decreases the value n = n - 1or n -= 1

Compunet Corporation26 Increment and decrement Public Module IncrementDecrement Sub Main() Dim n AS Integer =6 n += 1 ' 7 n = n + 1 ' 8 System.Console.Write ( n ) ' 8 will be printed n -= 1 ' 7 n = n - 1 ' 6 System.Console.Write ( n ) ' 6 will be printed End Sub End Module

Compunet Corporation27 Incrementing and Decrementing the Counter Variable Using the Step keyword, you can increment or decrement the counter by the value you specify. In the following example, the counter variable J is incremented by 1 each time the loop repeats Sub TwosTotal() Dim J, Total As Integer For J = 1 To 10 Step 1 Total = Total + 1 Next J MsgBox("The total is " & Total) End Sub

Compunet Corporation28 Incrementing and Decrementing the Counter Variable To decrease the counter variable, use a negative Step value. Sub NewTotal() Dim N, Total As Integer For N = 16 To 4 Step -1 Total = Total + 1 Next N MsgBox("The total is " & Total) End Sub

Compunet Corporation29 Exiting a For...Next Loop You can exit a For...Next loop before the counter passes its end value by using the Exit For statement.

Compunet Corporation30 Exiting a For...Next Loop Sub ExitDoExample() Dim Counter As Integer = 0 Dim Number As Integer For Number = 10 To 1 Step -1 If Number <= 0 Then Exit For Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") End Sub

Compunet Corporation31 Faster For...Next Loops Integer variables are slightly faster to update than either Short or Long ones. ' First case -- Integer is fastest. Dim Fastest As Integer For Fastest = 0 to ' Statements to execute for each value of Fastest. Next Fastest

Compunet Corporation32 Faster For...Next Loops Integer variables are slightly faster to update than either Short or Long ones. ' Second case -- Long is not as fast. Dim NotAsFastest As Integer For NotAsFastest = 0 To ' Statements to execute for each value of Fastest. Next NotAsFastest

Compunet Corporation33 Faster For...Next Loops Integer variables are slightly faster to update than either Short or Long ones. ' Third case -- Decimal is slower. Dim MuchSlower As Decimal For MuchSlower = 0 To 'Statements to execute for each value of Fastest. Next MuchSlower

Compunet Corporation34 Exercise (THE do LOOP) Write an application that generates the first 15 numbers in the Fibonacci series (i.e., … ) A number in this series is calculated by adding together the two numbers that precede it. Use a Do loop to control the computations. Show how to replace this For loop with a While For I AS Integer = 1 To 10 Step 2 Next I

Compunet Corporation35 Nested Loop When the body of one loop contains another, the second is said to be nested inside the first. For I AS Integer = 0 To 5 Step 1 For J AS Integer = 0 To 5 Step 1 System.Console.Write (I & " ") Next J Next I

Compunet Corporation36 Exercise (NESTED LOOP) Write an application that counts the total number of characters in all of its command-line arguments. Write a program that finds all the prime number between 100 and 200 Write a program to display the following:

Compunet Corporation37 Exercise Write an application that searches through its command- line arguments. If an arguments is found that does not begin with an uppercase letter, display an error message and terminate.

Compunet Corporation38 Exercise Write a program that accepts one command-line argument and displays its Spanish equivalent. For example the token "one," "two," "three," "four," and "five" are translated to "Uno," "Dos," "Tres," "Quatro," and "Cinco." Write an application that can process a sequence of command-line arguments that describe the quantities and types of coins held by a person. This sequence of arguments might be similar to the following: 5 nickles 4 quarters 2 dimes. In this case, the program would display a message indicating that the total value of these coins is $1.45.