Vous êtes sur la page 1sur 23

m   

{Lecturer, Computer Engineering Department }


Ú  i = 1  2.5  0.5

  i
  i
© 2 e program prints t is sequence of numbers:
1
1.5
2
2.5

  
Ú i=1 5
Ú j=1 5

 i + j
  j
  I
 
©In Visual Basic, you can use an Exit For statement to
exit a For...Next loop before t e loop as finis ed
executing.

  


Ú i=1 5
Ú  j = 1  5
 i + j = 8    Ú 

  i + j
 j
  i

 
©     
       
 s ould repeat.
© As an alternative to a Ú      
    statements until a
certain condition in t e loop is true.
© A Standard Do Loop::
  |  
 | 
  
    | 
!

  
i=0
  i < 25
'block of statements to be executed

 "value is :: "; i
i=i+1
!
 
© NO2E ::
2 is code brings up an interesting fact about Do Loops:
if t e condition at t e top of t e loop Is not 2rue
w en t e Do statement is first evaluated, Visual Basic
never executes t e Do loop.
© If you want t e loop to always run at least once in a
program, put t e conditional test at t e bottom of t e
loop.

  


i=0
Do
Print "value is :: "; i
i=i+1
Loop W ile i < 25
 
© 2 e advantage of t is syntax is t at you can update t e
variable before t e conditional test in t e loop
© Do loops are relentless, so it is very important t at you design test conditions
carefully.

© Eac loop must ave a 2rue exit point.

© If a loop test never evaluates to False , t e Loop will execute endlessly, and
your program will no longer respond to input
© Do loops are relentless, so it is very important t at you design test conditions
carefully.

© Eac loop must ave a 2rue exit point.

© If a loop test never evaluates to False , t e Loop will execute endlessly, and
your program will no longer respond to input
© So far, t e Do loops you ave seen use t e W ile keyword to
execute statements as long as t e     
© In Visual Basic, you can also use t e Until keyword in Do loops to
cycle 6  
  6@
© 2o test a condition, you can use t e ÿ     
     " t e  


 
 
 6    
^     ÿ  #
© A loop t at uses t e Until keyword is very similar to a loop t at uses
t e W ile keyword.

© In our example, t e only difference is t at t e test condition usually


contains t e equal to operator (=) versus t e not-equal-to operator
(<>).

© If using t e Until keyword makes sense to you, feel free to use it


wit test conditions in your Do loops.
© 2o execute a group of statements for a specified period of time, you
can use t e 2imer control in t e Visual Basic toolbox.
© 2 e 2imer control is an invisible stopwatc t at gives your programs
access to t e system clock.
© You can use t e 2imer to:
1) Count down from a preset time, like an egg timer.
2) Delay a program.
3) Repeat an action at prescribed intervals.

© Objects t at you create wit t e Visual Basic 2imer:


1) Are accurate to 1 millisecond (1/1000 of a second).
2) Aren¶t visible at run time.
3) Are associated wit an event procedure t at runs w enever t e
preset timer interval elapses
© 2o set a timer interval, you start by using t e timer Interval property.
© 2 en, you activate t e timer by setting t e timer Enabled property to
2rue.
© W en a timer is enabled, it runs constantly.
© 2 e program executes t e timer event procedure at t e prescribed
interval ² until t e user stops t e program, t e timer is disabled, or
t e Interval property is set to 0.
  $ ÿ %%
© Open a new project and place a timer object on
your form.
© 2 en locate a label at t e center of t e form and
adjust t e size as s own in t e figure below.
`  $ 
 %%
© Set t e properties of eac object as follows,
© Ú 
:: Caption Digital Clock
© 
:: Interval 1000
:: Enabled 2rue
© !
:: Alignment 2-Center
:: Caption ³ ³ (Optional)
::Font Arial, 20 pts, Bold
r ^ $ %%


Label1.Caption = 2ime
 

&   $ 
 $%%
© In t is example of a digital clock, setting t e timer
    '''(m
 update t e clock time every 1000 milliseconds
(once per second).
xOOD DAY !!

Vous aimerez peut-être aussi