Page 1 :
In the chapter*, • GOTO statement, • IF THEN ELSE statement, 4, • Using loops, • DO LOOP, • WHILE WEND loop, Conditional statement, and looping in QBASIC, • Nesting of loops, • QBASIC programs, A basic program in QBASIC is that in which each and every step is, followed and executed by a computer. Let us learn to write some, programs where all statements are not executed. Only some of the, statements are executed based upon the satisfaction of the condition, given in the program. Some of these statements are GOTO, IF...THEN.., ELSE, FOR...NEXT, DO WHILE....LOOP, DO...LOOP, WHILE and WHILE...WEND., smart, TIP, GOTO, A GOTO statement is used to tell the computer to jump to, a particular section in a program to continue the program., When the computer reads the GOTO statement, it moves to, the line number mentioned there and whatever is written in, between those lines is ignored by the computer., Line numbers are used when, control of the program is, to be jumped to a different, section. In other cases, QBASIC, understands and executes, the program correctly even if, there are no line numbers., Syntax, GOTO 80, (where 80 is the line number), IF...THEN...ELSE, An IF...THEN....ELSE statement is used to check a condition and then decide the result., Since the result is based on the outcome of a condition, it is known as a conditional, statement. The IF section contains the condition which is to be checked. The THEN, section states the steps to be followed if the condition is true. The ELSE section states, the steps to be followed if the condition is false., ntax, IF A > B THEN GOTO 50 ELSE GOTO 80, Let us understand the use of an I...THEN...ELSE statement with the help of, these programs., For the teacher: Demonstrate the difference in the output when the comma (,) and semi-colon (;) are used, in the PRINT and INPUT statements., 43, Conditional statement and looping in QBASIC
Page 2 :
Program 1: Program to find the greater of two numbers, 10 CLS, 20 INPUT "Enter first number : ", A, 30 INPUT "Enter second number : ", B, 40 IF A > B THEN GOTO 50 ELSE GOTO 70, 50 PRINT A; "is greater", 60 GOTO 80, 70 PRINT B; "is greater", 80 END, Program 2: Program to find if a person is eligible to vote or not, 10 CLS, 20 INPUT "Enter your name : ", AS, 30 INPUT "Enter your age : ", B, 40 IF B>=18 THEN GOTO 50 ELSE GOTO 70, 50 PRINT A$, "You ar e eligible to vote", 60 GOTO 80, 70 PRINT A$, "You are not eligible to vote", 80 END, Using loops, A loop is created in a program for performing repetitive jobs. Loops allow a group, of statements to be executed repeatedly until a certain condition is fulfilled., QBASIC offers the following three types of looping statements:, 1. FOR...NEXT, 2. DO...LOOP, a. DO WHILE...LOOP, b. DO LOOP...WHILE, 3. WHILE...WEND, The dots shown in these loops denote the statements constituting the body of the, loop. These are the statements meant to be repeatedly executed till the condition is true., Each loop works in a slightly different way and is useful in different situations., FOR...NEXT loop, A FOR...NEXT loop is used when you know exactly how many times the loop has to be, repeated. A FOR statement is placed at the beginning of a loop, and NEXT at its end., The syntax of a FOR... NEXT loop is:, FOR I=<Initial Value> TO <Final Value> Step<Increment/Decrement>, ..., Executable statements, Increment The amount by which something increase, Decrement The amount by which something decrease, ..., NEXT I, Executable Something that can be performed, 44, Conditional statement and looping in QBASIC
Page 3 :
Structure of the FOR...NEXT loop, 1. A FOR loop uses a variable called a counter variable (here, 1). The value of counter, variable increases or decreases during each repetition of the loop. The counter variable, is a numeric variable that controls the number of times the loop is executed. The first, time the loop is executed, the counter variable takes the initial value. The counter, variable increases or decreases by the specified step value each time the loop is, repeated. By default, the initial value increases by 1 in each step., 2. The final value or end value is the number that controls the end of the looping, process. When the counter value exceeds the end value, the loop is terminated. The, computer then starts executing the statement immediately after the NEXT statement., 3. The FOR loop ends with a NEXT statement. The NEXT statement is used to:, e Signal the end of the loop., eIncrease or decrease the counter, and then transfer the control to the FOR, statement to compare with the end value., 4. The statements that are to be repeatedly executed till the condition is true, are, written between the FOR and NEXT statements. These statements make the body, of the loop., Program 3: The following code prints an asterisk (*) in each one of the, ten rows., FOR 1=1 TO 10, PRINT "*, NEXT I, In this program, 'T' assumes the initial value of 1. Then, the statement PRINT, is executed. It prints the asterisk (*) on the screen and moves to the statement, NEXT I. The value of I is increased by 1 and the control of computer moves to, FOR I=1 to 10 statement, the statement PRINT *" is executed again. This way, * is printed 1o times., The program and its output is shown in Figure 4.1., (11), FAle Edit, Vieu Search Tin De bug Opt iens, Wntitled, FOR 1IO LO, PRINI, HEKT I, (a) Program, (b) Output, A Fig. 4.1 Program and its output, 45, Conditional statement and looping in QBASIC
Page 4 :
Smart, TIP, You can use a FOR...NEXT loop to write a simple program. A simple program, can be either one that prints statements or simple calculations. This can be, done in two ways., Suppose you want to execute some statements five times. You can use, two methods to write the loop:, OFirst method, • Second method, FOR 1=5 TO1 STEP -1, Executable statements, FOR I=1 to 5, Executable statements, NEXT I, NEXT I, ww., Program 4: Write a program to print the first ten natural numbers., FOR X=1 TO 10, PRINT X, NEXT X, Output: This program will execute the PRINT X statement till it reaches the end, value of X, that is, 10. Thus, the numbers 1 to 10 will get printed, as X, gets incremented by one, each time the loop is executed., Program 5: Write a program to print the numbers 1 to 30 in reverse order., FOR A=30 TO 1 STEP -1, PRINT A, NEXT A, Output: The FOR statement will first assign the value 30 to A. The PRINT A, statement will be executed till it reaches the end value of A, that is, 1., Thus, the numbers from 30 to 1 will get printed, as A gets decremented, by one, each time the loop is executed., Activity in the lab, ACTIVITY Write a program to print the first ten natural numbers and their sum., HINTS:, SUM = 0, FOR A=1 TO 10, PRINT A, SUM = SUM + A, NEXT A, PRINT "THE SUM OF FIRST TEN NATURAL NUMBERS IS", SUM, END, 46, Conditional statement and looping in QBASIC
Page 5 :
Class Activity, Answer these questions., 1. Fill in the blanks with the most appropriate answer(s)., a. A, is created in a program for performing repetitive jobs., b., and, are the three, types of loops., C. The, the loop has to be repeated., d. In a FOR NEXT loop, the, times the loop is executed., loop is used when you know exactly how many times., variable controls the number of, e. The counter variable increases or decreases by the, each time the loop is executed., value, 2. Write a FOR loop to print the even numbers from 1 to 50 in reverse order., Label the Control variable, Initial value, Final value, step and body of loop., 3. Write the output of the following program:, FOR I=1 TO 100 STEP 10, PRINT I, PRINT ", NEXT I, DO...LOOP, A DO...LOOP repeats the statements present in its loop body depending upon the given, condition which is placed either after DO (at the entry) or after LOOP (at the exit)., DO WHILE...LOOP, A DO WHILE...LOOP repeats as long as the condition evaluates to be true. This loop is, used when it is required that a condition be tested first and if the condition is true,, only then the statements in the loop body should be executed., Syntax of DO WHILE...LOOP is, DO WHILE (<test condition>), ..., Executable statements, ..., LOOP, Structure of DO WHILE...LOOP, The structure of DO WHILE...LOOP is as follows:, 1. It begins with a DO WHILE statement. This statement is used to define the condition, that is to be checked., 47, Conditional statement and looping in QBASIC