(IX) Ch-18 Control Structures

1. Fill in the blanks : a. program ,...condition b. one after another c. sequential, sequential and iteration (loop) d. if and select e. false f. pre test g. post test h. nested loop 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 chart 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 QN 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

ans: 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.

ans e. To find the sum of all even numbers between 1 to 100.

ans f. To print the series 3, 6, 10, 15, 21, .... up to 10th terms

ans 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


QN 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

QN 6) Debug the following programs: (a) REM To print first 20 even numbers from 100 to 50 and find their sum. L=1 ‘Counter N=100 ‘Initial value of N S=0 ‘Variable to store sum DO PRINT N S=S-N N=N+2 L=L-1 WHILE L<=20 PRINT "SUM=";N END
CORRECTED PROGRAM:
CLS L = 1 'Counter N = 100 'Initial value of N S = 0 'Variable to store sum DO PRINT N S = S + N N = N - 2 L = L + 1 LOOP WHILE L<=20 PRINT "SUM=";S 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:
CLS INPUT "Enter sales amount of salesman::::::";AMT SELECT CASE AMT CASE 2000 TO 5000 C = (AMT * 10) / 100 PRINT "COMMISION AMOUNT IS" ;C CASE 5001 TO 10000 C = (AMT * 15) / 100 PRINT "COMMISION AMOUNT IS" ;C CASE IS >=10001 C = (AMT * 20) / 100 PRINT "COMMISION AMOUNT IS" ;C CASE ELSE PRINT "No Commision" END SELECT END
QN 8)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,18,32, ....up to 10thterms. CLS FOR I = 1 TO 10 PRINT I^2*2 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 9. 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
QN 10) Write programs in QBASIC for the following: a. Write a program to input two different numbers and print the smallest number. CLS INPUT " Enter two numbers : " , A,B IF A<B THEN PRINT A;" is smallest number " ELSE PRINT B; " is smallest number " END IF END b. Write a program to input three different  numbers and print the greatest numbers. CLS INPUT " Enter three different numbers : " , A,B,C IF A>B AND A>C THEN PRINT A ; " is greatest number " ELSEIF B>A AND B>C THEN PRINT B ; " is greatest number " ELSE PRINT C ; " is greatest number " END IF END c. Write a program to input three different numbers and print the middle number CLS INPUT "Enter three numbers : " , A, B, C IF A > B AND A < C OR A < B AND A > C THEN PRINT A; " is middle number " ELSE IF B > A AND B < C OR B < A AND B > C THEN PRINT B; " is middle number " ELSE PRINT C; " is middle number " END IF END d. Write a program to input ten different numbers and find the smallest number. CLS C = 1 INPUT " Enter First number : " , S DO INPUT " Enter next number : " , N IF N < S THEN S = N C = C + 1 IF C = 10 THEN EXIT DO LOOP PRINT " Smallest number is : " ; S END e. Write a program to input ten different numbers and print the greatest number. CLS C = 1 INPUT " Enter First number : " , G DO INPUT " Enter next number : " , N IF N > G THEN G = N C = C + 1 IF C = 10 THEN EXIT DO LOOP PRINT " Greatest number is : " ; G END f. Write a program to input ten different numbers and find the sum of the even numbers. CLS SUM=0 FOR I = 1 TO 10 INPUT " Enter next number : " , N IF N MOD 2 = 0 THEN SUM = SUM + N END IF NEXT I PRINT " Sum of even numbers is : " ; SUM END g. Write a program to input ten different numbers and find the sum of the even and odd numbers separately. CLS FOR P = 1 TO 10 INPUT " Enter next number : " , N IF N MOD 2 = 0 THEN EVEN = EVEN + N ELSE ODD = ODD + N END IF NEXT P PRINT " Sum of even numbers is : " ; EVEN PRINT " Sum of odd numbers is : "; ODD END
End of Chapter-18