You are on page 1of 8
SNAP/RECAP Programming statements can be sequential, conditional or iterational. IP... THEN ... ELSE is used to execute a statement based on a condition. END IF is given to end the IF statement. ; ELSEIF allows executing a set of statements if the previous condition is false. 1 2 3. 4. CEARNING OBJECTIVES You will learn about: 1. FOR... NEXT 4. WHILE ... WEND 2. DO WHILE ... LOOP 5. EXIT Command 3. DO UNTIL... LOOP Introduction - Sometimes there is a need to repeat a set of statements more than once based on a certain Condition. This process of repetition is called Loop or Iteration in programming, A loop is, thus, used to repeat a block of statements a specific number of. time: loops are available in QBASIC: + DO WHILE... LOOP + FOR... NEXT + WHILE ... WEND * DO UNTIL... LOOP s. The following The FOR statements are best used to perform a loop a specific number of times, However, the DO ... and WHILE ... WEND statements are best used to perform a loop an undetermined number of times. Scanned with CamScanner FOR ... NEXT FOR ... NEXT structure is used when you want to perform a loop a specific nurmber of times, Ieuses a counter variable which is incremented or decremented with each repetition of the Joop. Syntax RK counter_variable = Start Value TO End Value S' StepValue Statement2; counter_variable ‘The explanation of the syntax is given below: + FOR statement is the beginnin of the loop. StartValue is the initial value of the counter_variable. EndValue is the value that the counter_variable must exceed to exit the loop. StepValue is the amount by which the counter is changed each time through the loop. NEXT statement is the end of the loop. STEP statement is given to specify the StepValue. If not specified, STEP defaults to one. This value can be either positive (when counter increases) or negative (when counter deer s). After all the statements in the loop have been executed, Step Value is added to the counter. At this value point, either the statements in the loop execute again (based on the same test that caused the loop to execute initially), or the foop is exited and execution continues with the statement following the NEXT statement. Sample Program 1:To print multiples of 5 from 0 to 30 The input and the corresponding output for Sample Program | is given in Table 4.1. ‘Table 4.1 Input - Output CLs 0 FORA =0TO 30 STEP 5S 5 PRINTA 10 NEXTA 15 20 25 30 a a eet} | tl Or) LD Scanned with CamScanner Se Sample in 1 and 10 Pi : betwee Togram 2:To display even numbers 0° given in Table 4.2, The i mput svowram 2 Patand the corresponding output for Sample Progra" 4 — Output Ree Table 4.2 np cLs ay FOR As PRINT A NEXT A TO9 st a ae Tf you w: , You want to create a countdown loop where the number is decremented with each loop simply use a negati ‘ative StepValue as shown in the following example. Sam ; — wie Program 3: To display countdown from 10 to 1 put and the corresponding output for Sample Program 3 is given in Table 4.3. i Table 4.3 Input — Output cLs 0 FORA =10TO1 STEP-1 9 PRINT A ' NEXTA 7 6 5 4 3 2 1 GD» na decrementing loop the starting number is greater tha i ae n the ending number Tr. A. t the square of numbers from 1 to 10 using FOR ... NEXT | ce loop. B. Print the odd numbers from 20 to 1 using FOR ++ NEXT loop, a eo Bae Scanned with CamScanner DO WHILE . LOOP 3 ... LOOP is performed as long : PeaA ements written within DO ... LOOP will be repeated till the condition is true, Every loop ends with the LOOP statement. Before the loop is ended, the counter variable must be incremented or decremented to avoid an infinite loop. A counter variable is used to control execution of a DO WHILE ... loop. It increments/ decrements each time through the loop. and is tested in the loop expression. A counter is good to use when you specifically know how many times you need to execute the loop. For example, in DO WHILE ... loop in Sample Program 1 count is used as a counter. Syntax DO WHILE test condition Statement! Statement2 as the condition being tested is true. It means LOOP Sample Program 4:To print numbers 1 to 10 The input and the corresponding output of Sample Program 4 is given in Table 4.4. ° cLs 1 count = 1 2 DOWHILE coump<= 19g ot 3 count =count +P —~ Pv ral Coun 4 LOOP 5 6 7 8 9 10 Try and observe the output for the as Try and observe the output for the following: a following: az! LETa=1 DO WHILE a>= 1 DO WHILE a<=10 PRINT a PRINT a+a aza-l Earl LOOP LOO! Scanned with CamScanner DO UNTIL aie atements until the condition Do ist UNTIL ... is different from DO WHILE... as it executes the st it exits the loop if Tue. In other words, it executes the statement if the condition is false and it ex , the condition is true, Syntax DO UNTIL test condition Statement! Statement2 LOOP Let us write a sample program using DO UNTIL ... LOOP. Sample Program 5: To print numbers 1 to 9 ‘The input and the corresponding output of Sample Program 5 is given in Table 4.5. Table 4.5 Input - Output a | CLs count = | veTze DO WHEE count = 10 count = count #1 Loop ——— Print count Coed Anew ne D> 10 is not printed in this program as the statements get executed only till the condition is false. As soon as the value of count becomes 10, statement is true and the Joop ends. WHILE ... WEND While...wend works just like any other loop. It executes a series of statements as long as specified condition is true. Syntax WHILE test condition Statements WEND a Ba Scanned with CamScanner Sample program 6: To generate multiples of 5 from 5 to 50 The input and the corresponding output of Sample Program 6 is given in Table 4.6, Table 4.6 Input - Output cLs 5 LET num =5 10 WHILE num <=50 15 PRINT num 20 num = num +5 25 WEND 30 © 35 40 45 50 EXIT Exit command is used to come out of a loop before the expected number of executions. EXIT command is used followed by either FOR or DO. Sample Program 7: To exit after 3 while printing 1 to 5 ‘The input and the corresponding output of Sample Program 7 is given in Table 4.7. Table 4.7 Input - Ouput Lie > a Sa cLs FORM=1TOS PRINT M,esptatttd IF M=3 THEN EXIT FOR wre NEXT M A. Print multiples of 10 from 10 to 100 using WHILE ... WEND. B. Print the sum of 1 to 5 natural numbers using DO WHILE ... LOOP. + The FOR ... NEXT structure is used when you want to perform! pe number of times. It uses counter variable which is incremented Or with each repetition of the loop. . + ADO WHILE... LOOP is performed as long as the conditio + DO... UNTIL LOOP is different from DO WHILE as it executes the statements until the condition is true. + The purpose of DO ... LOOP and WHILE ... WEND is similar except for the syntax. + To come out of a loop before the expected number of execution, is used followed by either FOR or DO. n being tested is true. EXIT command € @ Fillinthe blanks. 1. FOR... NEXTisa type of QBASIC Statement. is used to end DO loop. works similar to DO ... WHILE. is used to repeat certain steps a fixed number of times. 2. 3 4, 5. The DO... LOOP can be used either with WHILE statement or .. statement. : © Answer the following questions. : 1. Whatis an iteration in QBASIC? Give examples. When do you use FOR... NEXT? Write a small code to support your answer What is the purpose of WHILE ... WEND in QBASIC? Write a small code. latnhababbaeahaehcadanheanteateahetaabtabenbalobtebtnahstehatesbteen” What is the difference between a DO WHILE and a DO UNTIL statement? Why do you use EXIT command? Give example. Scanned with CamScanner @ Find out the errors in the following programs: 41> 1 A=1 2. FORX=10T01 DO UNTILA <= 10 PRINT X PRINT A x A=A+1 LOOP Loop © © Give the output of the following programs: 1. TEACHER'S NOTES Tell the students that two loops can be used together. We can Nest one loop inside another. The outside Joop is evaluated first and then the nested loop is processed and evaluated. Demonstrate with an example. cls 2. M=1 FORY=1TO50step5 DO until M>=10 PRINT Y, Print M NEXTY M=M+1 LOOP .. Sarah wants to generate an even number series from 10 to 20, Which loop should she use? Write the code for this program. Write program to generate an odd number series from . Rita has been given a task of writing the multiples of 7 statements should she write to display these on the QBASIC screen? STEP -1 100 to 130. from 70 to 140. What @ Scanned with CamScanner

You might also like