0% found this document useful (0 votes)
19 views

Looping in QBASIC

Uploaded by

Sanjay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
19 views

Looping in QBASIC

Uploaded by

Sanjay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
Learning Objectives amming nee EE en sme : ve these statements using et coe Oe 9. oe The outpat see ; 3 or a comment in the program that docs not get executed. satement is used to clear assign a value to a from the user and soreitin » The CLS st variable, LET statement is used to > The used to take a value > The INPUT statement is a variable. > ‘The IF...THEN...ELSE is statements depending upon a conditional statement that executes a set ‘a condition. INTRODUCTION TO LOOPS ou may want to tepeat the execution of a certain seta : ‘using loops. A Joop is used ® Statements th . We will eatt sing FOR-NEXT Statement ihe FOR.-NEXT statement is executed a specific numbe ‘The gene FoR al form of the FC Nex’ ‘the statements written be In the above-mentioned F > counter is a numeric called the control or the body gets executed. > step value is the valu decremented every time negative value, but it it is omitted, the coun ted. is exe Consider the following ex Initial vale Control tariable FORA = 1 PRINT “Hell NEXt Table 5.1 Step-b Value of A | 1 L— sping FOR-NEXT Statement for Looping FOR.-NEXT statement is used when you want a group of statements to be me a The ated a specific number of times. general form of the FOR...NEXT statement is given below e [STEP value The the statements written between FOR and NEXT form the body of the loop, in the above-mentioned FOR...NEXT statement. s a numeric variable that controls the loop and heace is also > counter i value changes every time the loop called the control or the index variable. I FOR...NEXT and OOP statements ASIC. g a loop, body gets executed. » step value is the value by which the counter variable is incremented or the loop body is executed. It can be a positive or a The step value is optional. In case ¢ the loop decremented every t negative value, but it cannot be ze variable is increased by one every tim: » itis omitted, the counte is executed following example based on FOR...NEXT loop. In this example, and store it in pie Control variable: A = Initial value of control variable: 1 cutes a set of T “Hello”, Final value of control variable: 3 Step value: 1 Body ofthe Loop Table 5.1. Step-by-step description of the above FOR...NEXT loop string “Hello” g Se es on the outpu i" nen the 100P ©"° the following examPlc er ‘The output of this progr nel 30 a step value Using the step yatue by which the control vary pecify the progran cis executed ; h In the following P should change > every time the body of the loop 6 ia, the control variable X changes The output of this program is: case, thevat ‘You can also assign a negative number as a step value. In thi of the control variable is decreased by the step value every time the lps executed, When the step value is negative, the initial value of the con variable is more than the final value. ‘The output of this program is: the inal and the fina z0t0, then the loop i ee The output of he progam is a) Alleven nurnbers ttm 110 JA Node numbers from 1 to 4 Let us consider some more Example 4: To add natural The output of this program is: Ifthe inital and the final values ofthe contro variable are equal and the step value is ze10, then the loop is executed once trol variable - changes by following programs and choose the correct answer 2 FRE ram is: consider the f The ouput ofthe progam 2) Numbers fom 3t0 50 in ascendingorder | 1 Numoers rom 1150 in desconcng oder “The output ofthe program is: 4) Ateven numbers from 41010, Jf Rods numbers ftom 4 t 10 Letus consider some more examples of FOR,..NEXT loop. Bample 1: ‘To add natural numbers from 1 to 20. : nunbers from 1 to 20 Be cammpea: toast tern 5 : “ You may not alv 4 The execution o situation, the ‘There are two varia 4. DO WHILE...LOOP 2. DO UNTIL...LOOP DO WHILE_LOOP ‘The DO WHILE...LOOP is exect The gen n of the DO WE The loop body comprising The statements written betwee loop. Let us look at an example of th of the statements may depend up the use of a DO...LOOP is preferred over a FOR...N are two vari 10... LOOP. 1, DO WHILE...LOOP 2, DO UNTIL...LOOP DOWHILE_LOOP Untitled WHILE...LOOP Example 4: To display odd m 1.52 Step-by-step description of the DO incremented to 3. Toop ends. | 1 y 1 Example 2: To add numbers ‘The above loop is executed as long as the value of A is less than 3, Ifthe condition in the DO WHILE... LOOP is not true, the loop is notes atall. Consider the following example. example 4: To display odd numbers less than 30, a0 PRINT “The sum of numbers = the DO UNTI the condition is false. L... LOOP is: The general form a set of statements to be executed ‘The loop comprising a set of Let us look at an example of this loop. ‘The output of this progany Fe 5.12 Oups ‘The above loop is executed as long as the condition A>3 is false or asl} os -ay> > Ais less than or equal to 3. . Table 53 Step-by-step description of the DO UNTIL...LOOP EXITING Leer For this, you use the E! EXIT DO statement in t The following program loop ends if the value of Pree Giunye ete aleatae he only ditfene Of he false or as longa 2, The default value ofthe step va at no 4) a posithe value Words to Know 1 5 + Loop: Ris usedtorepeet te execution of a group af statements many times. ] 6. What wil be the oxtput forthe + Loopingstatements are usetto repeat the execution ofa certain setoseenat : imustple bes. . a4 » +, FOR.NEXT and D0. L00P ar wo loping statements in QBASIC. zi : * TheFOR NEM statemest i usecwhenyouknow the umber ofunesseied vet Wirt the value taken by the contro has to be I

You might also like