0% found this document useful (0 votes)
17 views4 pages

For Loop in BASIC

The document provides an overview of the for loop in BASIC programming, detailing its syntax and different types, including standard iteration, step increments, and reverse iteration. It includes examples of using the for loop to check for prime numbers, count positive, negative, and zero values from user input, and determine if a number is an Armstrong number. Additionally, it mentions the 'exit for' statement to break out of a loop.

Uploaded by

andysingh5599
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

For Loop in BASIC

The document provides an overview of the for loop in BASIC programming, detailing its syntax and different types, including standard iteration, step increments, and reverse iteration. It includes examples of using the for loop to check for prime numbers, count positive, negative, and zero values from user input, and determine if a number is an Armstrong number. Additionally, it mentions the 'exit for' statement to break out of a loop.

Uploaded by

andysingh5599
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BASIC PROGRAMMING

For Loop
The for loop is one of the looping statements in which we can iterate the
expressions multiple times by declaring the number of loops.
It is also known as ‘Senitel’ in programming environment. The for loop is used to
solve the normal programs, list-based programs, array-based programs and to
solve the pattern matching operation.
There are three different types of the for loop that is –
a. When the loop iterates and only one counter value becomes increased at
a time then the following syntax is used.
For lower_bound to upper_bound
Expressions
Next counter
For i=1 to 10
Print i
Next i

b. When the loop counter value need to increase more than one times then
the ‘step’ keyword along with the value of increment is written.
For lower_bound to upper_bound step n
Expressions
Next counter
For i=1 to 20 step 3
Print i
Next i
c. When the loop iterates from upper value to lower value then step with
negative decrement value must be written.
For upper_bound to lower_bound step -n

PROF. JITENDRA KUMAR SINHA 1


BASIC PROGRAMMING

Expressions
Next counter

For i=10 to 1 step -2


Print i
Next i

Once a user need to forcefully came out from the for loop body then the BASIC
programming provides one of the statement known as ‘exit for’.

Q WAP that accept any number then check it is prime number or not?
Cls
C=0
Input “Enter the number:” ,num
For i=2 to num-1
If num mod i=0 then
C=1
Exit for
End if
Next i
If c=0 then
Print “Prime”
Else
Print “Not prime”
End if

Q WAP that accept 10 numbers count how many of them are +ve, -ve or zero?
Cls
PROF. JITENDRA KUMAR SINHA 2
BASIC PROGRAMMING

P=0
N=0
Z=0
Print “Enter 10 numbers:”
For i=1 to 10
Input num
If num > 0 then
P=p+1
Elseif num < 0 then
N=n+1
Elseif num=0 then
Z=z+1
End if
Next i
Print “+ve=”,p
Print “-ve=”,n
Print “Zero=”,z

Q WAP that accept any number check it is armstrom number or not?


Cls
s=0
Input "Enter the number:", n
x=n
While x > 0
d = x Mod 10
x = x \ 10

PROF. JITENDRA KUMAR SINHA 3


BASIC PROGRAMMING

s=s+d*d*d
Wend
If n = s Then
Print "Armstrom"
Else
Print "Not armstrom"
end if

PROF. JITENDRA KUMAR SINHA 4

You might also like