Professional Documents
Culture Documents
CCP503
CCP503
CCP503
ANDHRAPRADESH
Name : K.Sreeramacharyulu
Designation : Lecturer
Branch : Commercial and Computer
Practice
Institute : Govt Polytechnic, Srikakulam
Year/Semester : V Semester
Subject : VISUAL BASIC-I
Sub. Code : CCP-503
Topic : Programming Fundamentals
Duration : 50 Mts
Sub. Topic : Control structures, Loops
Teaching Aids : PPT, Clips and Images
CCP503.32 1
Objective:
CCP503.32 2
Control Flow
Normally, instructions in a program are
executed one after the other
But in some cases like testing a value, this
linear order to be changed
Sometimes, same set of instructions have to
be repeated
Control flow statements help in changing the
flow of execution of program instructions
CCP503.32 3
Visual Basic divides the control flow statements
into two types:
Looping Structures
Decision Structures
CCP503.32 4
Looping Structures
Loop statements allow us to execute one or more
lines of code repetitively
Many tasks consist of operations that must be
repeated over and over again
Looping structures are an important part of any
programming language
CCP503.32 5
VB supports following three types of loop
structures :
Do…Loop
For…Next
While…Wend
CCP503.32 6
Also support some loop breaking statements
such as
CCP503.32 7
Do…Loop
CCP503.32 8
There are two variations of the Do…Loop
statement and both use the same basic model
A loop can be executed either while the conditions
is true or until the conditions becomes True
These two variables use the keyword “while” and
“until” to specify how long the statements are
executed
Syntax of Do…Loop will be as follows:
CCP503.32 9
To execute a block of statements while a
condition is true, use the following syntax:
Do While condition
Statement-block
Loop
To execute a block of statements until the
condition becomes true, use following syntax:
Do Until condition
Statement-block
Loop
CCP503.32 10
Examples of Do…Loop in a single
program:
This program prints numbers from 0 to 9
i=0
Do while i<10
print i
i=i+1
Loop
CCP503.32 11
This program prints numbers from 10
to 1
Do Until i=0
print i
i=i-1
Loop
CCP503.32 12
For…Next
CCP503.32 13
It has following syntax:
For counter = start to end [step increment]
statements
Next
Keywords in square brackets are optional
The arguments “counter”, “start”, “end”, and
“increment” are all numeric
CCP503.32 14
Example of For…Loop:
This program prints 1 to 10 and their square
values
For i = 1 to 10
print i : i * i
Next i
CCP503.32 15
While…Wend
CCP503.32 16
Example for While…Wend :
total = 0
i=1
While i<=10
number= inputbox(“ please enter another value”)
total= total + number
i = i+1
Wend
PRINT TOTAL
The above program unit asks 10 numbers and sums
and shows the sum
CCP503.32 17
Loop Breaking Statements
Exit Statement
CCP503.32 18
EXIT DO:
CCP503.32 19
n=0
s=0
I =1
Do until i = 20
n = InputBox (“Enter any Number : ”)
if (n = 0) then
Exit do
end if
s = s+n
i = i+1
Loop
PRINT “sum = “: s
CCP503.32 20
EXIT FOR:
The following program demonstrates use
“EXIT FOR”
s=0
n=0
For i = 1 to 20
n = Inputbox(“Give value: ”)
if (n = 0) then
EXIT FOR
End if
s = s+1
Next i
PRINT “sum of:; i -1; “values is “; s
CCP503.32 21
Summary
Control Flow Statements – Looping structure
- DO…WHILE
- FOR..NEXT
CCP503.32 22
Frequently Asked Questions
CCP503.32 23
Quiz
CCP503.32 24
2. Which of the following loop is used to repeat
the statements for fixed no. of times
a) For…Next
b) Select…Case
c) While…Wend
d) Do.. While
CCP503.32 25
3. ______ is used to break / halt a For…Next
loop
A. Exit
B. Next
C. Exit For
D. Break
CCP503.32 26
Assignment
CCP503.32 27