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