Control Structure In QBASIC Class 10 Computer Science
Introduction
A program is the
collection of statements that are arranged in a certain manner. The execution of
statements of the program normally takes place from the beginning statement to the
end statement in sequential order. The
order of execution of statements of a program is known as the flow of program.
The program flow depends on the statements used in the program. The statement
used in the program can change the flow of a program. The statement with changes the flow of a program is known as control
structure. There are three basic
control structures used in the construction of a program. They are:
Sequential
structure
Decision
(Selection) structure
Repetition (Loop)
structure
A program in QBASIC
may have one or a combination of three different programming structures. So, QBASIC
is also referred as a structural programming language.
1.
Sequential Structure
The sequential
structure consists of statements that execute one after another in a sequence of order. In this structure, the result of one statement is used by the next
statement of the program. In the sequential structure, there is no decision-making statement. The flow of the program in a sequential structure is always in
one direction and straightforward.
Flowchart of sequential structure |
2.
Decision (Selection) Structure
If the execution
of the statement of a program depends upon the result of condition, then such
type of a structure is known as decision or selection structure. If the
condition is true it executes one statement and executes another statement if
the condition is false. The branching i.e. transferring the program control
from one part to another part of the program is on the basis of the results of
the condition is known as conditional branching. IF….THEN and SELECT CASE
statement are used for selection structure.
If Statement
If statement is the
conditional branching statement that changes the order of execution of the
program on the basis of the specified condition. It decides which statement of
the program has to be executed among the statements of the program when the
result of the condition is true. If statement can be of one-way branching, two-way branching, and multi-way branching.
IF…………. THEN Statement
The one-way branching
of if statement evaluates the condition and executes a statement after THEN if
the condition is true.
Flowchart of IF....THEN structure |
Syntax:
IF <condition> THEN
Statement
OR
IF <condition> THEN
Block of
Statements
END IF
Where,
Condition is a logical
expression that evaluates either true or false.
Example:
REM deicide clever or
not
CLS
INPUT” Enter a number
greater than 10”; N
IF N>10 THEN PRINT
“You are clever”
END
IF….. THEN......ELSE Statement
This statement is two-way branching statement. This statement can decide which task is it has to
perform when the condition is true or false. That is, it executes certain
statement when the condition is true and another statement when the condition
is false.
Flowchart of IF....THEN...ELSE structure |
Syntax:
IF <condition> THEN
Statement1 ELSE Statement2
Or,
IF< condition> THEN
Statement Block1
ELSE
Statement Block2
END IF
NOTE:
Statement1 and
Statement Block1 will execute only when the condition is true.
Statement2 and
Statement Block2 will execute only when the condition is false.
Example:
REM decides you
can vote or not.
CLS
INPUT” Enter your
age”; Age
IF Age>=18
THEN
PRINT” You can
vote”
ELSE
PRINT” You cannot
vote”
END IF
END
IF….THEN…..ELSEIF…..ELSE Statement
This is multi
way branching form of IF statement. This statement is used when there are two
or more than two conditions to be evaluated.
Flowchart of multiple branching structure |
Syntax:
IF <condition1> THEN
Statement 1
ELSEIF
<condition2> THEN
Statement 2
ELSEIF
<condition3> THEN
Statement 3
………. ………..
……….. …………
ELSE
Statement N
END IF
Example:
REM find the greatest
number among three numbers.
CLS
INPUT” Enter first
number”; a
INPUT” Enter second
number”; b
INPUT” Enter third
number”; c
IF a>b AND a>c
THEN
PRINT” Greater is”;
a
ELSEIF b>c AND
b>a THEN
PRINT” Greater is”; b
ELSE
PRINT” Greater is”; c
END IF
SELECT…..END SELECT Statement.
This SELECT CASE Statement
is the selection structure statement. It executes one of the several statement
blocks depending on the value of an expression.
Syntax:
SELECT CASE
test-expression
CASE expressionlist-1
Statement Block 1
CASE expressionlist-2
Statement Block 2
…….. ……..
…….. ……..
CASE ELSE
Statement Block N
END SELECT
Where,
Testexpression is
any numeric or string expression.
Expressionlist is
one or more expressions that decide which statement block has to be executed
for testexpression.
The
expressionlist argument can have any of these forms or a combination of them,
separated by commas:
a.
Expression,
expression
Eg: 7, 8, 9, “a”,
“b”
b.
Expression To
expression
Eg: 20 TO 50, “A”
TO “E”
c.
IS Relational
Operator expression
Eg: IS=100,
IS>=555, IS”NEPAL”
Note:
·
If the textexpression matches the expressionlist
associated with CASE clause then the statement block following that CASE is executed.
·
The smaller value must be in first if you use TO
keyword for specifying range.
For example: -10 TO 5
, “A” TO “F”
·
The statement of CASE ELSE executes when all the
expressions list do not match with the test expression.
·
When there are more than one expressionist matches the
textexpression, the block statement of the expressionlist which satisfy the
textexpression first is executed.
Example:
REM counts number of
vowels and consonants in a supplied word.
CLS
INPUT” Enter a word”;
W$
R$=UCASE(W$)
FOR I= 1 TO LEN(R$)
E$=MID$(R$, I, 1)
SELECT CASE E$
CASE “A”, “E”,
“I”, “O”, “U”
Vcount=Vcount+1
CASE ELSE
ConsCount=ConsCount+1
END SELECT
NEXT I
PRINT” The number
of vowels in the word “; Vcount
PRINT “ The
number of consonants in the word “; ConsCount
END
GOTO Statement
GOTO statement
transfer the program control from one state to another statement without testing
condition that is unconditionally. The GOTO statement branches without testing
the conditions, so it is called unconditional branching statement.
Syntax:
GOTO {label}
Where,
label is the
name used as placeholder. It can be either number or alphanumeric. The label
name cannot be more than 40 characters in length and it must be terminated with
colon.
Example:
REM to guess a
correct number.
CLS
TOP:
INPUT” Enter a
number”; N
IF N=28 THEN
PRINT” Your guess
is correct”
ELSEIF N>20
AND N<30 THEN
PRINT” You are
about to get a number. Try again”
GOTO TOP
ELSE
PRINT” You are
far from the number. See you”
END IF
END
LOOP Structure
In a program,
sometimes we need to repeat a set of statements to a specified number of times or
till the condition is satisfied. The
repetition of a statement block during program execution is known as looping.
In the loop, after the execution of the last statement in the block, the program
control returns back to the first statement in the block again. The repetition
of a statement depends on the given condition or a specified number of times.
FOR….NEXT, WHILE…..WEND and DO….LOOP statements are used for looping.
Flowchart of LOOP structure |
Note:
If the termination of
the loop does not occur in any condition, then such loop is known as infinite
loop. To terminate the execution of infinite looping press CTRL+C or CTRL+
Break.
Terms used in LOOP structure
a.
Counter
A variable that counts
the number of times of a particular part of program has been repeated is known
as a counter. Normally, the initial value of the counter is set as 0 or 1. The
counter value is incremented by one for each repetition.
b.
Accumulator
A variable that stores
the result of arithmetic calculation is known as accumulator. The value of accumulator
is always set as 0 in the beginning.
c.
Flag
A variable that stores
are the true or false is known as Flag
FOR……NEXT Statement
The FOR….NEXT
statement repeats the set of statements to a specified number of times. This
statement is used when we know the number of times a set of statements to be
repeated.
Syntax:
FOR Loopvariable=
Start TO end STEP (increment/decrement)
Block of statements
NEXT Counter
Where,
·
Loopvariable is a numeric variable used as in loop counter. It is also known as a control variable.
·
Start is the initial value of the loop variable
·
End is the final value of the loop variable
·
STEP is an optional. STEP increment or decrement
indicates how is the value of the variable in each in the increased or
decreased. The default STEP value is 1.
Note:
1.
When the initial value of loop variable is greater
than the final value of variable the step value must be negative.
2.
The NEXT part of the statement perform two task:
·
It increases or decreases the value of loop variable
on the basis of step value. If the STEP value is positive, then it increases
the loop variable value by the step value otherwise it decreases the loop
variable value.
·
It returns the program control back to the FOR
statement
3.
The FOR statement check the return value of the loop
variable from the NEXT part is within the range of FOR or not. If it is within
the range, it executes the loop again.
Example:
REM displays “Welcome
to QBASIC” 10 times
CLS
FOR I = 1 TO 10
PRINT” Welcome to
QBASIC”
NEXT I
END
REM displays number
from 1 to 10
CLS
FOR I = 1 TO 10
PRINT I
NEXT I
END
REM displays sum of
first 10 even numbers and sum of those even numbers
CLS
N=2
SUM=0
FOR I = 1 TO 10
PRINT N;
SUM = SUM +N
N=N+2
NEXT I
PRINT” The sum of
first 10 even numbers”; SUM
END
REM to display first 100 natural numbers
CLS
FOR I = 1 TO 100
PRINT I;
NEXT I
END
WHILE……WEND Statement
This statement
executes a series of statement repeatedly till the specified condition is true.
Syntax:
WHILE <condition or
Boolean Expression>
Statement Block
WEND
Where,
Condition is an
expression that returns true or false
Note:
The statement inside WHILE
and WEND are executed only when the result of the condition is true. WHILE checks
the condition in the entry point and let the execution of the loop only when
the condition is satisfied. So, it is called an entry control loop. When the
program control encounters with WEND the program control returns back to the WHILE
part and it evaluates the condition with the new value.
Example:
REM this program repeats
statements till the user input “Y”
CLS
LET ans$=”Y”
CONST PI=22/7
WHILE <>
“N”
INPUT” Enter
radius”; r
A=PI*r*r
PRINT” Area of
circle”; A
INPUT” Do you
want to continue (Y/N)”; ans$
WEND
DO…LOOP Statement
This statement repeats
a part of program while the condition is true or until a condition becomes true.
The DO…LOOP statement has two different structures. They are in Entry control loop
and Exit control loop. The Entry control loop structure check the condition in
the entry point and let the education of the loop only when the condition is
true. In the case of an Exit control loop if first let to execute loop and
check the condition at exit point. So at least one time the loop is executed
even if the condition false.
Syntax 1: (Entry Control Loop Structure)
DO(WHILE/UNTIL)
<condition>
Statements Block
LOOP
Syntax 2: (Exit Control Loop Structure)
DO
Statements Block
LOOP
(WHILE/UNTIL) <condition>
Where,
Condition is the
logical expression that evaluates as true or false.
Note:
·
In the WHILE condition loop will continue till the
result of the condition is true.
·
In the UNTIL condition loop will continue till the
result of the condition is false.
·
If DO…LOOP does not have any condition, then the loop
is an infinite loop.
Example:
REM displays
number from 20 TO 10
N=20
DO WHILE N>=10
PRINT N;
N=N-1
LOOP
END
REM sum of digits
of an integer
CLS
SUM=0
INPUT” Enter a
number”; N
DO
R=N MOD 10
SUM= SUM +R
N=N\10
LOOP UNTIL N=0
PRINT” Sum of
digits”; SUM
END
Exit statement.
The exit statement
termites loop or procedure.
Syntax:
EXIT (DO/FOR/FUNCTION/SUB)
Note:
·
When we use EXIT DO, it directly exits from the DO…LOOP.
EXIT DO can be used only inside the DO…LOOP statement. When EXIT DO statement
is encountered the program control is transferred to a statement following the
LOOP statement.
·
When we use EXIT FOR, it provides an alternative from
a FOR…NEXT. EXIT FOR can be used only inside the FOR…NEXT statement. It
transfers the program control to the statement following the NEXT statement.
Example:
CLS
FOR P = 1 TO 15
PRINT P;
IF P = 8 THEN EXIT FOR
NEXT P
END
Nested Loop
sometimes a loop is
needed to use inside another loop. The loop inside a loop structure is called a
nested loop. Loops need to be nested when a repetitive task has to be turn
carried out repeatedly. The loop which contains other loop is called outer loop
and the loops which are inside are called inner loop. The nested loop structure
can be created by using FOR…NEXT, WHILE….WEND and DO…LOOP statements.
Nested Loop Structure for FOR….NEXT Statement
Syntax:
FOR loopvariable1 = start TO final (STEP
increment/decrement)
FOR loopvariable2=
start TO final ( STEP increment/decrement)
Statement Block
NEXT loopvariable2
NEXT loopvariable1
Example:
FOR P = 1 TO
5
FOR Q = P TO
5
PRINT Q;
NEXT Q
NEXT P
END
Nested Loop Structure for DO….LOOP Statement
Syntax:
DO
DO
Statements Block
LOOP(WHILE/UNTIL)
<condition2>
LOOP(WHILE/UNTIL)
<condition1>
Example:
CLS
P=1
DO
Q=P
DO
PRINT Q;
Q=Q+1
LOOP WHILE Q<=5
PRINT
P=P+1
LOOP WHILE P<=5
END
Nested Loop Structure for WHILE …..WEND Statement
Syntax:
WHILE<condition1>
WHILE<condotion2>
Statements Block
WEND
WEND
Example:
CLS
P=5
WHILE P>1
Q=1
WHILE Q<=P
PRINT Q;
Q=Q+1
WEND
P=P-1
PRINT
WEND
END
Click here to get QBASIC related Question Answer
Comments
Post a Comment