CIS310 Introduction to Programming


<< back to courseware demo page
- This online lecture is for demonstration purposes

 

Lecture Three

This lecture will cover the topics of control structures (elementary selection and repetition), and of general procedures in Visual Basic. The menu to the right provides links to the major topics of the lecture as well as to the assignment page for this week. You may also scroll down to begin reading the lecture material.

 

Lecture Menu

Control Structures

Top Down Design

Arguments

Running Programs

Quizzes

Assignment

Control Structures
 

The three basic control structures are sequence, selection and repetition.

  • Sequence means that program statements are performed one after the other in order (unless another control structure takes precedence).
    • This control structure is built in to the Visual Basic language and most other computer languages.
  • Selection means that one part or another of a program is executed, based on the outcome of a test for which part should be the one executed.
  • Repetition means that a part of a program is to be executed over and over until a criterion for stopping the repetition is satisfied. 

Selection
 

  • Selection involves making a choice.
    • This is sometimes called branching (choosing one branch or the other).
  • Simple branching is done in Visual Basic with the If- Then-Else construct. This can be viewed as follows: 


If <condition> Then 

        <true block> 

Else 

        <false block> 

End If 

This causes the statement(s) in <true block> to be performed if <condition> is true; otherwise the statement(s) in <false block> are performed. This flow chart may help to visualize the situation. 


Repetition
 

  • Simple repetition (looping) may be implemented in Visual Basic by the Do While construct.
  • The action of the Do While is as follows: 
Do While <condition> 
        <loop body> 

Loop 

 

The above causes <condition> to be tested.

  • If true, the statement(s) in <loop body> are performed and <condition> is again tested.
  • As long as <condition> remains true, <loop body> is performed over and over.
  • If <condition> ever tests false, <loop body> is no longer repeated and control is transferred to the statement after <loop body>.
  • Statements in <loop body> will normally cause <condition> to become false at some point, or else the loop would continue forever.
  • If it does continue forever, it is called an infinite loop; this is a common programming error.
Note that if <condition> comes out false on the first test, then <loop body> is never executed.

Thus a Do While loop may execute:

  • zero times under some circumstances,
  • finitely many times under others,
  • and infinitely often under others (or until the computer is turned off or rebooted).
This flow chart may clarify the action of a Do While loop. 

Conditions in If-Then-Else and Do While control structures often make use of comparison (or  relational) operators, which are explained below. 
 

Comparison Operators in VB 
 

         Common VB comparison operators (all of equal precedence): 

Operator -- Operation: 

     = -- equals 
     <> -- is not equal 

     < -- less than 

     > -- greater than 

     <= -- less than or equal 

     >= -- greater than or equal 

The comparison operators are lower in precedence than the arithmetic operators and higher in precedence than the boolean operators (Not, And, Or, Xor, Eqv and Imp, explained in Lecture IV). 
 


Top Down Design 
 

Procedures are the primary structuring tool for programs.

  • A complicated task may be broken down into subtasks, and a subprogram (procedure) written to perform each subtask.
  • The procedures can then be combined into a program that performs the original task.
  • This may be done repeatedly; that is, the subtasks may be broken down themselves, and so on, until the resulting subtasks are simple enough to solve directly.
  • At this stage writing a procedure for the subtask should be easy (but sometimes it is still hard).
  • This decomposition of tasks into subtasks is the basis of top down design. 
VB programs are by their nature composed of at least event procedures like Form_Load, Form_Click and Command1_Click.
  • They may also contain general procedures.
  • Event procedures begin execution when a certain event (e.g. clicking the form) occurs.
  • General procedures can only begin execution by being called from another procedure.
  • You may think of this as temporarily stopping the first (calling) procedure and then running the called procedure.
When the called procedure finishes:
  • It is said to return to the caller.
  • The calling procedure starts running again at the point where it left off.
  • We use the term caller to mean the calling procedure.
  • A procedure may be called many different times from many different places, so the caller may be different for different calls or invocations of a procedure.
VB has two kinds of procedures; Subs and Functions. For now we will be concerned primarily with Subs; event procedures are all Subs. 

To illustrate a procedure call, consider the example below: 
 

Sub example() 
        'execution gets here only after call 

        'code for procedure goes here 

End Sub  'control returns to caller 

Private Sub Form_Click() 
        'code for 1st part of Form_Click 

example 'procedure call; control transferred 

        'execution resumes here when procedure returns 

        'code for remainder of Form_Click 

End Sub 

This flow chart may be of help in understanding the flow of control during a procedure call and return. 
 

Arguments
Arguments are used by a procedure to communicate with the rest of the program.
  • Using arguments, data can be sent into the procedure by the caller or from the procedure back to the caller.
  • Data may be sent to the procedure by value arguments; 
  • data sent back to the caller by the procedure must be sent via reference arguments.
For example, in the procedure heading: 

Sub proc (ByVal x, ByVal y, n, m) 
 

  • x and y are value arguments since they are preceded by the keyword ByVal;
  • n and m are not and are therefore reference arguments (the VB default).
  • x and y can be used to send data into the procedure;
  • such arguments are called input arguments because the data is being input to the procedure.
  • This is not the same usage of the word "input" as in "input from the keyboard" since the data is coming from the caller and not from outside the program.
  • n and m can be used to get data out of the procedure and back to the caller.
  • Such arguments are called output arguments;
  • similarly the word "output" does not mean the same as in "output to the screen."
  • n and m could also be used as input arguments or as both kinds, in which case they are called input/output arguments or update arguments. 
If the procedure proc above changes the values of x or y these changes would be lost when the procedure returns, but changes to the values of n and m would be retained when it returns. See VB Help on the Sub statement. 

Running Programs


 

Save your program before running it.

  • A program may cease to respond to user input;
  • in this case we sometimes say the program (or the computer) hangs.
  • You can normally stop a program running in VB by pressing the break or end buttons on the toolbar. 
  • Otherwise you may be able to force the program to quit by holding down one of the Ctrl keys and pressing Break (or Pause). 
  • Ctrl-C will sometimes work also.
  • Occasionally Esc may get you out of a hanging program.
  • If the preceding do not work you may have to use Ctrl-Alt-Del, press the reset button, or turn off the machine.
  • In such cases the program code (or its most recent version) would be lost unless the project had been saved before the run.
  • Ctrl-Alt-Del is sometimes humorously called the three fingered salute. 
Quizzes

You will be tested on the Visual Basic language and software 
engineering concepts like top down design, and not on Windows or the VB editor. If you need to know the meaning of certain 

computer terms, look them up in the index of the text. Also you should look in the index to find references to more detailed 

discussions. You will need to know how to evaluate Visual Basic expressions like those in section 4.4 of this above, and others 

using the VB predefined functions Abs, Cint, Fix, Sqr and Int which are explained in the text. Example: 

Cint(Sqr(Abs(-10))) = 3. 

You will be expected to be able to predict the output of short programs or code fragments, and to be able to write short programs 
and procedures to accomplish specified tasks. You should be able to correct simple syntax errors and answer short questions on 

topics such as algorithms, value and reference arguments, procedures, If-Then-Else, Do While and the data types covered above 

in Lecture II.

 
 

SAMPLE QUIZ 
 

1.  What will be printed on the form by the statement below if the value of xray is: 
a) 75 

b) -3 

c) 37 

If xray <= 30 Then 

      Print "Not over 30.  The value is"; xray 

Else 

      Print "Over 30.  The value is"; xray 

End If 

 

2.  For the following code answer the question below, assuming the form has been clicked. 

Private Sub Form_Click () 
 x = Val(InputBox("How old are you?")) 

 If x > 45 And x < 60 Then Text1 = "Middle age." Else Text1 = "Not so middle." 

 If x <= 68 Then 

   Text2 = "You can retire in " & 68-x & " years." 

 Else 

   Text2 = "Happy retirement!" 

 End If 

End Sub 

What do Text1 and Text2 display following user input of (a) 45 (b) 69 (c) 68 to the input box? 

a) Text1__________ b) Text1___________ c) Text1____________ 

 Text2____________  Text2____________  Text2____________ 
 

3.  What is displayed on the form, when clicked, by the following? 
Private Sub Form_Click( ) 

x = 3 

Do While x > 1 

 If x Mod 2 = 1 Then 

  x = 3 * x + 1 

 Else 

  x = x \ 2 

 End If 

 Print x 

Loop 

End Sub 

 
 
 

ANSWERS: 

1. 
a) Over 30.  The value is 75 

b) Not over 30.  The value is-3 

c) Over 30.  The value is 37 

2. 
a) Not so middle. 

You can retire in 23 years. 

b) Not so middle. 
Happy retirement! 

c) Not so middle. 
You can retire in 0 years. 

3. 
10 


16