Chapter-18 Control Structure:
1. Fill in the blanks :
2. Write syntax and purpose of the following statements:
a. SELECT -END SELECT Syntax: SELECT CASE Test-Expression CASE Expression-list statement Block 1 CASE Expression-list statement Block 2 CASE Expression-list statement Block 3 CASE ELSE statement block 4 END SELECT Purpose: It is a control flow statement that executes one of the several statement blocks depending on the value of an expression, which may be string or numeric type. b. IF - END IF Syntax: IF (condition 1) THEN statement block 1 ELSEIF (condition 2) THEN statement block 2 ELSEIF (condition 3 ) THEN statement block 3 ELSE statement block 4 END IF Purpose: In the above case if the condition 1 is true, the statement Block 1 will be executed and execution will exit from the IF-END IF block. If the condition 1 is false, then it checks the condition 2. If the condition 2 is true, the Statement Block 2 will be executed and after that, execution will exit from the IF-END IF. IF the condition 2 is false, then it checks the condition 3. The ELSE block of statements will be executed if all conditions are false. The ELSE clause is optional if you do not include it and if the given conditions are false, the IF-END IF structure does not give any result. c. FOR - NEXT Syntax: FOR Counter = start value to final value[ step increment] The block of statements that will be executed once for each iteration of loop NEXT [counter] Purpose: It is used to execute a series of instruction a given number of times. d. WHILE-WEND Syntax: WHILE (condition) Program Statement Increase / Decrease the value of the control variable WEND Purpose: To execute a series of statements in a loop as long as the given condition is true. When the value of the condition switches to false, the loop stops. e. DO UNTIL - LOOP Syntax: DO Statement block LOOP UNTIL CONDITION Purpose: To executes while the condition remains false. It is a post - test structure , which executes at least once. f. DO- LOOP WHILE Syntax: DO Statement Block LOOP WHILE <condition> Purpose: Used to execute the condition remains true. It is a post -test loop structure, which executes the statement block at least once.
3. Answer the following questions:
a. What is control flow structure? Write three basic control structures of QBASIC. Ans: The structure which is used to transfer the flow of a program from one part to another depending upon the conditions is called control flow structures. There are three types of basic control structures are: 1. Sequential Structure 2. Selection Structure 3. Loop Structure b. Define a loop structure. List the looping statements supported by QBASIC. Ans: The process of repeating same statements until a condition is satisfied is called loop structure. There are three types of looping statement supported by QBASIC and they are: 1. FOR NEXT statement 2. WHILE - WEND statement 3. DO WHILE LOOP statement c. Differentiate between selection structure and loop structure.
Selection Structure | Loop Structure |
---|---|
Selection structures, also known as conditional statements, are used to make decisions in a program. They allow the execution of different code blocks based on whether a specified condition is true or false. | Loop structures are used to repeat a certain block of code multiple times until a specified condition is met. They help in automating repetitive tasks. |
Examples: Common selection structures include if, else if, and else statements. Switch statements are also a form of selection structure. | Examples: Common loop structures include for loops, while loops, and do-while loops. |
d. How loop structure reduces the program codes? Ans: The situation may arises when you need to perform the same task for a number of times. For instance, consider a problem where you need to get the information for ten different students. Instead of writing the same tedious code ten times, you can repeat a loop ten times. In this way, loop reduced the program codes. e. What is counter or control variable? Explain with an example. Ans: A numerical variable that controls the number of repetitions is called counter or control variable. For example, FOR I = 1 to 10 ..... NEXT I f. Differentiate between pre-test looping and post-test looping statements with flowcharts.
Pre Test Looping | Post Test Looping |
---|---|
1. Pre Test Loop structure checks the condition before executing the statements of loop. | 1. Post Test Loop structure checks the condition after executing the statements of loop. |
2. A pre test structure may not execute at all when the condition goes false. | 2. A post test structure executes at least once , when the condition goes false. |
Flow charts is given below:
g. What is nested loop? Write its structure.
Ans: A loop that lies within another loop is called nested loop. A nested loop contains an outer loop and the inner loop.
Structures of Nested Loop:
FOR A
FOR B
NEXT B
NEXT A
4. Draw flowcharts for the following:
a. To input three different numbers and to print the smallest number
b. To print the greatest number among ten different input numbers
c. To print whether the input number is odd or even
d. To find the sum of all numbers from 1 to N, where N is an input value.
e. To find the sum of all even numbers between 1 to 100.
f. To print the series 3, 6, 10, 15, 21, .... up to 10th terms
g. To input percentage and to print division using the following conditions:
Percentage | Division |
---|---|
Greater than or equal to 60 | First |
In between 50 to 59.9 | Second |
In between 40 to 49.9 | Third |
Below 40 | Fail |
5. Rewrite the following programs using Do-LOOP WHILE and DO-LOOP UNTIL
statements: (a) REM to repeat the loop ten times CLS PRINT "Beginning of loop" FOR I = 1 TO 10 PRINT " Loop is repeating "; I ; " time " NEXT I PRINT "End of loop" END Answer:Using DO-LOOP WHILE | Using DO - LOOP UNTIL |
---|---|
CLS PRINT "Beginning of loop" P = 1 DO PRINT " Loop is repeating " ; P ; " time " P = P + 1 LOOP WHILE P<=10 PRINT "End of Loop" END | CLS PRINT "Beginning of loop" P = 1 DO PRINT " Loop is repeating " ; P ;" time " P = P + 1 LOOP UNTIL P>10 PRINT "End of Loop" END |
(b) REM Example of FOR-NEXT Statement FOR N = 10 TO 1 STEP -2 PRINT N^3 NEXT N PRINT " Loop Ended " END Answer:
Using DO-LOOP WHILE | Using DO - LOOP UNTIL |
---|---|
CLS N = 10 DO PRINT N^3 N = N - 2 LOOP WHILE N>=1 PRINT " Loop Ended " END | CLS N = 10 DO PRINT N^3 N = N - 2 LOOP UNTIL N<=1 PRINT " Loop Ended " END |
6. Debug the following programs:
(a) REM To print first 20 even numbers from 100 to 50 and find their sum. L=1 REM Counter N=100 REM Initial value of N S=0 REM Variable to store sum DO PRINT N S=S-N N=N+2 L=L-1 WHILE L<=20 PRINT "SUM=";N END
(b) REM To input sales amount and find the commision. INPUT "Enter sales amount of salesman::::::";AMT SELECT CASE AMT CASE 2,000 TO 5,000 C = (AMT*10%) CASE 5001 TO 10,000 C = (AMT*15)/100 CASE IS >=10001 C = (AMT*100)/20 ELSE CASE PRINT "No Commision" SELECT END END CORRECTED PROGRAM:
7. Write programs to print the following series:
(i) 1,2,3, ....up to 10thterms. Ans: CLS FOR I = 1 TO 10 PRINT I NEXT I END (ii) 100,98,96,....20thterms. CLS A=100 FOR I = 1 TO 20 PRINT A A = A - 2 NEXT I END (iii) 1,4,9 ....up to 10thterms. CLS FOR I = 1 TO 10 PRINT I^2 NEXT I END (iv) 1,5,9,13, ....up to 20thterms. CLS A=1 FOR I = 1 TO 20 PRINT A A = A + 4 NEXT I END (v) 2,8,16,32, ....up to 10thterms. CLS FOR i= 1 TO 10 PRINT 2^i NEXT I END (vi) 1,2,3,5,8, ....up to 10thterms. CLS A = 1 B = 2 FOR I = 1 TO 10 PRINT A C = A + B A = B B = C NEXT I END (vii) 4,5,9,14,23 ....up to 10thterms. CLS A = 4 B = 5 FOR I = 1 TO 10 PRINT A C = A + B A = B B = C NEXT I END (viii) 10,5,16,8,4,2,1,4,2,1 CLS A = 10 FOR I = 1 TO 10 PRINT A R = A MOD 2 IF R = 0 THEN A = A / 2 ELSE A = A * 3 + 1 END IF NEXT I END (ix) 2,2,4,6,10,...up to 10thterms. CLS A = 2 B = 2 FOR I = 1 TO 10 PRINT A C = A + B A = B B = C NEXT I END (x) 2,5,9,14,20,...up to 10thterms. CLS A = 2 B = 3 FOR I = 1 TO 10 PRINT A A = A + B B = B + 1 NEXT I END
8. Write programs to print the following patterns:
(a) 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 | CLS
FOR I = 5 TO 1 STEP-1
FOR J= 1 TO I
PRINT J;
NEXT J
PRINT
NEXT I
END |
(b) 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 | CLS
FOR I = 5 TO 1 STEP-1
FOR J= 1 TO I
PRINT I;
NEXT J
PRINT
NEXT I
END |
(c) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | CLS
S$ = "**********"
A=LEN(S$)
FOR I = A TO 1 STEP-2
PRINT LEFT$(S$,I)
NEXT I
PRINT LEFT$(S$,1)
END |
(d) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | CLS
A$ = "**********"
PRINT LEFT$(A$, 1)
L=LEN(A$)
FOR I = 2 TO L STEP 2
PRINT LEFT$(A$, I)
NEXT I
END |
(e) 1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 | CLS
K=0
FOR I = 1 TO 5
FOR J = 1 TO 3
PRINT J+K;
NEXT J
PRINT
K=K+1
NEXT I
END |
(f) 5 4 3 2 1 5 4 3 2 5 4 3 5 4 5 | CLS
FOR I = 1 TO 5
FOR J = 5 TO I STEP -1
PRINT J;
NEXT J
PRINT
NEXT I
END |
(g) 5 5 4 5 4 3 5 4 3 2 5 4 3 2 1 | CLS
FOR I = 5 TO 1 STEP -1
FOR J = 5 TO I STEP -1
PRINT J;
NEXT J
PRINT
NEXT I
END |
(h) 1 121 12321 1234321 123454321 | CLS
A# = 1
FOR I = 1 TO 5
PRINT A# ^ 2
A# = A# * 10 + 1
NEXT I
END |
No comments:
Post a Comment