0
Can any 1 tell me what is use of for loop?
The reason given in tutorial is not justifying me. plz explain thanks
10 Answers
+ 1
A dynamic variable can be a lot of thing but a little exemple is with the native function rand () Who give a random number like rand (1, 10) can return 1 or 2 or 3 etc.. so
$a = rand( 1 , 10 );
for ($i = 0; $i <$a ; $i++)
{
echo ($i . "</br>");
}
if $a = 3 this code print:
0
1
2
and if $a = 1 this code print:
0
dynamic variable can be a result of a request to database or the timestamp with time () used for exemple to print the date in real time or anythink
+ 1
For loop is used mainly for two purposes:
1. Repeating a block of code a number of times.
2. Iterating through the values of an array
0
it used to do a loop on a fixed number or a dynamic variable for a lot of thing and not hardcode all possibility
0
ohh okay thanks a lot
0
can u also give eg for dynamic variable
0
thanks again
0
your welcome ankita
0
to make iterative code easy..it assign value to variable also give limit and also increament/decreament it in single line code.
0
i you want some calculations in repeating order
loops will help you eg if you want to add first 10 natural number without loops you can add them but it is time consuming with loops you can do it with very few statements eg
$num=1;
$num2=1;
while($num!=10)
{
$num2+=$num;
$num++;
}
0
Can also be use in generating random number for example BVN or MATRICULATION NUMBER of students