CCP503

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

DEPARTMENT OF TECHNICAL EDUCATION

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:

On completion of this period, you would be able to


Know

 Control Flow statements – Looping


structures

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

 Exit For and


 Exit Do

CCP503.32 7
Do…Loop

 Executes a block of statements for as long as a


condition is True
 If statement is false , the program continues and
the statement following the loop is executed

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

 One of the oldest loop structures in programming


languages
 For.. Next loop repeats a fixed number of times
 Uses a variable that increases or decreases in
value during each repetition of the loop

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

 It executes a block of statements as long as a


condition is satisfied
 It has following syntax:
While <condition>
statement-Block
Wend
 As long as the condition is True , all the statements
are executed

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

 There may be situations when we need to


immediately exit from a loop, before it logically
ends
 VB provides a method to do this job :
EXIT DO statement
EXIT FOR statement

CCP503.32 18
EXIT DO:

 The following program asks up to 20 values and


sums them and shows result. However, by giving
‘0’ for value, looping can be discontinued with
‘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

2. Write the syntax of the Do…Loop statement in


VB?
3. Write the need of the For…Next statement?
4. Give the syntax and explain the While…Wend
statement?

CCP503.32 23
Quiz

1. Which of the following NOT belongs to


Looping structure ?
 Do…Loop
 If…Then…End IF
 While … Wend
 For … Next

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

1. Write a program in 1 to n numbers

2. Write a program that accepts 10 numbers and

find their product

CCP503.32 27

You might also like