0% found this document useful (0 votes)
38 views23 pages

Control Structures - Repetition: AAPP00-8-3-2

The document discusses different types of repetition control structures in Visual Basic .NET, including Do While, Do Until, and For...Next loops. It provides examples of how to write each type of loop to iterate through numbers or accept user input. The key differences between each loop type are explained, such as Do While testing at the beginning or end of each iteration. Nested loops are also introduced.

Uploaded by

Yovin Lekamge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views23 pages

Control Structures - Repetition: AAPP00-8-3-2

The document discusses different types of repetition control structures in Visual Basic .NET, including Do While, Do Until, and For...Next loops. It provides examples of how to write each type of loop to iterate through numbers or accept user input. The key differences between each loop type are explained, such as Do While testing at the beginning or end of each iteration. Nested loops are also introduced.

Uploaded by

Yovin Lekamge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Visual Basic .

Net
AAPP00-8-3-2

Control Structures - Repetition


Topic & Structure of Lesson

Do While iteration – test beginning / test end


Do Until Loop – test beginning / test end
For .. Next iterative construct

Nested Loops

AAPP00-8-3-2 VBN Visual Basic .Net Slide 2 (of 18)


Learning Outcomes
Be able to:
1. Write a Do Loop iteration (all 4
variations)
2. Use a For .. Next iterative construct
3. Write a Nested Loop

AAPP00-8-3-2 VBN Visual Basic .Net Slide 3 (of 18)


Key Terms you must be able to use

If you have mastered this topic, you should


be able to use the following terms correctly
in your assignments and exams:

Do While Loops (test at beginning and end)


Do Until Loops (test at beginning and end)
For.. Next

AAPP00-8-3-2 VBN Visual Basic .Net Slide 4 (of 18)


Do While … Loop

Do While condition is True


execute statement(s)
Loop

AAPP00-8-3-2 VBN Visual Basic .Net Slide 6 (of 18)


Flowchart – Do While … Loop

ctr = 1

ctr = ctr + 1

True
Ctr <=10 Do something

False

AAPP00-8-3-2 VBN Visual Basic .Net Slide 7 (of 18)


Do While … Loop

Display numbers from 1 to 10


n=1
Do While n <=10
List1.Additem n
n=n+1
Loop

AAPP00-8-3-2 VBN Visual Basic .Net Slide 8 (of 18)


Exercise 1
• Draw a flowchart and write a program to
display the sum of the digits from 1 to 10

i.e. 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
• Show your answer using the
Do While .. Loop
• Use a MessageBox to display the Sum

AAPP00-8-3-2 VBN Visual Basic .Net Slide 9 (of 18)


While End While
Exercise 1 Solution
Dim i, intSum as integer
‘Sum the integers from 1 to 10
i=1
Do While i <=10
sum = sum + i
i=i+1
Loop
MessageBox “The sum is” & sum

AAPP00-8-3-2 VBN Visual Basic .Net Slide 10 (of 18)


Do Loop
There are 4 versions of the Do Loop:
Do While condition is True Do Until condition is true
execute statement(s) execute statement(s)
Loop Loop

•Do While test at beginning •Do Until test at beginning


a pre-test loop a pre-test loop
Do Do
execute statement(s) execute statement(s)
Loop While condition is true Loop Until condition is true

•Do While test at end •Do Until test at end


a post-test loop a post-test loop

AAPP00-8-3-2 VBN Visual Basic .Net Slide 11 (of 18)


Do Loop
There are 4 versions of the Do Loop:
Use the form that “reads” most naturally
In this lesson, we will concentrate on the
Do … Loop While – with the test at the end

Do
execute statement(s)
Loop While condition is true

AAPP00-8-3-2 VBN Visual Basic .Net Slide 11 (of 18)


Flowchart – Do … Loop While condition
Ctr = 1

Do something

ctr = ctr + 1

False True
Ctr <=10

AAPP00-8-3-2 VBN Visual Basic .Net Slide 12 (of 18)


Do While

n=1
Do
list1.additem n
n=n+1
Loop while n <= 10

AAPP00-8-3-2 VBN Visual Basic .Net Slide 13 (of 18)


Exercise 2 & 3
• Write a program that accepts 5 Fahrenheit
temperatures using an InputBox and converts
them to Celsius.
• Display all the converted temperatures in Celsius
using a list box.

Formula : FtoC = (fahrenheit - 32) * 5/9

• Use a Do While … Loop


• Repeat the program using a Do … Until Loop

AAPP00-8-3-2 VBN Visual Basic .Net Slide 14 (of 18)


Exercises 2 and 3 Solutions

Dim fahr, celsius as Double Dim fahr, celsius as Double


Dim ctr as integer Dim ctr as integer
ctr = 1 ctr = 1
Do While ctr <= 5 Do
fahr = InputBox(“Enter a fahr = InputBox(“Enter a
temperature in Fahrenheit”) temperature in Fahrenheit”)
celsius = (fahr-32) * 5 / 9 celsius = (fahr-32) * 5 / 9
ctr = ctr + 1 ctr = ctr + 1
MsgBox (“Celsius: ” & celsius) MsgBox (“Celsius: ” & celsius)
Loop Loop until ctr > 5

AAPP00-8-3-2 VBN Visual Basic .Net Slide 10 (of 18)


For Next

For variable = startValue to


endValue Step stepValue
Step stepValue is
optional; if missing,
execute statement(s) assumed to be 1

Next variable

AAPP00-8-3-2 VBN Visual Basic .Net Slide 11 (of 18)


Flowchart – For Next

Ctr = 1

Do something
True

Ctr <=10 ctr = ctr + 1

False

AAPP00-8-3-2 VBN Visual Basic .Net Slide 12 (of 18)


For Next

MessageBox (“Squares of the first five numbers:”)


For ctr = 1 to 5
MessageBox (ctr & “squared is: “ & ctr * ctr)
Next

AAPP00-8-3-2 VBN Visual Basic .Net Slide 13 (of 18)


Exercise 4

• Write a program that accepts 5 Fahrenheit


temperatures using an InputBox and converts
them to Celsius.
• Display all the converted temperatures in
Celsius using a MessageBox.

Formula : FtoC = (fahrenheit - 32) * 5/9

• Use a For Next

AAPP00-8-3-2 VBN Visual Basic .Net Slide 14 (of 18)


Exercise 4 Solution
Dim fahr, celsius as Double
Dim ctr as integer
For ctr = 1 to 5
fahr = InputBox(“Enter a temperature in Fahrenheit”)
celsius = (fahr-32) * 5 / 9
MessageBox (“Celsius: ” & celsius)
Next

AAPP00-8-3-2 VBN Visual Basic .Net Slide 10 (of 18)


Summary
• Do while … Loop executes statements as long as the
condition is true
• Do until … Loop executes statements as long as the condition
is false
• Do … Loop While executes statements as long as the
condition is true; at least once
• Do … Loop Until executes statements as long as the
condition is false; at least once
• For …Next executes a loop a pre-determined number of times

AAPP00-8-3-2 VBN Visual Basic .Net Slide 16 (of 18)


Next lesson

Sub Procedures

AAPP00-8-3-2 VBN Visual Basic .Net Slide 17 (of 18)


Q&A
AAPP00-8-3-2 VBN Visual Basic .Net Slide 18 (of 18)

You might also like