Chapter-10
Review of Control Structure
Fill in the banks:
A never terminating loop is called an infinite loop.
The for next loop does not work if the initial value is bigger than the final value and step is missing.
WHILE-WEND loop works if the condition is TRUE.
WHILE -WEND is a pre-test loop structure.
DO-LOOP WHILE or DO LOOP UNTIL is a post-test loop structure.
A loop within another loop is called nested loop.
Write down the syntax and purposes of the following statements and functions:
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.
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.
DO-LOOP UNTIL
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.
DO-WHILE LOOP
Syntax: DO WHILE CONDITION
Statements Block
LOOP
Purpose: It repeats the block of statements while the condition is true or until the condition becomes true.
Answer the following questions:
Define a control flow structure. Write the names of basic control structures of QBASIC.
ANSWER: The statement which display one statement when the condition is true, otherwise display another statement is known as decision-making statement or selection statement . Since these statement “control” the flow of execution, they are also known as control statement.
There are two types of Control Structures of QBASIC:
IF Statement
Select Case Statement
Differentiate between a sequential structure and a loop structure.
ANSWER:
Define a loop structure. Write the importance of loop in programming.
ANSWER: The process of executing the same statement repeatedly until a condition is satisfied is called Loop Structure.
Importance of loop in programming are as follows:
i) It helps to repeat the same statement according to our requirement.
ii) It reduces the number of statement in a program.
What is an infinite loop? How do you break infinite loop?
ANSWER: Never terminating loop or the loop that does not end is called Infinite loop.
Press “Ctrl + Break ” keys to break infinite loop.
Define counter or control variable.
ANSWER: A numeric variable that controls the number of repetitions is called counter or control variable.
Differentiate between pre-test looping statement and post-test looping statement.
ANSWER:
Debug the following programs:
REM to print factors of an input number.
INPUT “Enter a number”;N
FOR K = 1 TO 5
IF N MOD K = 1 THEN PRINT N
NEXT I
END
ANSWER:
REM to print factors of an input number.
INPUT “Enter a number”;N
FOR K = 1 TO N
IF N MOD K = 0 THEN PRINT K
NEXT K
END
REM to print multiplication table of an input number.
INPUT “Enter a number ::::::”;N
FOR L = 1 TO 10 STEP-1
PRINT N; “*”; “=”; N*K
NEXT K
END
ANSWER:
REM to print multiplication table of an input number.
INPUT “Enter a number ::::::”;N
FOR K = 1 TO 10 STEP +1
PRINT N; “*”;K; “=”; N*K
NEXT K
END
Rewrite the following programs using WHILE-WEND and DO-LOOP UNTIL statements:
REM to repeat loop ten times
FOR I=10 TO 1 STEP-1
PRINT “Loop is repeating” ; I ; “time”
NEXT I
PRINT “End of Loop”
END
ANSWER:
REM to repeat loop ten times using WHILE-WEND
I = 10
WHILE I > = 1
PRINT “Loop is repeating” ; I ; “time”
I = I - 1
WEND
PRINT “End of Loop”
END
REM to repeat loop ten times using LOOP UNTIL CONDITION
I = 10
DO
PRINT “Loop is repeating” ; I ; “time”
I = I - 1
LOOP UNTIL I<1
PRINT “End of Loop”
END
REM example of loop
FOR N = 2 TO 20 STEP +2
PRINT N^3
NEXT N
PRINT “Loop Ended”
END
ANSWER:
REM example of loop using WHILE-WEND
N=2
WHILE N<=20
PRINT N^3
N=N+2
WEND
PRINT “Loop Ended”
END
REM example of loop using LOOP UNTIL CONDITION
N = 2
DO
PRINT N^3
N = N + 2
LOOP UNTIL N > 20
PRINT “Loop Ended”
END
Type the following programs, see the output and answer the given questions.
REM find the output
CLS
INPUT “ Enter a number ” ; N
C=1
WHILE C < = N
R = N MOD C
IF R = 0 THEN
PRINT C
END IF
C = C + 1
WEND
END
ANSWER:
Dry Run:
Enter a number : 5
If the value of C is 1 and 5 , it produces remainder 0 .That’s why 1 and 5 Will be display on screen.
OUTPUT; Enter a number : 5
1
5
REM find the output
CLS
INPUT “ Enter a number to find factorial” ; N
C = 1
F = 1
DO
F = F * C
C = C + 1
LOOP WHILE C < = N
PRINT “ Factorial of “ ; N ; “=” ; F
END
ANSWER:
Dry Run:
Enter a number : 5
OUTPUT; Enter a number : 5
Factorial of 5 = 120.
REM find the output
CLS
C = 1
INPUT “Enter First Number” ; G
DO
INPUT “Enter Next Number” ; N
IF N > G THEN
G = N
END IF
C = C + 1
IF C = 10 THEN EXIT DO
LOOP
PRINT “Greatest Number is” ; G
END
ANSWER: Dry Run:
Enter a number : 3
G = 3
OUTPUT: Greatest Number is 55
REM find the output
TOP:
CLS
INPUT “ Enter a number bigger than 1”; N
IF N < = 1 THEN GOTO TOP
FLAG = 0
FOR K = 2 TO N / 2
R = N MOD K
IF R = 0 THEN
FLAG = 1
EXIT FOR
END IF
NEXT
IF FLAG = 1 THEN
PRINT N; “IS COMPOSITE NUMBER”
ELSE
PRINT N; “IS PRIME NUMBER”
END IF
END
ANSWER: DRY RUN:
Enter a number bigger than 1 = 4
Step 1:
K = 2
R = 4 MOD 2 = 0
FLAG = 1
OUTPUT:
4 IS COMPOSITE NUMBER.
Write the programs to print the following series:
1,2,3….upto 10th terms
ANSWER:
CLS
FOR P = 1 TO 10
PRINT P
NEXT P
END
100,95,90,....upto 10th terms
ANSWER:
CLS
P = 100
FOR I = 1 TO 20
PRINT P
P = P - 5
NEXT I
END
1,4,9,......upto 10th terms
ANSWER:
CLS
FOR P = 1 TO 10
PRINT P ^ 2
NEXT P
END
1,5,9,13…….upto 20th terms
ANSWER:
CLS
P = 1
FOR I = 1 TO 20
PRINT P
P = P + 4
NEXT I
END
2,8,18,32…... upto 10th terms
ANSWER:
CLS
FOR P = 1 TO 10
PRINT P ^ 2 * 2
NEXT P
END
1,1,2,3,5,8,...... upto 10th terms [FIBONACCI SERIES]
ANSWER:
CLS
A = 1
B = 1
FOR P = 1 TO 10
PRINT A
C = A + B
A = B
B = C
NEXT P
END
2,8,15,23,32,....upto 10th terms
ANSWER:
CLS
A = 2
B = 6
FOR P = 1 TO 10
PRINT A
A = A + B
B = B + 1
NEXT P
END
9,28,14,7,22 until the number is 1.[HAILSTONE SERIES]
ANSWER:
CLS
P = 9
FOR I = 1 TO 20
IF P MOD 2 = 0 THEN
P = P / 2
ELSE
P=P*3+1
END IF
NEXT I
END
7,22,11,34,....upto 10th terms [HAILSTONE SERIES]
ANSWER:
CLS
P = 7
FOR I = 1 TO 10
IF P MOD 2 = 0 THEN
P = P / 2
ELSE
P=P*3+1
END IF
NEXT I
END
1,2,4,7,11, ...upto 10th terms
ANSWER:
CLS
A = 1
B = 1
FOR P = 1 TO 10
PRINT A
A = A + B
B = B + 1
NEXT P
END
Write programs to display the following pattern:
12345
1234
123
12
1
ANSWER:
CLS
FOR P = 5 TO 1 STEP-1
FOR L = 1 TO P
PRINT L;
NEXT L
NEXT P
END
55555
4444
333
22
1
ANSWER:
CLS
FOR P = 5 TO 1 STEP-1
FOR L = 1 TO P
PRINT P;
NEXT L
NEXT P
END
NEPAL
NEPA
NEP
NE
N
ANSWER:
CLS
P$ = “NEPAL”
FOR L = LEN(P$) TO 1 STEP-1
PRINT LEFT$ ( P$ , L)
NEXT I
END
*N*
*NE*
*NEP*
*NEPA*
*NEPAL*
ANSWER:
CLS
P$ = “NEPAL”
FOR L = 1 TO LEN(P$)
PRINT “ * ” ; LEFT$ ( P$ , L ) ; “ * ”
NEXT L
END
KATHMANDU
ATHMAND
THMAN
HMA
M
ANSWER:
CLS
A$ = “KATHMANDU”
C = 9
T = 1
FOR I = 1 TO 5
PRINT TAB( T ); MID$ ( A$ , I , 1 )
C = C - 2
T = T + 1
NEXT I
END
54321
5432
543
54
5
ANSWER:
CLS
FOR P = 1 TO 5
FOR L = 5 TO P STEP-1
PRINT L;
NEXT L
NEXT P
END
5
54
543
5432
54321
ANSWER:
CLS
FOR P = 5 TO 1 STEP-1
FOR L = 5 TO P STEP-1
PRINT L;
NEXT L
NEXT P
END
1
121
12321
1234321
123454321
ANSWER:
CLS
N# = 1
FOR P = 1 TO 5
PRINT N# ^ 2
N# = N# * 10 + 1
NEXT P
END
***** END OF CHAPTER - 10 *****