Вы находитесь на странице: 1из 28

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. Cod : CCP-503
Topic : Programming Fundamentals
Duration : 50 Minutes
Sub. Topic : Programs on Loops
Teaching Aids : PPT, Clips and Images
CCP503.34 to 35 1
Objective:

On completion of this period, you would be able


to know:
 Different applications of Loop Constructs
 Writing small programs using Loop
control flow structures – like Do…Loop
 While…Wend
 For…Net Loop Structures etc,.

CCP503.34 to 35 2
Recap

 Loops
 Decision Structures

CCP503.34 to 35 3
Programs on Do…Loop Structures

CCP503.34 to 35 4
Program on Do…Loop

Follow the steps to develop application:

 Open a new Standard EXE project & name it as


Form1
 Add the code in the Form_load as in Fig 34.1
 Now run application
 Observe changes

CCP503.34 to 35 5
Fig 34.1
CCP503.34 to 35 6
Program1 On For…Next To Print All Even
Numbers In Between 0 To 20 Numbers On
Form

Follow the steps to develop application:

 Open a new Standard EXE project & name it as


Form1
 Add two buttons ”compute” , ”exit” and one text and
two label boxes as in Fig 34.2

CCP503.34 to 35 7
Program1 On For…Next To Print All
Even Numbers In Between 0 To 20
Numbers On Form (Contd.)

 Now run application, & click “compute”


 Variable starts with 0 and increments by up to 20
 Even numbers between 0 to 20 will be printed on
screen

CCP503.34 to 35 8
Fig 34.2
CCP503.34 to 35 9
Program 2 On For…next And An Array To
Accept List Of 10 Elements And Then Print
Them On The Form
Follow the steps to develop application:

 Open a new Standard EXE project & name it as


Form1
 Add three buttons ”Enter” , “Display” & ”exit” Fig 34.3
 Now run application, & click Enter button to enter
array
 After entering array, click Display to display array on
screen
 Click Exit to End the Program
CCP503.34 to 35 10
Fig 33.3
CCP503.34 to 35 11
Program3 on For…Next to accept list of
elements into a Matrix and then display
elements.
Follow the steps to develop application:
 Open a new Standard EXE project & name it as
Form1
 Add two buttons ”Fill” , ”Exit” & a text box as in Fig
34.4
 Now run application, & click Fill button
 An Array will be filled on the text box
 Click Exit to End the Program

CCP503.34 to 35 12
Fig 33.4
CCP503.34 to 35 13
Program on by using While…Wend to print
list of 5 numbers from 5, 10, 20… etc,.

Follow the steps to develop application:


 Open a new Standard EXE project & name it as
Form1
 Add two buttons ”compute” , ”exit” and one text and
two label boxes as in Fig 32.10
 Now run application, & click “compute”
 Display all multiples of 5 from 1 to 25

CCP503.34 to 35 14
Fig 34.5

CCP503.34 to 35 15
Program Prime Number Test

CCP503.34 to 35 16
Output of Prime Number Test
Program

CCP503.34 to 35 17
Output of Prime Test Program

CCP503.34 to 35 18
Output of Prime Test Program

CCP503.34 to 35 19
Summary
Writing small programs in VB using control Flow
statements – Looping Structure like
 Programs on Do….Loop
 Programs on While.…Wend
 Programs on For….Next etc,.

CCP503.34 to 35 20
Frequently Asked Questions

1. Explain Control Flow Statements with examples?


2. Write syntax of Do…Loop in VB? Use examples
to explain?
3. Give syntax of For…Next ? And write a simple
array program using For…Next ?
4. What is the syntax of While…Wend ? and write
an example for it?

CCP503.34 to 35 21
Assignment

CCP503.34 to 35 22
Practice Programs on Do…Loop
Counter = 0
Do While counter <=1000
             text1. text = counter
             counter = counter+1 
Loop 
*The above example will keep on adding until >1000 
The above example can be rewritten as
        Do
               text1 . text=counter
             counter =counter+1 
       Loop until counter>1000

CCP503.34 to 35 23
Practice Programs on While…Wend

 The following program prompts user for numeric


data
 User can type a negative value to indicate that all
values are entered

number=0
While number=>0
total= total + number
number =Input box (Please enter another value”)
wend
CCP503.34 to 35 24
Assignment

1. Which line of code is executed after an Exitdo


Statement?
2. Which syntax of the Do statement executes
the code block for the statement at least
once?
3. Does a start and end value always have to be
provided to the For statement?

CCP503.34 to 35 25
Quiz

CCP503.34 to 35 26
1. What is the output of following program?

private sub command1_click()


For count = 1 to 1000 step 10
count = count+1
print count
Next
End sub 

 Prints 2, 13, 24, 35, 46, 57,…..


 Prints 1,10,20,…..990,1000
 Prints 2, 4,6,8, ….1000
 Prints 1, 12, 23, 34, 45, 56, ….          
CCP503.34 to 35 27
2. What is the output of following program?

Private sub command2_click()


For counter=500 to 5 step -5
counter=counter-10
print counter
Next
End sub

A. Prints 500,495,490,485,480,475……..10,5 on
screen
B. Prints 490,475,460,445,430…..40,25,10,-5 on
screen
C. Prints 500, 490, 480, 470, ….5
D. Prints 495, 490, 485, 480, ….0       
CCP503.34 to 35 28

Вам также может понравиться