I have just entered the world of coding and am learning all about loops. I just learnt about for and while loops but don’t understand why the return different results. Can someone please explain the logic in a lay person’s terms.
/* for loop code */
$counter = 0;
$start = 1;
$end = 11;
for($start;$start<$end;start++) {
$counter=$counter+1;
print $counter;
}
The result I get is 1, 2, 3, 4, 5, 6, 7 , 8, 9 , 10
/* while loop code */
$start=0;
$end=11;
while($start<end) {
$start=$start+1;
print $start;
}
The result I get is 1,2,3,4,5,6,7,8,9,10,11
Why is that the while loop returns a result of 1 through to 11 whilst the for loop returns a result of 1 through to 10
In
forcase$startstarts from 1 but inwhilecase$startstarts from 0.Working on
for–Similarly work on the
whileloop. It’s a paper-pencil exercise.