Professional Documents
Culture Documents
CH 11 Exercise Questions
CH 11 Exercise Questions
The computer executed the instructions in numerical order, from smallest line number to largest line
number until it reached last statement. However the order in which program statements are executed
can be changed using control statements i-e IFTHEN statement, ONGOTO statement. By this we
transfer the control from one part of program to another specified part of program.
Example:
10 READ A, B In above example line number 40
20 PRINT A, B transfers the control of program to
30 DATA 10, 20 line number 20.
40 GOTO 20
50 END
11.07 What is difference between conditional and unconditional transfer of control? Give example of
each type?
Conditional Transfer of control: When transfer is based on condition then it will be known as
conditional transfer. IFTHENELSE is the conditional transfer of control. In this control if the result of
expression is 1 (TRUE), the THEN is executed. If the result of expression is 0 (FALSE), the THEN is ignored
and the ELSE statement if present is executed, otherwise executions continuous with the next
executable statement.
Syntax: IF (condition) THEN (statement 1) ELSE (statement 2)
Example:
10 INPUT A
20 NPUT B
30 IF A = B THEN GOTO 10
40 ELSE 50
50 END
Unconditional transfer of control: GOTO statement causes unconditional transfer of control. It breaks
the normal sequential execution of program and transfer the control from one part of program to other
part of program unconditionally. GOTO statement instructs to go to a particular line number, this line
number is the one that appears after the word GOTO.
Syntax: Line # GOTO line number
Example:
10 READ A, B In above example line number 40
20 PRINT A, B transfers the control of program to
30 DATA 10, 20 line number 20.
40 GOTO 20
50 END
IFTHEN statement: IFTHEN statement is used to make decisions. IFTHEN statement with GOTO
forms a controlled loop in which repetition is predefined. The repetition continues until certain
condition is satisfied.
Example:
10 INPUT table of , S Output of program
20 N = 1 table of ? 7
30 P = S*N 7 1 7
40 PRINT S, N, P 7 2 14
50 N = N+1
60 IF N > 10 THEN GOTO 80 7 10 70
70 GOTO 40
80 END
11.12 What is the purpose of FOR statement in the FORNEXT Loop? Explain with example?
The purpose of FOR statement at the beginning of the loop is to name the counter and put its initial and
final values.
Example: K = Name of counter
50 FOR K = 1 TO 10 1 = initial value of counter
10 = finial value of counter
11.13 What is the function of NEXT statement in the FORNEXT Loop? Explain with example?
Function of NEXT in FORNEXT Loop: There are three functions of NEXT statement:
I. It indicates end of loop.
II. It gives an increment to counter variable.
III. Test the counter variable value.
Example:
10 FOR K = 1 TO 4
20 PRINT Pakistan zindabad
30 NEXT K
40 END
11.15 What is function of WEND in WHILEWEND statement?
WEND statement indicates the continuity of the loop. If the condition is false, the loop ends and the
program continue to line following WEND if there is a statement present.
11.14 how do WHILEWEND loop differ from FORNEXT loop?
FORNEXT LOOP WHILEWEND LOOP
1 It is a counter loop. It is a conditional loop.
2 It executes statements up to specified It executes statements as long as condition
number of times. remains true.
3 The number of repetitions are known in The number of repetitions are unknown in
advance. advance.
4 Syntax: Syntax:
FOR K = 1 TO 5 WHILE (condition)
Statements Statements
NEXT K WEND
5 Example: Example:
10 PRINT odd numbers 10 A =1
20 FOR K = 1 TO 100 STEP 2 20 WHILE A=10
30 PRINT K 30 PRINT A
40 NEXT K, 40 A++
50 END 50 WEND
60 END