Control/Loop Structure QBASIC – QBasic Tutorial

ControlLoop Structure QBASIC - QBasic Tutorial

Control Structure

The process of departure of the control of the program Conditionally or Unconditionally from its sequential flow depending upon the result of the condition is known as Branching or Jumping.

Control Structure is the statement of the structure that is used to handle different condition and iteration. It is used to control the flow of the program. They are Sequential Structure, Selection Structure and Loop Structure.

Sequential Structure:

A structure in which the statements are executed sequentially one after another without changing the flow of the program.

Example,

CLS
INPUT A
INPUT B
LET S = A + B
PRINT " SUM OF NUMBER"; S
END

Selection Structure:

It is also known as a branching structure that allows you to transfer the program control from one part to another on the basis of a specified condition or without condition.  

Types of Selection Structure:

i)  If Statement  ii) Select Case Statement  

If Statement

This is a decision-making statement that decides which statement has to be executed on the basis of a specified condition.

CLS
INPUT A
INPUT B
LET S = A + B
PRINT " SUM OF NUMBER"; S
END

1. If…..Then Statement

It is one-way decision-making statement that evaluates a condition and executes the statement if the result of the condition id true.   Syntax,

If < Condition > Then
Statement 1
End If

2. If…..Then….Else Statement

It is the two-way decision-making statement that can decide which part of the computer it has executed when the condition is true or false. It executes one part of a program if the condition is true and another part if the condition is not true or false.   Syntax:

If <  Condition > Then
Statement 1
Else
Statement 2
End if

Write a program to check whether the number is divisible by 4 and 5

CLS
INPUT" ENTER THE NUMBER" ; N
IF N MOD 4 = 0 AND N MOD 6 = 0 THEN
PRINT" NUMBER IS DIVISIBLE IS DIVISIBLE BY 4 AND 6"
ELSE
PRINT" NUMBER IS NOT DIVISIBLE BY 4 AND 6"
END IF
END
Mod=(divisible sign)

3.  If…..Then….Else If……Else

It is a multi-way decision-making statement that is used when there are two or more conditions to be evaluated. Syntax

If < Condition 1 > Then
Statement 1
Else If < Condition 2 > Then
Statement 2
-----------------------
--------------------
Else Statement- N
End If

( N= Any Number)

Loop Structure

a. For……..Next Statement

It repeats a block of statement for a specified number of times. 1,2,3,4…..10

CLS 
FOR K = 1 TO 10
PRINT K ;
NEXT K
END

b. While…..Wend Statement

It executes a  block of statements repeatedly until the specified condition is true.   1,4,7,10…….25

CLS 
A = 1
WHILE A < = 25
PRINT A
A = A +3
WEND
END

c. Do….Loop Statement

It repeats a part of the program while a condition is true or until the condition becomes true. 100,90,80……up to 10

CLS 
S = 100
DO
PRINT A;
A = A -10
LOOP WHILE A > = 10
END

Nested Loop

The Loop inside the loop structure is called Nested loop. It contains an outer loop and an inner loop.

7  7  7  7  7  7  7  7
6  6  6  6  6  6  6
5  5  5  5  5  5
4  4  4  4  4
3  3  3
2  2
1

CLS 
FOR A = 7 TO 1 STEP -1
FOR B = 1 TO A
PRINT A;
NEXT B
PRINT
NEXT A
END