Download presentation
Presentation is loading. Please wait.
Published byRobert Hutchinson Modified over 8 years ago
1
Loops ISYS 350
2
Write a Program that asks user to enter any numbers and displays the largest number Process: Largest = the first number Get the next number If the next number is greater than Largest Then Largest = the next number Repeat this part until no more data Note: Loop
3
Repetition Structure (or Loop) Visual Basic has three structures that allow a statement or group of statements to repeat – Do While – Do Until – For...Next
4
Do While Syntax Do, While, and Loop are new keywords The Do While statement marks the beginning of the loop The Loop statement marks the end The statements to repeat are found between these and called the body of the loop Do While expression statement(s) Loop
5
An Infinite Loop Do While 1 > 0 MessageBox.Show("LOOPING") Loop
6
Using a Counter to Control the Loop Dim counter As Integer = 0 Do While counter < 5 MessageBox.Show("counter = " + counter.ToString) counter = counter + 1 Loop
7
Using a Flag Dim continueFlag As Boolean = True Do While continueFlag=True If MessageBox.Show("Do you want to continuue? ", "Continue?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then continueFlag = False End If Loop
8
Accumulator Find the sum of all numbers between 1 and N. Dim N, Sum As Integer N = InputBox("enter an integer, N") Sum = 0 Do While N > 0 Sum = Sum + N N = N - 1 Loop MessageBox.Show("The sum is: " + Sum.ToString)
9
DO While Loop Example N Factorial, N! Dim N, Nfact As Integer N = InputBox("enter an integer, N") Nfact = 1 Do While N > 0 Nfact = Nfact * N N = N - 1 Loop MessageBox.Show("The sum is: " + Nfact.ToString)
10
Enter Loan and Rate in textboxes, then display monthly pay for terms from 5 to 40 with a step of 5 in a ListBox Dim Loan, Rate, Term, payment As Double Loan = TextBox1.Text Rate = TextBox2.Text Term = 5 Do While Term <= 40 payment = Pmt(Rate / 12, Term * 12, -Loan) ListBox1.Items.Add("Term=" + Term.ToString + "Payment=" + payment.ToString) Term = Term + 5 Loop
11
For…Next Loop Ideal for loops that require a counter For, To, and Next are keywords CounterVariable tracks number of iterations StartValue is initial value of counter EndValue is counter number of final iteration Optional Step allows the counter to increment at a value other than 1 at each iteration of the loop For CounterVariable = StartValue To EndValue [Step] statement Next [CounterVariable]
12
Accumulator Find the sum of all numbers between 1 and N. Dim N, Sum, Counter As Integer N = InputBox("enter an integer, N") Sum = 0 For Counter = 1 To N Sum = Sum + Counter Next MessageBox.Show("The sum is: " + Sum.ToString) Sum = 0 For Counter = N To 1 Step -1 Sum = Sum + Counter Next MessageBox.Show("The sum is: " + Sum.ToString)
13
Enter Loan and Rate in textboxes, then display monthly pay for terms from 5 to 40 with a step of 5 in a ListBox Dim Loan, Rate, Term, payment As Double Loan = TextBox1.Text Rate = TextBox2.Text For Term = 5 To 40 Step 5 payment = Pmt(Rate / 12, Term * 12, -Loan) ListBox1.Items.Add("Term=" + Term.ToString + "Payment=" + payment.ToString) Next
14
Increment smaller than 1: Enter Loan and Term in textboxes, then display monthly pay for rates from 3% to 10% with a step of.5% in a ListBox Dim Loan, Rate, Term, payment As Double Loan = TextBox1.Text Term = TextBox2.Text For Rate = 0.03 To 0.1 Step 0.005 payment = Pmt(Rate / 12, Term * 12, -Loan) ListBox1.Items.Add("Rate=" + Rate.ToString + " Payment=" + payment.ToString) Next
15
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 15 Find the Sum of All Even Numbers between 1 and N Dim N, Sum, Counter As Integer N = InputBox("enter an integer, N") Sum = 0 For Counter = 1 To N If Counter Mod 2 = 0 Then Sum = Sum + Counter End If Next MessageBox.Show("The sum is: " + Sum.ToString)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.