Sie sind auf Seite 1von 12

Controlling Program Flow

Basically, in programming, there are two kinds of controls Loops and Branching.

Branching: The if-then-else statement is used to control the program flow. Usually a program may need to change course to branch out -- depending on some conditions within the program. The If-Then construct format: If (condition) then Block of tatements !nd If The If-Then-Else construct format: If (condition) then Block of tatements !lse Block of tatements !nd If

"age # of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

*et us see how we can apply these. !-ample #. tart !-ample/ If it is raining then take an umbrella

Take a shower "ut on your clothes !at Breakfast True

Is it rainin g

0alse

Take an umbrella

tart your car

+ri.e to work

top

"age $ of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

!-ample $. )alculate the weekly pay of an employee. 1.ertime is paid on the basis of time and half. tart

3et input/ hours worked per week and hourly rate.

4eekly labor hours 5 6'

)alculate 7egular "ay

)alculate 1.ertime "ay

"rint "ay )heck

top

*et us plan the program/ User Interface should look something like the following/

"age 2 of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

&isual Basic ob8ect names and captions/ 1b8ect 0irst *abel econd *abel Third *abel 0irst Te-t Boecond Te-t BoThird Te-t Bo0irst )ommand Button econd )ommand Button 0orm 9ame *abel# *abel$ *abel2 t-t:ours t-t7ate t-t3ross"ay cmd)alculate"ay cmd;uit 0orm# )aption :ours 4orked 7ate per :our 3ross "ay

)alculate "ay ;uit 4eekly "ay )laculator

"age 6 of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

"ri.ate ub cmd)alculate<)lick() =This program )alculates the weekly "ay =of an employee =It demonstrates the if-then-else construct in &isual Basic =If an employee works for 6' or less hours in a week =the pay is calculated as hours worked times 7ate =howe.er if the employee works for more than 6' hours in a week =then the pay is calculated as straight time for the first 6' hours =and time and half for the o.ertime hours +im :ours >s Integer +im 7ate >s )urrency +im 3ross<"ay >s )urrency :ours ? )Int(t-t:ours.Te-t) 7ate ? ))ur(t-t7ate.Te-t) If :ours @? 6' Then 3ross<"ay ? :ours A 7ate !lse 3ross<"ay ? 6' A 7ate B (:ours - 6') A 7ate A #.( !nd If t-t3ross"ay.Te-t ? trC(3ross<"ay) !nd ub "ri.ate ub cmd;uit<)lick() !nd !nd ub

"age ( of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

>fter the "rogram is 7un/

"age D of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

Loops:
> loop is a programming construct that repeats an operation a certain number of times or repeats an operation until some condition is met (or as long as some condition is true). *oops are again of two types. +eterminate and Indeterminate. If one repeats an operation a fi-ed number of times it is called a determinate loop% continuing until a condition becomes true is indeterminate.
The FOR-NEXT LOOP

+im I as Integer 0or I ? # to #' "rint I 9e-t I I is a counter tarting .alue is set to # The ending .alue is set to #' !-plain how &isual Basic process a 0or-9e-t tatement. I is set to #% it is compared to #'E if it is greater than #'% nothing happens. If it is less than #'% &B process all the subseFuent statements until it comes to the key word 9!GT. It adds one to the counter. and starts the process again. This process continues until the .alue of the counter is greater than #'. 4hen the counter is greater than #'% the control passes to the statement following the statement with word H9!GTI Indentation within a 017 tatement 1ff by one errors --Jou donKt always count by ones. Jou can count by twos% fractions or negati.es (backward). L"rint all the positi.e odd integers less than #''

"age M of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

Dim I as Integer

For I = 1 to 100 step 2 Print I Ne t I L imulate count down


Dim I as Integer

For I = 10 to 1 step -1 Print !It"s t minus ! # I # ! an$ counting%& Ne t I


Print Blastoff!!

"ri.ate ub +emo*oops<)lick() =This program demonstrates the results of .arious kinds of *oops =The 0or-9e-t =The +o-*oop-Until =The +o-*oop-4hile =The +o-4hile-*oop ="rogrammer Tsegay ,oges +im I >s Integer =The 0or-9e-t tatement "rint "rint N0rom the for-then statementN 0or I ? # To ( "rint I% 9e-t I "rint =needed to clear the print function "rint "rint N0rom the for-then statement with the step functionN 0or I ? # To #' tep $ "rint I% 9e-t I "rint =needed to clear the print function "rint "rint N0rom the +o-*oop-UntilN I?# +o
"age O of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

"rint I% I?IB# *oop Until I 5 ( "rint =needed to clear the print function "rint "rint N0rom the second +o-*oop-Until statementN I?# +o "rint I% I?IB$ *oop Until I 5 #' "rint =needed to clear the print function "rint "rint N0rom the +o-*oop-4hile statementN I?# +o "rint I% I?IB# *oop 4hile I @? ( "rint =needed to clear the print function "rint "rint N0rom the +o-4hile statementN I?# +o 4hile (I @? () "rint I% I?IB# *oop "rint =needed to clear the print function !nd ub "ri.ate ub cmd;uit<)lick() !nd !nd ub

"age P of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

User Interface +esign/

7esults of the .arious *oops in the "rogram/

L''P(
"age #' of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

!-ample #.

The Do-Until Loo


+im number >s Integer number ? # +o "rint number number ? number B # *oop Until number 5 #' !-ample $.

The Do-While Loo


+im number >s Integer number ? # +o "rint number number ? number B # *oop 4hile number @ ## !-ample 2.

The For-Next Loo


+im number >s Integer 0or number ?# To #' "rint number number ? number B # 9e-t number @ ##

"age ## of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

!-ample 6.

The Nested For-Next Loo


+im num# >s Integer +im num$ >s Integer 0or num# ? # To #$ 0or num$ ? # To #$ "rint num# Anum$ 9e-t num$ "rint 9e-t num#

"age #$ of #$% &B% !T$'(% ) U*B% Instructor +r. Tsegay ,oges

Das könnte Ihnen auch gefallen