Friday, 7 March 2014

Control Statements

Control Statements in BASIC

The control statements control the flow of statements execution within the program. The decision block controls the statements to be executed, while the repetition block controls the repeated execution o one or more statements.

Goto statement

Goto is a control statement that can change the order of execution of statements. Goto causes the control to transfer anywhere within the program.

Program

10 Input “enter your name”;Name$

20 Print “welcome”;Name$

30 Goto 10

40 End


Output

Enter your name Badal khan

Welcome Badal khan

Conditional transfer to control

To make decision regarding program flow based on the result of an expression we transfer the control conditionally.

a)If…Then statement

If….Then statement is used to make decisions. If…Then statement with Goto forms a controlled loop in which repetition is performed. The repetition continues until certain condition is satisfied.

Program

10 print “enter your name”, Name$

20 if Name$=”Javid” Then Goto 40

30 Goto 10

40 “welcome Javid”

50 End


Output

Enter your name rasheed

Enter your name Javid

Welcome Javid

b) If…Then…Else statement

If…Then…Else statement transfers control to another part of the program when the conditon is false. The format of If…Then…Else statement is:-

Line # If (condition) Then (statement 1) Else (statement 2)

Program

10 Print “enter your marks percentage”; m

20 if m>=60 then G$= “first” : Goto 50 Else

30 if m>=45 then G$= “Second” : Goto 50 Else

40 if m>=33 then G$= “Third” : Goto 50 Else

50 print “your division is”, G$

60 End

On…Goto statement

Multiple branching is done by using On…Goto statement that transfers control to any number of choices. It has the following format:-

Line # On Variable Goto 1Line #, 2 Line #, 3 Line #....

Variable must be a positive number from 1 to 255. When the variable is 1 then control jumps to 1 Line #, when variable is 2 the control jumps to 2 Line # and so on.

For…Next Loop

In some cases a part of a program is repeated several times. It is necessary to use a loop to execute the part of the program repeatedly. In For…Next loop the execution of certain statement is repeated for a specific number of times. Its format is:-

For counter = Initial value To Final value

Body of the loop

Next counter

Rest of the program

Program

10 For K= 1 To 3

20 Print “BRC Turbat”

30 Next K

40 End


Output

BRC Turbat

BRC Turbat

BRC Turbat

While and Wend statement

While and Wend statement is used to execute a series of statements in a loop as long as a given condition is true. If the condition is false the loop ends and the program continues to the line following End. While and Wend statement is used when the number of iterations is not known in advance.

Program

10 Print “ente a number”, N

20 F=1, A=N

30 While N >0

40 F=F*N

50 N=N-1

60 Wend

70 Print “the factorial of “,A”=”,F

80 End

Nested Loops

A loop inside another loop is called Nested Loop.

Program

10 For a=1 To 2

20 Print “I Love”

30 For b=1 To 2

40 Print “BRC Turbat”

50 Next b

60 Next a

70 End

Output

I Love

BRC Turbat

BRC Turbat

I Love

BRC Turbat

BRC Turbat








No comments:

Post a Comment