# QBASIC LOOP STATEMENTS

We use machines every day. Machines are very useful because they can tirelessly do things over and over again and as long as we want.

They do any task by using repeated motions or cycles.

Computer Programs also use cycles. Much of the usefulness of computer software comes from doing things in cycles.

In programming, cycles are called Iterationsor loops

When program has a loop in it. We can execute the same set of statements repeatedly.

Situations may arise when you need to perform the same task many times in a program.

For Instance, Consider a Problem where you need to get the same information for twenty students in a class.

Instead of same code twenty times in a program you can write a loop statement just once and it will be executed twenty times.

Introduction to LOOP Statements:

§  The cycles or iterations are very important in programming.

§  It is also possible to achieve iterations in QBASIC with the help of LOOP Statements.

§  The Following LOOP Statements are available in QBASIC:

FOR…..NEXT

DO…..WHILE

WHILE…WEND

DO….UNTIL

Types of LOOP Statements:

FOR…..NEXT LOOP:

§  The FOR…..NEXT LOOP is the fundamental looping statement in QBASIC.

§  It provides a way to repeat a block of statements over a predetermined number of times.

§  The FOR…..NEXT loop is used when we are required to perform repetitive tasks, such as to display addition of all values between 100 to 200, display all prime numbers between 1 to 100 etc.

§  The Syntax of the FOR…..NEXT loop statement is as follows:

§  FOR COUNTER_VARIABLE = STARTVALUE TO ENDVALUE STEP STEPVALUE

(Statement of Loop)

NEXT COUNTER_VARIABLE

where,

o    COUNTER_VARIABLE is a variable which controls the execution of the FOR…..NEXTloop.

o    STARTVALUE is the initial value of the COUNTER_VARIABLE.

o    ENDVALUE is the final value of the COUNTER_VARIABLE.

o    STEPVALUE is the amount by which theCOUNTER_VARIABLE value is modified each time through the loop.

o    RANGE is the set of values fromSTARTVALUE to ENDVALUE.

§  A basic FOR…..NEXT loop appears as follows:

FOR X = 1 TO 50 STEP 2

PRINT X

NEXT X

§  In this example X is theCOUNTER_VARIABLE, “1 to 50” is theRANGE and “2” is the STEPVALUE. The STARTVALUE in the range is 1 and ENDVALUE is 50.

§  The FLOW CHART of FOR…..NEXTloop statement is as follows:  WHILE…WEND LOOP:

§  The WHILE…WEND loop is the fundamental loop statement inQBASIC.

§  It provides a way to repeat a block of statements, as long as the condition is true.

§  It is used to perform a repetitive task based on a condition, such as to display first 10 prime numbers, search for a word in a text etc.

§  The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean value (TRUE or FALSE).

§  The Syntax of WHILE…WEND loop statement is as follows:

WHILE (TEST CONDITION)

(Statements OF Loop)

WEND

where,

o    TEST CONDITION is a numeric or string expression that evaluates whether TRUE or FALSE.

o    Statements OF Loop is a set of one or more QBASIC statements.

o    The WHILE Statement always checks the condition before it begins the loop.

o    LOOP continues till the condition remains TRUE.

o    The FLOW CHART ofWHILE….WEND loop Statement is as follows:  §  If TEST CONDITION is true, all statements within the loop are executed until the WEND statement is encountered.

§  Control then returns to the WHILEStatement and Condition is checked again.

§  If Condition is still true, then the process is repeated.

§  If the condition is false, then the loop statements are skipped and statement that appears after WEND statement is executed.

DO….WHILE LOOP:

§  DO….WHILE loop is used when we want to repeat a set of statements as long as condition is true.

§  The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean value (TRUE or FALSE).

§  The condition may be checked at the beginning of the loop or at the end of the loop.

§  The Syntax of DO….WHILE loop statement is as follows:

DO WHILE (TEST CONDITION)

(Statements Of Loop)

LOOP

where,

o    TEST CONDITION is numeric or string expression that evaluates whether TRUE or FALSE.

o    Statement of Loop is a set of one or more QBASIC Statements.

o    If the Condition is TRUE, all statements in Statement in Loop are executed until the Loop Statement is encountered.

o    Control then returns to theWHILE Statement and Condition is again checked.

o    If the Condition is still TRUE, Then the process is repeated.

o    If the Condition is FALSE, then the loop statements are skipped and the statement after LOOP statement is executed.

o    The FLOW CHART ofDO….WHILE loop Statement is as follows:  DO….UNTIL LOOP:

§  DO….UNTIL loop is used when you want to repeat a set of statements as long as the condition is false.

§  The condition may be checked at the beginning of the loop or at the end of the loop.

§  The Syntax of DO….UNTIL loop statement is as follows:

DO UNTIL (TEST CONDITION)

(Statements of Loops)

LOOP

Where,

o    TEST CONDITION is a numeric or string expression that evaluates whether TRUE orFALSE.

o    Statement of Loop is a set of one or more QBASIC Statements.

o    The FLOW CHART ofDO….UNTIL loop Statement is as follows:  EXIT Statement:

§  The EXIT Statement is used to terminate the loop based on certain criteria before the expected number of iterations.

§  When the EXIT Statement is executed, the control jumps to next statement immediately after the loop.

§  You can use the EXIT Statement in all types of loops.

§  The Syntax of EXIT Statement is as follows:

EXIT DO / FOR

§  The FLOW CHART of EXIT statement is as follows:  