0% found this document useful (0 votes)
10 views

Visual Basic Net

Here are the For...Next statements for the two assignments: 1. To compute the average score for 80 students across 5 courses: For student = 1 To 80 sum = 0 For course = 1 To 5 'get score input sum = sum + score Next course average = sum / 5 Next student 2. To sum the odd integers between 1 and 99: For count = 1 To 99 Step 2 sum = sum + count Next count
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Visual Basic Net

Here are the For...Next statements for the two assignments: 1. To compute the average score for 80 students across 5 courses: For student = 1 To 80 sum = 0 For course = 1 To 5 'get score input sum = sum + score Next course average = sum / 5 Next student 2. To sum the odd integers between 1 and 99: For count = 1 To 99 Step 2 sum = sum + count Next count
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

For Loop

• It executes a block of statements a specified number


of times. The number of iterations is controlled by a
loop variable which is initialized to a certain value by
the for statement then is incremented for each
iteration of the loop. The statements in the body of
the loop are repeatedly executed until the loop
variable exceeds. The loop variable can be of any
numeric type.
• i= 1
• For i= 1 to 10
• i = i+ 1
• Next.
Iteration cont.
• For Next Statement
• Ideal for loops that require a counter, pretest form only
• For Next loops provide only:
• Counter-controlled loop
• Pre-test loop
• Initializes counter variable
• Automatically increments counter
• Simplifies and shortens code
Iteration cont.

Format of a For Next loop


Iteration cont.

Flowchart of For Next loop


• Identify the statements that are correctly formed and
those that have errors. For those with errors, state
what is wrong.
(i) For decIndex = 3.5D to 6.0D, step 0.5D
Next decIndex (invalid)
(ii) For intIndex = intBegin to intEnd step 3
Next intEnd (invalid)
(iii) For 4= 1 to 10 step 2
Next For (invalid)
(iv) For intIndex = 100 to 0 step -25
Next intIndex (valid)
(v) For intIndex= 0 to -10 step -1
Next intIndex (valid)
(v) For intIndex = 10 to 1
Next intIndex (invalid)
• How many times will the body of the loop be
executed for each of these examples? What will be
the value of the loop index after normal completion
of the loop.
(a) For intCounter = 1 to 3
(b) For intCounter = 2 to 11 step 3
(c) For decCounter = 3.0D to 6.0D step 0.5D
(a) For intCounter = 1 to 3
Ans: will be executed 2 times, intCounter will have an
ending value of 4

(b) For intCounter = 2 to 11 step 3


Ans: will be executed 4 times, have an ending value of
14 in intCounter.

(c) For decCounter = 3.0D to 6.0D step 0.5D


Will be executed 7 times, decCounter will have an
ending value of 6.5
Iteration cont.
• Example: Computes the exam average program above using For Next
statement.
• Solution
Iteration cont.
Module Module1
Sub Main()
'define(variables)
Dim sum, average As Double
Dim numberOfExams As Integer = 5
Dim counter As Integer
Iteration cont.
' begin loop
For counter = 1 To 5 Step 1
Console.WriteLine("Enter an Exam Score: ")
sum = sum + Convert.ToDouble(Console.ReadLine())
Next
' compute and display the average
average = sum / numberOfExams
Console.WriteLine("The average is: " & Math.Round(average, 1))
End Sub
End Module
Iteration cont.
• Nested loop
• One iteration structure placed inside another

• Can place iteration inside selection


• And vice versa
Assignment.
• Assume 80 students sat for 5 courses in the first semester
examination, write a For… Next statement to computes the average
score.

• Write statement to sum the odd integers between 1 and 99 using a


For…Next statement. Assume that the Integer variables sum and
count have been declared.

You might also like