QBASIC – Difference between WHILE and FOR loop

As we know, QBASIC supports different types of looping structures; WHILE…WEND, FOR…NEXT and DO…LOOP WHILE. Since, WHILE…WEND and DO…LOOP WHILE has much significance, let’s understand the difference between WHILE…WEND loop and FOR…NEXT loop in QBASIC.

FOR loop provides a concise way of writing the loop structure. A FOR loop contains the loop variable initialization, condition of loop termination, and increment/decrement of loop variable in a single line whereas, in a WHILE loop, the loop variable is initialized first then the condition is provided with WHILE statement, and the loop variable is incremented/decremented inside the loop.

The next major difference is, FOR loop is used when we know how many times the loop needs to execute i.e., a fixed number of times whereas the WHILE loop is used when the number of times the loop is executed is unknown.

Leave a Comment

Your email address will not be published. Required fields are marked *