Am new to the world of coding and it is my first time understanding loops. I saw an example at a website and am utterly confused by the result.
/* Sample Code 1 */
$counter=0
$start=1
for($start;$start<11;start++) {
$counter=$counter+1;
print $counter;
}
This gives me the result of 1,2,3,4,5,6,7,8,9,10
Now if I update the code as follows
/* Sample Code 2 */
$counter=11;
$start=1;
for($start;$start<11;start++) {
$counter=$counter+1;
print $counter;
}
This gives me the result 12,13,14,15,16,17,18,19,20
However if I update the code as follows
/* Sample Code 3 */
$counter=11;
$start=1;
for($start;$start<11;start++) {
$counter=$counter-1;
print $counter;
}
I get the result 10,9,8.7.6,5,4,3,2,1
Please correct me if I am wrong
If the variable $counter has the value of 11, I am essentially start the increment at 11+1 in the code $counter=$counter+1. Is that correct?
But what confuses me is that how is the result in Sample Code 2 possible if my end value in the FOR loop is $start<11. Doesn't this mean it has to be less than 11?
When you start the loop, $start is less than 11. Then it is incremented at the end of the iteration. Then, the loop ends if it has reached 11.
That is, if $start is 10, then it will enter the loop. It reaches 11, so the for statement exits the loop. It is 11 when the loop ends.
Here is a description i wrote
for (//declare loop
$start = 0; //declare starting value and the value to store it in
$start < 10; // Each time it comes through, if $start is under 10, do the loop. if it is 10, exit
$start++ //Increment $start by 1
)
it seems that you're missing "21" in your second example results.
Could this be the cause of your confusion?
I'll give you an explanation from all example above:
Sample Code 1
$counter=0;
$start=1;
This is variable declaration to declare and initialize both variable.
for($start;$start<11;start++) {
$counter=$counter+1;
for loop has structure:
for({loop initialization}; {loop condition}; {per loop process}){
//the rest of loop process
}
Explanation:
As you can see, example #1 start loop from 1 (taken from $start = 1) and doing loop for as long as $start is smaller than 11. To prevent it from looping forever, that code add $start++ which translate to $start = $start + 1. This way, for every loop, $start is added by 1.
the loop condition has to return true in order the loop to run. If it return false, loop will quit.
Now, let's examine what's inside that loop:
$counter=$counter+1;
print $counter;
You see there: $counter=$counter+1. It means, you increment $counter by one for every loop and print the resulting $counter.
Let's breakdown the process (we start loop # with 1 as it's what's defined by $start = 1):
loop # $start ($start < 11?) $counter ($counter = $counter + 1)
1 1 Y 1
2 2 Y 2
3 3 Y 3
4 4 Y 4
5 5 Y 5
6 6 Y 6
7 7 Y 7
8 8 Y 8
9 9 Y 9
10 10 Y 10
11 11 N 11
From process breakdown above, we can see that condition ($start < 11) is result in false on loop #11. That's why the result is 1..10, not 1..11.
Same goes with Example #2:
$counter=11;
$start=1;
Loop structure:
for($start;$start<11;start++) {
$counter=$counter+1;
Let's breakdown this process:
loop # $start ($start < 11?) $counter ($counter = $counter + 1)
1 1 Y 12
2 2 Y 13
3 3 Y 14
4 4 Y 15
5 5 Y 16
6 6 Y 17
7 7 Y 18
8 8 Y 19
9 9 Y 20
10 10 Y 21
11 11 N 22
This will output 12..21. Because when loop #11 occured, it check that $start < 11 is false. Therefore, loop quit.
Please analyze your code.. you have a clear confusion b/w $start and $ counter variable. please use var_dump so as to see what your variables are going through
Related
the title may be pretty confusing. Here's a script which requires a start-end and step value to add numbers to the start value:
function addieren($start, $ende, $schritt = 1) {
if ($start < $ende) {
$erg = 0;
for ($i = $start; $i <= $ende; $i += $schritt) {
$erg += $i;
yield $erg;
}
}
}
foreach (addieren(2, 10, 2) as $erg) {
echo $erg . "<br>";
}
So the start value is 2, the end value is 10 (but it's not ending at 10 as the result shows), and it should add 2 to the $i every step.
Here's the output:
2
6
12
20
30
The first output is clear to me, since the $erg was 0 and it added the 2 because of 2 steps.
But the next output is 6 and I don't get why. In the second loop, $i is 2 and and the script says: $i += $schritt so when $i is 2 and the $schritt value is also 2, why doesn't that output 4 as the second output? Hope you get what I mean.. I guess it's a pure logic error in my head.
I have this piece of code that goes through an array where each position contains a line of previously pasted text. I want the for loop to skip 2 times the number of columns input by the user, once it reaches a line that contains the word "Total". I've searched around but all I found were answers for other languages. Can anybody enlighten me on this?
Code:
1 $j = -1;
2 $step = 1;
3 for($i = 0; $i < count($statisticsinput); $i++){
4 if(strpos($statisticsinput[$i], "Total") !== false){
5 $i += 2*$_POST['columno']; //Trying to make the counter skip the 2*col iterations but seems to have no effect
6 if($step !== 2){ //The word Total appears 2 times in the pasted text, the first time
7 $step++; //should keep the script going but not the second one
8 }else{
9 break;
10 }
11 continue; //After incrementing the counter 2*col, skip over the next steps and
12 } //go to the next loop. I expected it to jump 2*col loops (usually 22)
13 if($i % $_POST['columno'] == 0){
14 $j++;
15 }
16 $employees[$j][] = $statisticsinput[$i];
17 }
Many thanks.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a problem understanding how is the result 34 when it should be 32.Because the loop run's 4 times so when u add up 8 to the variable age it should bring up the sum as 32.Maybe I'm wrong please help out to understand.TQ
<?php
$age=24;
for($i=0; $i<=4; $i++){
$age= $age + 2;
}
echo ("At the end of the loop age = $age" );
?>
Result >>>>>>At the end of the loop age = 34
Your loop is not running four times; it's running five times.
$i<4 means the loop will terminate when $i reaches four - it terminates before that execution happens. $i<=4 means "continue looping so long as $i is less than or equal to four"
So, let's work through the examples:
for($i=0; $i<=4; $i++)
Begins, sets $i to value of 0
Loop: is $i less than or equal to 4? $i=0, so yes. $age += 2. ($age now equals 26).
End of first loop: $i++ ($i now equals 1).
Loop: is $i less than or equal to 4? $i = 1, so yes. $age += 2; ($age now equals 28).
End of second loop: $i++ ($i now equals 2).
Loop; is $i less than or equal to 4? $i = 2, so yes. $age += 2; ($age now equals 30).
End of third loop: $i++ ($i now equals 3).
Loop; is $i less than or equal to 4? $i = 3, so yes. $age += 2; ($age now equals 32).
End of fourth loop: $i++ ($i now equals 4).
Loop; is $i less than or equal to 4? $i = 4, so yes -- $i is equal to four, as specified by <=. $age += 2; ($age now equals 36).
End of fifth loop: $i++ ($i now equals 5).
Loop; is $i less than or equal to 4? $i = 5, so no. Loop terminates.
Final result: $age = 36
The number of elements between 0 and positive N is N+1.
You loop through 0 1 2 3 4.
Those are 5 iterations.
24 + (2*5) = 34.
the loop runs from i=0 to i=4
i age
0 26
1 28
2 30
3 32
4 34 ------>loop stops when i=5 since 5<=4 turns false
5
it should be
<?php
$age=24;
for($i=0; $i<4; $i++){
$age= $age + 2;
}
echo ("At the end of the loop age = $age" );
?>
the loop runs from i=0 to i=3
i age
0 26
1 28
2 30
3 32 ------>loop stops when i=4 since 4<4 turns false
4
5
Well that's because the loop starts from 0, so it runs 5 times (0,1,2,3,4).
Loop 1 (when value is 0): 24 + 2 = 26;
Loop 2 (when value is 1): 26 + 2 = 28;
Loop 3 (when value is 2): 28 + 2 = 30;
Loop 4 (when value is 3): 30 + 2 = 32;
Loop 5 (when value is 4): 32 + 2 = 34;
you can either start with 1 as:
for($i=1; $i<=4; $i++)
or make it < 4 instead of <= 4, as:
for($i=0; $i<4; $i++)
Using PHP 5.6, why does this code outputs
2
5
8
11
instead of
3
6
9
?
for ($i = 0; $i < 10; $i++) {
echo $i += 2 . "<br />";
}
Because i starts at 0, not 1. Lets see what is happening:
Loop 1
Initiate i, i = 0;
Check for loop condition ($i < 10). i is 0 so yes, run the loop.
Add 2 to i and echo. 0 + 2 = 2, echo 2.
End of loop, add 1 to i. i is 3.
Loop 2
Check for loop condition ($i < 10). i is 3 so yes, run the loop.
Add 2 to i and echo. 3 + 2 = 5, echo 5.
End of loop, add 1 to i. i is 6.
And so on. So you add 2, echo out then add 1.
You may be expecting the i++ in the for loop to run before the code, but it runs at the end of the code. From the PHP website:
At the end of each iteration, expr3 is evaluated (executed).
If you want your output to be 3, 6, 9 then you would need to initiate i to 1 at the start of your loop.
for($i = 1; $i < 10; $i++)
The increment (i++) in the loop header happens after the loop body is executed. So, i is initialized to 0, then you add 2 and print it. Then, the loop header increments it to 3, then you add 2 and print it again...
In the first iteration, you add 0 + 2, because you declared $i to be 0, so the output would be 2. then with the $i++ you do the same as you would write $i += 1. So now the value of $i is 3.
Then you add again 2 which gives you 5 as an output, then you add another 1. Value of $i = 6, add 2... outputs 8... add 1... add 2... outputs 11...
following is the steps in which i want to do things:
assign a value 1 to $x variable
using for loop increment this value upto 7, so that output is : 1234567
now multiply each value with 7, so that output is : 7 14 21 28 35 42 49.
for this i made the following code, but it does not work.
$yy=1;
for($yy==1; $yy<=8; $yy++){
$yy*7;
}echo $yy;
and i tried the do-while also :
$yy=1;
do{$yy*7;
echo $yy;}
while(
$yy>=7
)
but non is working. i think foreach will work here, not tried yet as i am not on it yet. will it be possible with any of these 2 functions?
You just need for loops:
for($yy = 1; $yy <= 7; $yy++){
echo $yy;
}
echo '<br/>';
for($yy = 1; $yy <= 7; $yy++){
echo $yy*7 .' ';
}
The result will be like this
1234567
7 14 21 28 35 42 49
You got wrong syntax: $yy==1 (comparison operator) on your for looop, initialize $yy=1 (assign operator) then the condition is $yy<=7 not eight