Microsoft KB Archive/94696

= How to Break Long Statements into Multiple Lines =

Article ID: 94696

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic for MS-DOS
 * Microsoft Cinemania 97 Standard Edition
 * Microsoft QuickBasic 4.5 for MS-DOS

-



This article was previously published under Q94696



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 &quot;VALUE is not equal to CURRENTVALUE and less than MAX&quot; 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 &quot;VALUE is not equal to CURRENTVALUE and less than MAX&quot; 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 &quot;Out of WHILE Loop&quot; 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 &quot;Out of WHILE Loop&quot; 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: KB94696

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.