How to Break Long Statements into Multiple Lines

Last reviewed: June 21, 1995
Article ID: Q94696
The information in this article applies to:

- Microsoft Visual Basic programming system for Windows, version 2.0 - Microsoft Visual Basic programming system for Windows, version 1.0 - The Standard and Professional Editions of Microsoft Visual Basic

  for MS-DOS, version 1.0
- Microsoft Basic Professional Development System (PDS) 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 reference words: 1.00 2.00 VBMSDOS QUICKBAS 4.50 BASICCOM 7.10
KBCategory: kbprg
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.