(X) Ans of Ch-10 Exercise Review of Control structure

 Chapter-10

Review of Control Structure

  1. Fill in the banks:

  1. A never terminating loop is called an infinite loop.

  2. The for next loop does not work if the initial value is bigger than the final value and step is missing.

  3. WHILE-WEND loop works if the condition is TRUE.

  4. WHILE -WEND is a pre-test loop structure.

  5. DO-LOOP WHILE or DO LOOP UNTIL is a post-test loop structure.

  6. A loop within another loop is called nested loop.


  1. Write down the syntax and purposes of the following statements and functions:

  1. 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.


  1. 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.

  1. 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.


  1. 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.

  1. Answer the following questions:

  1. 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

  1. Differentiate between a sequential structure and a loop structure.

ANSWER: 

Sequential Structure

Loop Structure

1. The structure, where statements are executed one after another from top to bottom or they are executed sequentially without changing the path is called Sequential Structure.

1. The process of executing the same statement repeatedly until a condition is satisfied is called Loop Structure.

2. It has no types. 

2. There are three types of Loop Structure. They are FOR-NEXT, WHILE-WEND, DO-WHILE LOOP.


  1. 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.

  1. 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.

  1. Define counter or control variable.

ANSWER: A numeric variable that controls the number of repetitions is called counter or control variable.

  1. Differentiate between pre-test looping statement and post-test looping statement.

ANSWER:

Pre-Test Loop

Post-Test Loop

1. The loop structures which checks the condition before executing the statements of a loop structure is called Pre-Test loop.

1. The loop structures which checks the condition after  executing the statements of a loop structure is called Post-Test loop.

2. It may not execute any statement at all when the condition is false.

2. It executes the statements block at least once when the condition is false.

3. Example: FOR-NEXT , WHILE-WEND, 

                   DO WHILE Condition -  LOOP, 

                     DO UNTIL Condition -  LOOP

3. Example: 

DO - LOOP WHILE Condition , 

DO - LOOP UNTIL Condition







  1. Debug the following programs:

  1. 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


  1. 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

  1. Rewrite the following programs using WHILE-WEND and DO-LOOP UNTIL statements:

  1. 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





  1. 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







  1. Type the following programs, see the output and answer the given questions.

  1. 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

C

R =  N MOD C

C = C + 1

1

R = 5  MOD 1 = 0

1+1=2

2

R = 5  MOD 2 = 1

2+1=3

3

R =5  MOD  3 = 2

3+1=4

4

R =5 MOD 4 =  1

4+1=5

5

R = 5 MOD 5 =  0 

5+1=6 

( CONDITION FALSE)


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

  1. 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

C

F = F * C

C = C + 1

1

F = 1*1=1

1+1=2

2

F = 1*2=2

2+1=3

3

F = 2*3=6

3+1=4

4

F = 6*4=24

4+1=5

5

F = 24*5=120

5+1=6 

( CONDITION FALSE)


OUTPUT;  Enter a number : 5

Factorial of 5 = 120.






  1. 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

C

N

IF N > G THEN G = N

C=C+1

1

4

4 > 3 , G = 4

1+1=2

2

1

4 > 1 , G = 4

2+1=3

3

41

41 > 4 , G = 41

3+1=4

4

15

41 > 15 , G = 41

4+1=5

5

8

41 > 5 , G = 41

5+1=6

6

9

41 > 9, G = 41

6+1=7

7

13

41 > 13 , G = 41

7+1=8

8

12

41 > 12 , G = 41

8+1=9

9

10

41 > 10 , G = 41

9+1=10

10

55

55 > 3 , G = 55

10+1=11


OUTPUT: Greatest Number is 55



  1. 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.




  1. Write the programs to print the following series:

  1. 1,2,3….upto 10th terms

ANSWER:

CLS

FOR P = 1 TO 10

PRINT P

NEXT P

END


  1. 100,95,90,....upto 10th terms

ANSWER:

CLS

P = 100
FOR I = 1 TO 20

PRINT P

P = P - 5

NEXT I

END

  1. 1,4,9,......upto 10th terms

ANSWER:

CLS
FOR P = 1 TO 10

PRINT P ^ 2

NEXT P

END


  1. 1,5,9,13…….upto 20th terms

ANSWER:

CLS

P = 1
FOR I = 1 TO 20

PRINT P

P = P + 4

NEXT I

END

  1. 2,8,18,32…... upto 10th terms

ANSWER:

CLS

FOR P = 1 TO 10

PRINT P ^ 2 * 2

NEXT P

END


  1. 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


  1. 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



  1. 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



  1. 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. 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


  1. Write programs to display the following pattern:


  1. 12345

1234

123

12

1

ANSWER:

CLS

FOR P = 5 TO 1 STEP-1

FOR L = 1 TO P

PRINT L;

NEXT L

PRINT

NEXT P

END


  1. 55555

4444

333

22

1

ANSWER:

CLS

FOR P = 5 TO 1 STEP-1

FOR L = 1 TO P

PRINT P;

NEXT L

PRINT

NEXT P

END


  1. NEPAL

NEPA

NEP

NE

N

ANSWER:

CLS

P$ = “NEPAL”

FOR L = LEN(P$) TO 1 STEP-1

PRINT LEFT$ ( P$ ,  L)

NEXT I

END




  1. *N*

*NE*

*NEP*

*NEPA*

*NEPAL*

ANSWER:

CLS

P$ = “NEPAL”

FOR L = 1 TO LEN(P$) 

PRINT “ * ” ; LEFT$ ( P$ , L ) ; “ * ”

NEXT L

END

  1.  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



  1. 54321

5432

543

54

5

ANSWER:

CLS

FOR P = 1 TO 5

FOR L = 5 TO P STEP-1

PRINT L;

NEXT L

PRINT 

NEXT P

END



  1. 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

PRINT 

NEXT P

END






  1. 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  *****