CSCI 220 Programming With Visual Basic Lecture 5: Conditions and Looping

Conditions:
  Definition
    Expression that can be evaluated true or false
    NOT an equation
    Usually compares a value to a variable - can be variable to variable
  IF-THEN-ELSE
    Most basic condition
    used for simple branching
    You've already seen syntax
  SELECT CASE
    Used for multiple branching
    Tests one variable for multiple values
    Syntax:
    SELECT CASE variable
      CASE value1
        BASIC statements.....
      CASE value2
        BASIC statements.....
      CASE ELSE
        BASIC statements.....
    END SELECT
    Example
  DO...LOOP
    Used for conditional looping
    DO and LOOP keywords mark beginning and end of loop
    WHILE Condition
      Loop continues as long as condition is TRUE
      Loop ends when condition is FALSE
    UNTIL Condition
      Loop continues as long as condition is FALSE
      Loop ends when condition is TRUE
      Opposite of WHILE
    Placement of condition:
    DO:
      condition is evaluated before code is run
      it's possible to skip code
      usually necessary to initialize sentry variable
    LOOP:
      code runs at least once before condition checked
      initialization often occurs in code
      more commonly used
      Three requirements of good loop:
        Must have a sentry variable
        Must somehow initialize that variable
        Code inside loop must change sentry to allow loop to end.
 
  FOR/NEXT Loop
    Description:
      Simpler and older loop
      Used for counting
      You must know how many times to do the task
      (directly or in a variable)
    Syntax:
      FOR counting_variable = 1 to limit [STEP steps]
        BASIC code...
      NEXT counting_variable
    Example

Other techniques you will need
  CLS method
    Attached to Picture boxes, and forms.
    Allows you to clear the object
    Derived from old CLS command
    Syntax:  ObjectName.Cls
  Print Method
    Attached to Picture boxes, forms, and printer object
    Allows you to print a value directly to the object
    Derived from old PRINT command
    Syntax:  ObjectName.Print Value
    Value can be string or numeric, direct or variable name

Your project - The super looper

Description:
This program will illustrate looping and branching structures using conditions. You will create a program that demonstrates a number of different programming structures.
Getting to the program:
I have already created the form and project for you, and put them on your disk.
If I did NOT have your disk last weekend, see me before starting the project.
Starting up:  
  Open up A:VB5.Mak on your disk.
  Looper.FRM is already created and attached to VB5.
  It contains all the objects, and some documentation, but NO code.
Form:  Change the form's caption so your name is in it
Select - Case
  You have a horizontal scroll bar that can be 0,1,or 2.
  Write code so the text box (TxtSelCase) says "High", "Medium", 
    or "Low" depending on the value of the scrollbar
  Use a select case structure for this code.
For - Next
  Write code in the clear button that uses the .CLS method to clear 
    the picture box.
  Write code in the count button that displays a list from 1 to 10
  Use a for - next loop in this code.
Do Loops
  You have four labels with different loop structures in the captions
  Study the captions so you see the differences.  They are subtle.
  In each label, put code that follows the loop structure in its caption
  Note that the code is NOT complete in the caption.  You will need to flesh it out a bit.
  Also note that some will NOT work.  The point is to see which ones 
  do work and understand why
Save as looper.frm and vb5.mak

Return to Syllabus.