How to Break Long Statements into Multiple Lines

ID: Q94696


The information in this article applies to:
  • Microsoft Visual Basic programming system for Windows, versions 2.0, 1.0
  • Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0
  • Microsoft Basic Professional Development System for MS-DOS, version 7.1
  • Microsoft QuickBASIC for MS-DOS, version 4.5


SUMMARY

This article describes how to break lengthy control-flow statements such as IF/THEN statements or WHILE loops into multiple shorter statements while retaining their functionality. There is no line continuation character in Basic or Visual Basic. It is useful to break up lines of code so they are easy to view in the edit window without scrolling and are within the compiler's (BC.EXE) line limit of 255 characters.


MORE INFORMATION

The following examples show how to use temporary variables to break up an IF/THEN statement and a WHILE loop into multiple shorter lines:

The IF/THEN statement is a control-flow statement that branches if a condition is true. A long IF/THEN statement such as:


   MAX = 3
   VALUE = 2
   CURRENTVALUE = 1

   IF ((VALUE > CURRENTVALUE) OR (VALUE < CURRENTVALUE)) AND (VALUE < MAX)
      THEN 'Combine with previous line -- Should all be on a single line
      PRINT "VALUE is not equal to CURRENTVALUE and less than MAX"
   END IF 
Can be broken down using temporary variables to:

   MAX = 3
   VALUE = 2
   CURRENTVALUE = 1

   TEMPVAL = (VALUE > CURRENTVALUE) OR (VALUE < CURRENTVALUE)
   TEMPVAL = TEMPVAL AND (VALUE < MAX)
   IF TEMPVAL THEN
      PRINT "VALUE is not equal to CURRENTVALUE and less than MAX"
   END IF 
These two code fragments are equivalent. They evaluate and execute the PRINT statement under the same conditions.

The following demonstrates the same technique with a WHILE loop:

   MAX = 10
   VALUE = 5
   CURRENTVALUE = 1

   WHILE ((VALUE > CURRENTVALUE) OR (VALUE < CURRENTVALUE)) AND (
   VALUE < MAX) ' This should all be on one line
      MAX = MAX - 1
   WEND
   PRINT "Out of WHILE Loop" 
This is the revised version using temporary values:

   MAX = 10
   VALUE = 5
   CURRENTVALUE = 1

   TEMPVAL = (VALUE > CURRENTVALUE) OR (VALUE < CURRENTVALUE)
   TEMPVAL = TEMPVAL AND (VALUE < MAX)
   WHILE TEMPVAL
      MAX = MAX - 1
      TEMPVAL = (VALUE > CURRENTVALUE) OR (VALUE < CURRENTVALUE)
      TEMPVAL = TEMPVAL AND (VALUE < MAX)
   WEND
   PRINT "Out of WHILE Loop" 
In both code examples, the TEMPVAL variable contains a value of 0 or -1 to signify a logical TRUE or FALSE.

Additional query words: 2.00 VBMSDOS QUICKBAS 4.50 BASICCOM 7.10

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: September 17, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.