Control Stuctures

Q.1: – Define control structure. How many control structures are available in BASIC, discuss briefly.

ANS. Control Structures

Control structures controls the flow of execution of a program.

Types of control structures

In BASIC language there are three types of control structures. All programs use some of these control structures to implement the program logic.

These control structures are as following:

i. Sequence Structure ii. Selection Structure iii. Loop Structure

i- Sequence Structure.

In a sequence control structure instructions are executed according to the increasing order of their line numbers. So the instructions at smaller line numbers are always executed first, then instructions at greater line numbers.

Example

10 A=5

20 B=20

30 c=30 4

40 SUM=A+ B +c

50 PRINT SUM

60 END

OUTPUT 

55

In the above program statements are executed in the same sequence in which they are written.

ii- Selection Structure

A selection structure chooses -which alterative program statements to execute. IF. …THEN, and IF. …THEN. . ..ELSE statements are used to implement selection structure.

iii- Loop Structure

Loop structure is used to repeat a number of statements up to specified number of times or until a specified condition is fulfilled.

Types of Loop

There are two types of loop, which are as following:

i. Counter Loop       ii. Controlled Loop

 

Q.2: – What does it mean by transfer of control? Briefly describe conditional and unconditional transfer of control in BASIC.

ANS – Transfer of control

In GW-BASIC, program control can be transferred from one part of the program to the other specified part of the program conditionally or unconditionally.

i- Unconditional Transfer of control .

In unconditional transfer of control the program control switches to a specific line by skipping one or more lines without any condition. GOTO is the unconditional transfer of control statement.

ii- Conditional Transfer of control

In conditional transfer of control the program control switches to a specific line number by skipping one or more program lines depending on a certain condition. In GW-BASIC, IF-THEN, IF-THEN-ELSE statements are used to conditionally transfer the control from one part of the program to the other.

Q.3: – Describe ON-GOTO statement with examples

ANS.:ON- GOTO Statement

It is a multi branching statement. It is favored when there are several alternatives rather than a single action. Unlike GOTO statement which allows only one transfer point, the ON-GOTO statement can have more than one transfer points, thus providing multiple branching facilities.

Syntax

Line N0. ON Variable or expression GOTO N1, N2, N3…. 

Interpretation

The expression is a valid BASIC expression and N1, N2, N3 …. are the valid line_numbers in the program where‘ the control will be transferred. The range of value of numeric variable or expression is 0 to 255.

In the ON-GOTO statement, the value of expression determines which  line number in the list will be used for branching. For example, if the value is 2, the second line number in the -list will be destination of the branch. In the same if the value is 3, the third line number in the list will be destination of the branch And so on. If the value is a non integer, the fractional portion is rounded. If the value is less or more than the count of line numbers following the GOTO statement then an “OUT OF RANGE” error message will be displayed.

Example:

10 CLS

20 INPUT “Enter First Number. .

30 I INPUT “Enter Second Number.=”,B

40 _ INPUT “l-ADD, 2-SUB, 3-MUL,4-DIV”;C

50 ON C GOTO 60, 70, 80, 90

60 PRINT A + B: END

70 PRINT A – ‘B: END.

80 PRINT A * B: END

90 PRINTA/B:END

Q.4: – Describe ON ERROR GOTO statement with examples

ANS.: ON ERROR GOTO Statement

ON ERROR GOT O statement enables error trapping feature of GW- BASIC and specify the first line of error handling routine. The ON ERROR GOTO statement suppresses error message, as soon as an error occurs, it transfers control to the line number specified, whereas ON ERROR OFF statement no longer suppresses error message

Syntax:

ON ERROR GOTO Line Number

Once error trapping has been enabled, all errors detected by GW-BASIC, including direct mode errors cause GW-BASIC to branch to the line number in the program that begins the specified error handling subroutine. GW-BASIC branches to the line specified by the ON ERROR statement until a RESUME statement is found. If line number does not exist, an “Undefined line” error results. To disable error trapping, execute the following statement.

ON ERROR GOTO0

Subsequent errors print an error message and halt execution. If an error occurs during execution of an error handling subroutine, the GW-BASIC error message is printed and execution terminated. Error trapping does not occur within the error handling subroutine. The error number is stored in the variable

ERR. The line number at which the error occurred is stored in the variable ERL

Program-1

10′ A=0

20 I B=30/A

30 PRINTB

40 END – E

OUTPUT Y

When we run this program it will give following error message

DIVISION BY ZERO AT LINE 20

Program-2 

10 ON ERROR GOTO

20 A=0

30 B=23/A

40 PRINT B

50 PRINT “YOU CANNOT DIVIDE ANY NUMBER BYO”

60  END

70 PRINT “ERROR CODE=”, ERR

80 PRINT “OCCURS AT LINE”, ERL

90 ooro 50

OUTPUT 

ERROR CODE=18

OCCURS AT LIN E=3O

DIVISION BY ZERO

YOU CANNOT DIVIDE ANY NUMBER BY 0

Q.5 – Explain IF THEN Statement.

Ans.:- IF THEN Statement

It is a decision making statement. It is used to make a decision regarding program flow based on the result returned by an expression. IF-T HEN statement transfers control to a specific portion of a program depending on the specified condition. A condition is an expression that either evaluates to true (usually represented by l) or false (usually represented by 0). It determines weather the specified condition is true or false. If the condition is true then statement will be executed. The condition of an IF statement can be a relational expression, a logical expression or a combination of the two.

Syntax

IF expression THEN Statement

IF expression THEN Line Number

If the expression 1s true then either

the statement at the specified line number  or the statement following the THEN False keyword is executed. If an IF-THEN.  statement is followed by a line number in the direct mode, an “Undefined line number” error results, unless a statement with the specified line number was previously entered in the indirect mode.

Example-1

Write down a program that will tell that number is less than 25 or greater than 25

10 INPUT “Enter Number=”; A

20 IF A<25 THEN PRINT”

Number is less than 25”

30 IF A>25 THEN PRINT”

Number is greater than 25”

40 END

Q.6: – Explain IF. . .THEN . ; ..ELSE Statement with examples.’

Ans.:- IF. . .THEN  .ELSE Statement

The IF-THEN-ELSE statement is a decision making statement as it decides the path of the program. It helps in making comparisons and testing whether a condition is true or not. The keyword ELSE is used to specify two different alternatives with IF statement. Based on a condition, one of the two alternatives is executed. IF is always followed by a valid BASIC condition or expression. If statement determines weather the condition is true or false. If it is time then statementl will be executed otherwise statement 2.

Else part of the statement is optional and can be omitted. The condition of an If statement can be a relational expression, a logical expression or a combination of the two. If the ELSE portion is not used in the IF statement and if the condition is false then the control will be transferred to the next executable statement after the IF statement .

Syntax

IF (Expression) THEN IF (Expression) THEN Line No

Statements (True Task) ELSE

ELSE Statements (False Task)

Statements (False Task)

 

Example

Write a program, which read two numbers and print the bigger number

Program

10 CLS

20 READ N1,N2

30 IF Nl>N2 THEN PRINT N1;“ larger No” ELSE PRINT N2; ”larger No”

40 ‘ DATA 50,25 ”

50 END

Example

Write a Basie program which finds the largest number of given three numbers.

Program

10 CLS

20 INPUT “FirstNumber …… .;=”;N1;

30 INPUT “Second Number. . . i=”; N2

40 INPUT “Third Number ….. ..:=”’ N3 A‘

50 IF (N 1>N2)AND (N 1>N3) THEN PRINTNI ,“ Largest N0”

60 IF (N 2>N 1 ) AND (N 2>N3) THEN PRINT N2,“ Largest No” ELSE

PRINT N3“ Largest N0 ”

80 END

Q.7: – Explain the use of Logical operators in BASIC.

Ans.:- Use of Logic Operators in BASIC

Logical operators perform tests on multiple relations, bit manipulation, or Boolean operations. The logical operators return a bit wise result which is either true or false. In an expression, logical operations are performed after arithmetic and relational operations.

Logical operators let us combine simple conditions to construct more complex ones. By condition we mean an expression evaluating to true or false value. Logical operators are used to make Logical, expressions. Logical operators are used for taking decisions depending upon some logical condition.

Types of Logical Operators

There are three logical operators, which are as following:

AND      .OR   .  NOT

i. AND Logical Operator

The logical AND operator when combines two conditions, evaluates to time if both the conditions are true, otherwise, it evaluates to-false value.

ii- OR Logical Operator

The logical OR operator when combines two conditions, evaluates ton time if any one of the conditions is true, otherwise, it evaluates to false value.

iii- NOT Logical Operator

The third logical NOT operator when applied to a condition, reverse the result of the evaluation. It means that if the condition evaluates to true, the logical NOT operator evaluates to false and vice versa. In an expression, logical operations are performed after arithmetic and relational operations.

Q.8: -Describe FOR-NEXT loop with examples

ANS: – For Next  loop 

FOR-NEXT loop is used to execute a series of instructions up to a specified number of times. This loop executes the set of statements written between for and next statement. When it is known in advance how many times the loop must be repeated the FOR-NEXT loop is the most effective option.

Syntax:

FOR Variable = x to y [Step z]

Body of the loop

NEXT [Variable]

Interpreter:

The variable name following EOR is called the loop control variable or loop variable. It is used as counter. x and y are numeric constants where x is the initial value for the counter and y gives the final value of the counter, STEP z specifies the counter increment for each loop. The increment can be negative also.

The initial and final values may each can be constant, variable or an expression. Program lines following the FOR statement are executed until the NEXT statement is encountered. Then the counter is incremented by the amount specified by STEP option. If the STEP is not specified, the incrementis assumed to be 1.

A check is performed to see if the value of the counter is now greater than the final value (y). If it is not, GW-BASIC branches back to the statement after the FOR statement, and the process is repeated. If it is greater, execution continues with the statement following the NEXT statement.

The body of the loop is skipped if the initial value of the loop times the sign of the step exceeds the final value times the sign of the step. If STEP is negative, the final value of the counter is set to be less than the initial value. The counter is decrement each time through the loop, and the loop is executed untiln the counter is less than the final value.

Short Questions

Q.1:- Define control structures

Ans.:-Control Structures

Control structures control the flow of execution of a program.

Q.2:- Name the types of control structures.

Ans.:-Types of Control Structures

The types of control structures are as following:

0 Sequence control structure 0 Selection control structure

0 Loop control structure

Q.3:- What is sequence control structure?

Ans.:-Sequence Control Structure

In sequence control structure instructions are executed according to the increasing order of their line numbers. So the instructions at smaller line numbers are always executed first, then instructions at greater line numbers.

Q.4:- Define transfer of control

Ans.:- Transfer of Control ‘ I

In GW-BASIC, during the execution of a program the program control can be transferred from one part of the program to another specified part of the program conditionally or unconditionally.

Q.5:- What is meant by conditional transfer of control?

Ans.:- Conditional Transfer of Control

In conditional transfer of control the program control switches to a specific line number by skipping one or more program lines. depending upon a certain specified condition.

Q.6:-What is meant by unconditional transfer of control?

Ans.:- Unconditional Transfer of Control

In unconditional transfer of control the program control switches to a specific line number by skipping one or more program lines without any specified condition.

Q.7:- Define GOTO Statement

Ans.:- GOTO Statement

This statement is used to unconditionally transfer control from a program line to a specified lineout of the normal program sequence.

Q.8:-What is the use of ON-GOTO statement?

Ans.:- ON-GOTO Statement

ON-GOTO is a multi branching statement. The ON-GOTO statement can have more than one transfer points. It is used for making of menus.

Q.9:-What is the use of ON ERROR GOTO statement?

Ans.:- ON ERROR GOTO statement

ON ERROR GOTO statement enables error trapping feature of GWBASIC and specify the first line of error handling routine.

Q.10:- Define selection structure

Ans.:- Selection Structure

A selection structure chooses which alternative program statement(s) to execute. IF»-THEN and IF-TEHN-ELSE statements are used to implement selection structure.

Q.11:- What is the use of IF-THEN statement?

Ans.:- IF-THEN Statement

IT is a decision making statement, depending upon the decision; it can change the order of program execution. IF-THEN statement is used to select a path flow in a program based on a condition.

Q.l2:- What is condition?

Ans.:- Condition e

A condition is an expression that either evaluates to true (usually represented by 1) or false (usually represented by 0)

Q.13:- What is the use of IF-THEN -ELSE statement?

Ans.:- IF-THEN-ELSE Statement

IF-THEN-ELSE is a decision making statement as it decides the path of the program. The keyword ELSE is used to specify two different alterative with IF statement. Based on a condition, one of the two alternatives is executed. It helps in making comparisons and testing whether a condition is true or not.

Q.14:-Define loop

Ans.:-Loop

A Loop is a technique to execute set of statements repeatedly.

Q.15:-~What is the use of FOR-NEXT loop?

Ans.:-FOR-NEXT Loop

FOR-NEXT loop is used to execute set of statements up to fixed number of times. It is a counter loop. In this loop the number of repetition are known in advance.

Q.16:-What is the use of WHILE-WEND loop?

Ans. :- WHILE-WEND loop

WHILE-WEND loop is a conditional loop. It executes set of statements.

until an associated condition becomes false.’ In this loop the programmer doe  not know in advance how many times the loop will be executed. .

Q..17 :- What is nested loop?

Ans.:- Nested Loop

Loop inside the-loop is called nested loop. Nesting can be done‘ in both  FOR-NEXT and WHILE-WEND loops.

Q.18:- Differentiate between FOR. . .NEXT loop and WHILE. . .WEN  loop?

Ans.:- Difference between. FOR-NEXT and WHILE-WNED Loop

WHILE  WEND loop is used when the number. Of iterations is not known in advance and it checks the looping condition and decides whether 0 not to continue. Whereas in FOR  NEXT loop starting and final values are known. It is used when the number of iterations is known in advance.

Please follow and like us:
error